module IdCardPrinterService
    class PdfProcessor

        def initialize(employer, layout, zip = false)
            @employer = employer
            @layout = layout
            @zip = zip
        end

        def call
            # if @zip
            #     group_cards_pdf_array = []
            # else
            #     group_cards_pdf = CombinePDF.new
            # end
            group_cards_pdf_array = []
            IdCard::PrintData.where(pl_plan_key: @employer.pl_plan_key).each do |card|
                url = IdCardPrinterService::JasperUrlGenerator.new(@employer.pl_plan_key, card.family_id, @layout).call
                puts url
                card_pdf = IdCardPrinterService::JasperPdfGenerator.new(url).call
                if @zip
                    card_filename = "#{card.full_name_last_name_first.gsub(", ", "_")}_digital_card_#{Date.today}.pdf"
                    group_cards_pdf_array << { name: card_filename, data: card_pdf.to_pdf }
                else
                    group_cards_pdf_array << card_pdf
                end
            end

            # if @zip
            #     group_cards_pdf = Zip::OutputStream.write_buffer do |zio|
            #         group_cards_pdf_array.each do |file|
            #             zio.put_next_entry(file[:name])
            #             zio.write(file[:data])
            #         end
            #     end
            # # else
            # #     todays_date = DateTime.current.strftime('%Y%m%d%H%M%S')
            # #     group_cards_pdf.save("tmp/#{@employer.name}_print_cards_#{todays_date}.pdf")
            # end

            group_cards_pdf_array
        end
    end
end