class Admin::NeedsController < ApplicationController
  layout "admin"
  before_filter :auth_admin
  before_action :build_category_tree, only:[:new, :update, :create, :edit, :index]

  def index

    # Get all needs to validate
    @needs_to_validate = Need.where(state: 'created').order(created_at: :desc)


    @needs = Need.shared

    # filters default value
    params[:o] ||= 'created-desc'
    params[:r] ||= 10

    # Include search in the query
    if(params[:q] != '')
      @needs = @needs.search(params[:q])
    end

    if(params[:c] and params[:c] != '')
      @category = NeedCategory.find(params[:c])
      ids =  @category.child_ids
      @needs = @needs.where(category_id: ids)
    end

    # Include order in the query
    case params[:o]
    when 'alpha-asc'
      @needs = @needs.order(title: :asc)
    when 'alpha-desc'
      @needs = @needs.order(title: :desc)
    when 'wishes-asc'
      @needs = @needs.with_wishes_count.order('wishes_count ASC')
    when 'wishes-desc'
      @needs = @needs.with_wishes_count.order('wishes_count DESC')
    when 'created-asc'
      @needs = @needs.order(created_at: :asc)
    when 'created-desc'
      @needs = @needs.order(created_at: :desc)
    when 'comments-asc'
      @needs = @needs.with_messages_count.order('messages_count ASC')
    when 'comments-desc'
      @needs = @needs.with_messages_count.order('messages_count DESC')
    end

    # Paginate
    @needs = @needs.page(params[:page]).per(params[:r])


    # Define order select options
    @orders = {
      "Les plus récents" => 'created-desc',
      "Les plus populaires" => 'wishes-desc',
      "Les plus commentés" => 'comments-desc',
      "Titre (de A à Z)" => 'alpha-asc',
      "Titre (de Z à A)" => 'alpha-desc'
    }

  end

  def new
    @need = Need.new()
    @comments = @need.messages.order(created_at: :desc).page params[:page]
  end

  def download_devis
    @need = Need.find(params[:need_id])
    send_file @need.devis.file.path,  :filename => "proposition-besoin-#{@need.id}.#{@need.devis.file.extension}"
  end

  def create
    @need = Need.new(need_params)

    if @need.save
      flash[:notice] = "Besoin créé avec succès."
      @need.validate!
      redirect_to admin_needs_path
    else
      render :action => "new"
    end
  end

  def edit

    @need = Need.find(params[:id])

  end

  def update
    @need = Need.find(params[:id])
    if @need.update_attributes(need_params)
      flash[:notice] = "Besoin sauvegardé avec succès."
      redirect_to admin_needs_path
    else
      render :action => "edit"
    end
  end

  def destroy
    @need = Need.find(params[:id])
    if(@need.destroy)
      flash[:notice] = "Besoin supprimé avec succès."
    end
    redirect_to admin_needs_path
  end


  def back_to_verified
    @need = Need.find(params[:id])
    if @need.back_to_verified!
      flash[:notice] = "Besoin repositionné à l'état vérifié"
    else
      flash[:error] = "L'état actuel de ce besoin ne permet pas ce changement d'état"
    end
    redirect_to admin_needs_path
  end

  def back_to_negociating
    @need = Need.find(params[:id])
    if @need.back_to_negociating!
      flash[:notice] = "Besoin repositionné à l'état de négociation en cours"
    else
      flash[:error] = "L'état actuel de ce besoin ne permet pas ce changement d'état"
    end
    redirect_to admin_needs_path
  end


  def validate
    @need = Need.find(params[:id])
    if @need.validate!
      flash[:notice] = "Besoin validé avec succès"
    else
      flash[:error] = "L'état actuel de ce besoin ne permet pas sa validation"
    end
    redirect_to admin_needs_path
  end

  def refuse
    @need = Need.find(params[:id])
    if @need.refuse!
      flash[:notice] = "Besoin refusé avec succès"
    else
      flash[:error] = "L'état actuel de ce besoin ne permet son refus"
    end
    redirect_to admin_needs_path
  end

  def negociate
    @need = Need.find(params[:id])
    if @need.negociate!
      flash[:notice] = "Le besoin est maintenant en cours de négociation"
    else
      flash[:error] = "L'état actuel de ce besoin ne permet pas cette action"
    end
    redirect_to admin_needs_path
  end

  def reject
    @need = Need.find(params[:id])
    if @need.reject!
      flash[:notice] = "Le besoin est maintenant en négociation échouée"
    else
      flash[:error] = "L'état actuel de ce besoin ne permet pas cette action"
    end
    redirect_to admin_needs_path
  end

  def accept
    @need = Need.find(params[:id])
    if @need.offers.length < 1
      flash[:error] = "Vous devez créer au moins une offre avant de passer ce besoin en négocié"
    else
      if @need.accept!
        flash[:notice] = "Le besoin est maintenant négocié"
      else
        flash[:error] = "L'état actuel de ce besoin ne permet pas cette action"
      end
    end

    redirect_to admin_needs_path
  end

  private

  def need_params
    params.require(:need).permit!
    #params.require(:need).permit(:domain_ids, :title, :image_file_id, :description, :category_id, :author_id)
  end

  def build_category_tree
    @tree = NeedCategory::create_tree
  end
end