module IdCardPrinterService
    class PdfFormatter

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

        def call

            if @layout == "FullPageCard"
                pdf_file = @pdf_array.first
                puts "-- CombinePdfPages --"
                puts pdf_file.pages.count
                pdf_file.pages.reverse.each do |page|
                    page_pdf = CombinePDF.new
                    page_pdf << page
                    page_data = page_pdf.to_pdf
                    full_name_last_name_first = get_watermark_field(page_data)
                    if full_name_last_name_first.present?
                        page_filename = "#{full_name_last_name_first.gsub(", ", "_")}_digital_card_#{Date.today}.pdf"
                        group_cards_pdf_array << { name: page_filename, data: page_data }
                    end
                end
                        
                group_cards_pdf = Zip::OutputStream.write_buffer do |zio|
                    pdf_file = pdf_array.first
                    puts "-- CombinePdfPages --"
                    puts pdf_file.pages.count
                    pdf_file.pages.reverse.each do |page|
                        page_pdf = CombinePDF.new
                        page_pdf << page
                        page_data = page_pdf.to_pdf
                        full_name_last_name_first = get_watermark_field(page_data)
                        if full_name_last_name_first.present?
                            page_filename = "#{full_name_last_name_first.gsub(", ", "_")}_digital_card_#{Date.today}.pdf"
                            puts "-- Filename --"
                            puts page_filename
                            zio.put_next_entry(page_filename)
                            zio.write(page_data)
                        end
                    end
                end
            else
                group_cards_pdf = CombinePDF.new
                pdf_array.each { |pdf| group_cards_pdf << pdf }
            end
            group_cards_pdf


            
        end
        
        private
        
        def get_watermark_field(page_data)
            watermark_field = ""
            reader = PDF::Reader.new(StringIO.new(page_data))
            puts "-- ReaderPages --"
            puts reader.pages.count
            page = reader.pages.first
            if watermark_match = page.text.match(/:WATERMARK:([^:]*):/)
                watermark_field = page.text.match(/:WATERMARK:([^:]*):/)[1].strip
            else
                puts "blank page"
            end
            watermark_field
        end

        def break_into_batches(pl_plan_keys)
            batches_by_card_template = IdCard::Setup.where(pl_plan_key: pl_plan_keys).group_by(&:card_template)
                .transform_values { |setups| setups.map(&:pl_plan_key) }
                .compact_blank
        end

        def combine_pdfs(pdf_array)
            if @zip
                group_cards_pdf = Zip::OutputStream.write_buffer do |zio|
                    pdf_file = pdf_array.first
                    puts "-- CombinePdfPages --"
                    puts pdf_file.pages.count
                    pdf_file.pages.reverse.each do |page|
                        page_pdf = CombinePDF.new
                        page_pdf << page
                        page_data = page_pdf.to_pdf
                        full_name_last_name_first = get_watermark_field(page_data)
                        if full_name_last_name_first.present?
                            page_filename = "#{full_name_last_name_first.gsub(", ", "_")}_digital_card_#{Date.today}.pdf"
                            puts "-- Filename --"
                            puts page_filename
                            zio.put_next_entry(page_filename)
                            zio.write(page_data)
                        end
                    end
                end
            else
                group_cards_pdf = CombinePDF.new
                pdf_array.each { |pdf| group_cards_pdf << pdf }
            end
            group_cards_pdf
        end

        def get_watermark_field(page_data)
            watermark_field = ""
            reader = PDF::Reader.new(StringIO.new(page_data))
            puts "-- ReaderPages --"
            puts reader.pages.count
            page = reader.pages.first
            if watermark_match = page.text.match(/:WATERMARK:([^:]*):/)
                watermark_field = page.text.match(/:WATERMARK:([^:]*):/)[1].strip
            else
                puts "blank page"
            end
            watermark_field
        end
    end
end
