class GrantsController < ApplicationController
  before_action :authenticate_user!
  include ApplicationHelper
  before_action :check_access
  before_action :set_dotation, only: %i[show edit update destroy activate]

  # GET /dotations or /dotations.json
  def index
    @dotations =
      Dotation.search(params[:search]).order(:name).page(params[:page])
  end

  def check_access
    redirect_to not_found unless role?('dotations')
  end

  # GET /dotations/1 or /dotations/1.json
  def show
    @company_sizes = CompanySize.all
  end

  # GET /dotations/new
  def new
    @dotation = Dotation.new
    prepare_dependencies
  end

  # GET /dotations/1/edit
  def edit
    prepare_dependencies
  end

  def activate
    @dotation.active = if @dotation.active.eql?(false)
                         true
                       else
                         false
                       end
    @dotation.save

    NotifyEmailsJob.perform_later(@dotation.id) if @dotation.active.eql?(true)
  end

  # POST /dotations or /dotations.json
  def create
    @dotation = Dotation.new(dotation_params)
    prepare_dependencies
    respond_to do |format|
      if @dotation.save
        format.html { redirect_to grants_url, notice: 'Utworzono pomyślnie.' }
        format.json { render :show, status: :created, location: @dotation }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @dotation.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /dotations/1 or /dotations/1.json
  def update
    prepare_dependencies
    respond_to do |format|
      if @dotation.update(dotation_params)
        format.html { redirect_to grants_url, notice: 'Zaktualizowano pomyślnie.' }
        format.json { render :show, status: :ok, location: @dotation }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @dotation.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /dotations/1 or /dotations/1.json
  def destroy
    @dotation.destroy

    respond_to do |format|
      format.html { redirect_to grants_url, notice: 'Usunięto pomyślnie.' }
      format.json { head :no_content }
    end
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_dotation
    @dotation = Dotation.friendly.find(params[:id])
  end

  def prepare_dependencies
    @currencies = Currency.all
    @company_sizes = CompanySize.all
    @company_activities = CompanyActivity.all
    @tags = Tag.all
    @projects = Project.all
    @experts = Expert.all
    @expenses = Expense.all
    @partners = Partner.all
  end

  # Only allow a list of trusted parameters through.
  def dotation_params
    params.require(:dotation)
          .permit(:name, :formal_name, :date_from, :date_to, :min_amount,
                  :max_amount, :max_percent, :localization, :company_size_id,
                  :partner_id, :expert_id, :expert_info, :start_date, :end_date,
                  :announcement_date, :full_descr, :positioning_text, :points,
                  :date_of_recruitment, :min_amount_curr_id,
                  :max_amount_curr_id, :min_dot_ammount, :max_dot_ammount,
                  :min_dot_amount_curr_id, :max_dot_amount_curr_id,
                  :start_date_quarter, :end_date_quarter, :ann_date_quarter,
                  project_ids: [], tag_ids: [], company_activity_ids: [],
                  company_size_ids: [], expense_ids: [])
  end
end
