module IdCard
    class PrintController < ApplicationController

        # View Methods
        def index
            @employer_setups = IdCard::Setup.active.to_a
            @queue_members = IdCardQueueService::GetQueuedMembers.new().call
            add_queued_count_to_employer_setup(@queue_members)
            @queued = @employer_setups.select { |setup| setup.queued_card_count > 0 }.sort_by { |setup| setup.print_name }
            @not_queued = @employer_setups.select { |setup| setup.queued_card_count == 0 }.sort_by { |setup| setup.print_name }
            render :index
        end

        # API Methods

        def print_all_queued
            @queue_members = IdCardQueueService::GetQueuedMembers.new().call
            cards_pdf = IdCardPrinterService::CardsGenerator.new(@queue_members, "PrintCard").call

            send_data cards_pdf.to_pdf,
                filename: "all_queued_cards_#{Date.today}.pdf",
                type: "application/pdf",
                disposition: 'attachment'

        end

        def print_queued_by_employer
            pl_plan_key = params[:id].to_s
            @employer = Employer.find_by(pl_plan_key: pl_plan_key)
            @queue_members = IdCardQueueService::GetQueuedMembers.new(pl_plan_key).call
            cards_pdf = IdCardPrinterService::CardsGenerator.new(@queue_members, "PrintCard").call

            send_data cards_pdf.to_pdf,
                filename: "#{@employer.name.parameterize(separator: "_")}_queued_cards_#{Date.today}.pdf",
                type: "application/pdf",
                disposition: 'attachment'

        end

        def generate_sample
            if Integer(params[:id], exception: false).is_a?(Integer)
                pl_plan_key = params[:id].to_s
                @employer = Employer.find_by(pl_plan_key: pl_plan_key)
            else
                slug = params[:id]
                @employer = Employer.find_by(slug: slug)
            end
            sample_cards_pdf = IdCardPrinterService::SampleCardsGenerator.new(@employer).call

            send_data sample_cards_pdf.to_pdf,
                filename: "#{@employer.name.parameterize(separator: "_")}_sample_cards_#{Date.today}.pdf",
                type: "application/pdf",
                disposition: 'attachment'

        end

        def generate_print
            if Integer(params[:id], exception: false).is_a?(Integer)
                pl_plan_key = params[:id].to_s
                @employer = Employer.find_by(pl_plan_key: pl_plan_key)
            else
                slug = params[:id]
                @employer = Employer.find_by(slug: slug)
            end
            cards_pdf = IdCardPrinterService::CardsGenerator.new(@employer.employer_member_keys, "PrintCard").call

            send_data cards_pdf.to_pdf,
                filename: "#{@employer.name.parameterize(separator: "_")}_print_cards_#{Date.today}.pdf",
                type: "application/pdf",
                disposition: 'attachment'

        end

        def generate_mobile_display
            if Integer(params[:id], exception: false).is_a?(Integer)
                pl_plan_key = params[:id].to_s
                @employer = Employer.find_by(pl_plan_key: pl_plan_key)
            else
                slug = params[:id]
                @employer = Employer.find_by(slug: slug)
            end
            cards_pdf = IdCardPrinterService::EmployerCardsGenerator.new(@employer, "MobileDisplayCard").call

            send_data cards_pdf.to_pdf,
                filename: "#{@employer.name.parameterize(separator: "_")}_mobile_display_cards_#{Date.today}.pdf",
                type: "application/pdf",
                disposition: 'attachment'

        end

        def generate_full_page
            if Integer(params[:id], exception: false).is_a?(Integer)
                pl_plan_key = params[:id].to_s
                @employer = Employer.find_by(pl_plan_key: pl_plan_key)
            else
                slug = params[:id]
                @employer = Employer.find_by(slug: slug)
            end
            cards_pdf = IdCardPrinterService::CardsGenerator.new(@employer.employer_member_keys, "FullPageCard", true).call
            # cards_pdf = IdCardPrinterService::EmployerCardsGenerator.new(@employer, "FullPageCard", true).call

            cards_pdf.rewind
            send_data cards_pdf.sysread, 
                        filename: "#{@employer.name.parameterize(separator: "_")}_full_page_cards_#{Date.today}.zip", 
                        type: 'application/zip',
                        disposition: 'attachment'

        end

        private
        

        def add_queued_count_to_employer_setup(queued_employer_members)
            queued_employer_members.each do |queued_employer|
                match = @employer_setups.find { |setup| setup.pl_plan_key == queued_employer[:pl_plan_key] }
                if match.present?
                    match.queued_card_count = queued_employer[:member_keys].length
                end
            end
        end

    end
end