# frozen_string_literal: true

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

  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
        pdf1a = WickedPdf.new.pdf_from_string(
               render_cover_header(@dotation, @company_sizes),
               {pdf: 'cover_header',
               dpi: 300,
               margin: { top: 0, bottom: 0, left: 0, right: 0 },
               background: true,
               no_background: false,
               enable_local_file_access: true})
        pdf1 = WickedPdf.new.pdf_from_string(
               render_to_stringi(@dotation, @company_sizes), {pdf: @dotation.safe_id, # Excluding ".pdf" extension.
               header: { content: render_to_string({ template: "shared/pdf_header.pdf", layout: false }), line: false },
               footer: { content: render_to_string({ template: 'shared/pdf_footer.html', layout: false }), line: false },
               dpi: 300,
               margin: { top: 50, bottom: 50, left: 0, right: 0 },
               background: true,
               no_background: false,
               enable_local_file_access: true})
        #
        pdf1b = WickedPdf.new.pdf_from_string(
               render_cover_footer(@dotation, @company_sizes),
               {pdf: 'cover_footer',
               dpi: 300,
               margin: { top: 0, bottom: 0, left: 0, right: 0 },
               background: true,
               no_background: false,
               enable_local_file_access: true})
        combiner = CombinePDF.new
        combiner << CombinePDF.parse(pdf1a)
        combiner << CombinePDF.parse(pdf1)
        combiner << CombinePDF.parse(pdf1b)
        send_data combiner.to_pdf
        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
    dotation = @dotations.first
    respond_to do |format|
      format.pdf do
        pdf1a = WickedPdf.new.pdf_from_string(
               render_cover_header(dotation, @company_sizes),
               {pdf: 'cover_header',
               dpi: 300,
               margin: { top: 0, bottom: 0, left: 0, right: 0 },
               background: true,
               no_background: false,
               enable_local_file_access: true})
        pdf1 = WickedPdf.new.pdf_from_string(
               render_to_pdf_all(@dotations, @company_sizes), {pdf: "zestawienie_dotacji_#{Time.now.strftime('%d%m%Y')}", # Excluding ".pdf" extension.
               header: { content: render_to_string({ template: "shared/pdf_header.pdf", layout: false }), line: false },
               footer: { content: render_to_string({ template: 'shared/pdf_footer.html', layout: false }), line: false },
               dpi: 300,
               margin: { top: 50, bottom: 50, left: 0, right: 0 },
               background: true,
               no_background: false,
               enable_local_file_access: true})
        #
        pdf1b = WickedPdf.new.pdf_from_string(
               render_cover_footer(dotation, @company_sizes),
               {pdf: 'cover_footer',
               dpi: 300,
               margin: { top: 0, bottom: 0, left: 0, right: 0 },
               background: true,
               no_background: false,
               enable_local_file_access: true})
        combiner = CombinePDF.new
        combiner << CombinePDF.parse(pdf1a)
        combiner << CombinePDF.parse(pdf1)
        combiner << CombinePDF.parse(pdf1b)
        send_data combiner.to_pdf, filename: "zestawienie_dotacji_#{Time.now.strftime('%d%m%Y')}.pdf"
      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 pdf_header

  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
