# frozen_string_literal: true

# Home
class HomeController < ApplicationController
  layout 'home_layout'

  def index
    prepare_filters
    @dotations = Dotation.extra_search(params[:search]).public_dot
                         .point_desc.end_date_asc.page(params[:page])
  end

  def search
    prepare_filters
    @dotations = Dotation.search_with_filters(build_filter_hash).public_dot
                         .point_desc.end_date_asc.page(params[:page])
  end

  def contact; end

  def show
    @dotation = Dotation.find(params[:id])
    @company_sizes = CompanySize.all
  end

  private

  def prepare_filters
    @company_size_chk = params[:company_size] || []
    @company_activity_chk = params[:company_activity] || []
    @project_chk = params[:project] || []
    @company_sizes = CompanySize.all
    @company_activities = CompanyActivity.all
    @projects = Project.all
  end

  def build_filter_hash
    {
      search: params[:search], company_sizes: @company_size_chk,
      company_activities: @company_activity_chk, projects: @project_chk,
      localization: params[:localization], ammount_chk: params[:ammount_chk],
      ammount_price: params[:ammount_price]
    }
  end
end
