# frozen_string_literal: true

# Home
class HomeController < ApplicationController
  layout 'home_layout'
  before_action :check_status

  def check_status
    if defined?(current_user) && user_signed_in?
    else
      redirect_to '/construction.html'
    end
  end

  def index
    require 'json'
    prepare_filters
    cookies[:filter] = JSON.generate(build_filter_hash)
    @dotations = Dotation.extra_search(params[:search]).public_dot
                         .point_desc.end_date_asc.page(params[:page])
    # count on all
    @dotations_size = Dotation.extra_search(params[:search]).public_dot
                              .point_desc.end_date_asc.count
  end

  def search
    require 'json'
    prepare_filters
    cookies[:filter] = JSON.generate(build_filter_hash)
    @dotations = Dotation.search_with_filters(build_filter_hash).public_dot
                         .point_desc.end_date_asc.page(params[:page])
    # count on all
    @dotations_size = Dotation.search_with_filters(build_filter_hash).public_dot
                              .point_desc.end_date_asc.count
  end

  def contact
    @email_message = EmailMessage.new
  end

  def register_contact
    @email_message = EmailMessage.new(email_messages_params)
    return unless @email_message.save

    SendEmailJob.perform_later(@email_message.id)
  end

  def order_meeting
    @dotation = Dotation.friendly.find(params[:d])
    @consultation_email = ConsultationEmail.new
  end

  def order_meeting_save
    @dotation = Dotation.friendly.find(params[:d])
    @consultation_email = ConsultationEmail.new(consultation_email_params)
    @consultation_email.dotation_id = @dotation.id
    return unless @consultation_email.save

    SendContactEmailJob.perform_later(@consultation_email.id)
  end

  def show
    @dotation = Dotation.friendly.find(params[:id])
    @intersting_dotations = Dotation.public_dot.point_desc
                                    .where.not(id: @dotation.id).limit(4)
    @company_sizes = CompanySize.all
    respond_to do |format|
      format.html
      format.pdf do
        render pdf: @dotation.safe_id # Excluding ".pdf" extension.
      end
    end
  end

  def emailfilter
    require 'json'
    @emailfilter = FilterForEmail.new(
      email: params[:email_filter_inp],
      filters: JSON.parse(cookies[:filter])
    )
    @emailfilter.save
    SendNotifyEmailJob.perform_later(@emailfilter.id, 1)
  end

  def prepare_pdf
    filters = JSON.parse(cookies[:filter]).transform_keys(&:to_sym)
    @company_sizes = CompanySize.all
    @dotations = Dotation.search_with_filters(filters).public_dot
                         .point_desc.end_date_asc
    respond_to do |format|
      format.pdf do
        render pdf: "zestawienie_dotacji_#{Time.now.strftime('%d%m%Y')}"
      end
    end
  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

  # Only allow a list of trusted parameters through.
  def email_messages_params
    params.require(:email_message).permit(:subject, :email, :message)
  end

  def consultation_email_params
    params.require(:consultation_email).permit(:phone_number, :email,
                                               :project_value)
  end
end
