module IdCardPrinterService
    class MemberCardsGenerator

        def initialize(member_keys, layout)
            @member_keys = Array.wrap(member_keys)
            @employer = employer
            @layout = layout
            @zip = zip
        end

        def call
            IdCard::PrintData.where(mb_member_key: @employer.pl_plan_key).destroy_all
            IdCardPrinterService::EmployerDataFormatter.new(@employer).call

            pdf_array = IdCardPrinterService::PdfProcessor.new(@employer, @layout, @zip).call

            group_pdfs = combine_pdfs(pdf_array)
            group_pdfs

        end

        private

        def combine_pdfs(pdf_array)
            if @zip
                group_cards_pdf = Zip::OutputStream.write_buffer do |zio|
                    pdf_array.each do |file|
                        zio.put_next_entry(file[:name])
                        zio.write(file[:data])
                    end
                end
            else
                group_cards_pdf = CombinePDF.new
                pdf_array.each { |pdf| group_cards_pdf << pdf }
            end
        end
    end
end
