class Admin::DocumentsController < ApplicationController
  layout "admin"
  before_filter :auth_admin
  before_filter :load_offer, :except => [:edit, :update]

  def edit
    @document = Document.find(params[:id])
  end

  def update
    @document = Document.find(params[:id])
    
    if @document.update_attributes(params.require(:document).permit!)
      
    else
      render :action => "edit"
    end
    
    
  end

  def create

    @accepted_offer.documents.create({title: params[:document][:title]})


    if @accepted_offer.save
      flash[:success] = "Document créé"
    else
      flash[:error] = "Impossible de créer le document"
    end
    redirect_to :back
  end

  def upload_document
    if !params[:document]
      flash[:error] = "Vous devez sélectionner un fichier"
    else

      @document = @accepted_offer.documents.find(params[:document_id])
      @document.document = params[:document]
      if @document.save
        @document.state = :document_available

        CustomerMailer.document_available(@accepted_offer.customer, @document).deliver

        if @document.returned_document?
          @document.remove_returned_document!
        end
        @document.save
        flash[:success] = "Document chargé"
      else
        flash[:error] = "Impossible de charger le document"
      end
    end
    redirect_to :back
  end

  def upload_returned_document
    if !params[:returned_document]
      flash[:error] = "Vous devez sélectionner un fichier"
    else

      @document = @accepted_offer.documents.find(params[:document_id])
      @document.returned_document = params[:returned_document]
      if @document.save
        @document.state = :document_returned
        @document.save
        flash[:success] = "Document chargé"
      else
        flash[:error] = "Impossible de charger le document"
      end
    end
    redirect_to :back
  end


  def load_offer
    @offer = Offer.find(params[:offer_id])
    @accepted_offer = @offer.accepted_offers.find(params[:accepted_offer_id])
  end

  def download
    @document = @accepted_offer.documents.find(params[:document_id])
    send_file @document.document.file.path
  end

  def download_returned
    @document = @accepted_offer.documents.find(params[:document_id])
    send_file @document.returned_document.file.path
  end

  def delete
    @document = @accepted_offer.documents.find(params[:document_id])
    if @document.document? && @document.state != :document_verified
      @document.remove_document!
      if @document.returned_document?
        @document.remove_returned_document!
      end
      @document.state = :not_available
      @document.save
      flash[:success] = "Fichier supprimé"
    else
      flash[:error] = "Impossible de supprimer le fichier"
    end
    redirect_to :back
  end

  def destroy
    @document = @accepted_offer.documents.find(params[:document_id])
    if @document.destroy

      flash[:success] = "Document Supprimé"
    else
      flash[:error] = "Impossible de supprimer le document"
    end
    redirect_to :back
  end

  def delete_returned
    @document = @accepted_offer.documents.find(params[:document_id])
    if @document.returned_document? && @document.state != :document_verified
      @document.remove_returned_document!
      @document.state = :document_available
      @document.save
      flash[:success] = "Fichier supprimé"
    else
      flash[:error] = "Impossible de supprimer le fichier"
    end
    redirect_to :back
  end

  def force_verified
    @document = @accepted_offer.documents.find(params[:document_id])
    @document.state = :document_verified


    if @document.save
      flash[:success] = "Document vérifié"
    else
      flash[:error] = "Impossible vérifier le document"
    end
    redirect_to :back
  end

  def verify_returned
    @document = @accepted_offer.documents.find(params[:document_id])
    @document.state = :document_verified


    if @document.save
      CustomerMailer.document_verified(@accepted_offer.customer, @document).deliver
      flash[:success] = "Document vérifié"
    else
      flash[:error] = "Impossible vérifier le document"
    end
    redirect_to :back
  end
end
