module BenefitsWordDocService
    class MapEmployerLogo

        def initialize(employer, word_doc)
            @employer = employer
            @card_setup = @employer.id_card_setup
            @word_doc = word_doc
        end

        def call
            Zip::File.open(@word_doc) do |zip_file|
                media_files = zip_file.select { |entry| entry.name.start_with?('word/media/') && !entry.directory? }

                if media_files.length > 1
                    logo = media_files.last
                    file_extension = File.extname(logo.name)
                    meme_type = Marcel::MimeType.for(logo.get_input_stream)
                    image_binary = logo.get_input_stream.read
                    # image_binary = File.binread(logo.get_input_stream.read)

                    filename = @employer.name_to_logo_filename(file_extension)

                    logo = IdCard::EmployerLogo.find_or_create_by(filename: filename) do |clf|
                        clf.image_data = image_binary
                        clf.content_type = meme_type
                    end
                    
                    # new_logo = CardLogoFile.create!(
                    #     filename: filename,
                    #     image_data: image_binary,
                    #     content_type: meme_type,
                    #     logo_type: "employer"
                    # )

                    # image_io = StringIO.new(image_binary)
                    # width, height = FastImage.size(image_io)
                    # image_ratio = width.to_f / height
                    # if (0.8..1.2).cover?(image_ratio)
                    #     @employer.single_card_template = "FairosRxIDCard-Half"
                    # else
                    #     @employer.single_card_template = "FairosRxIDCard"
                    # end

                    @card_setup.employer_logo = logo
                end
            end
            @employer
        end

        # def call
        #     extracted_images = []
        #     Zip::File.open(@word_doc) do |zip_file|
        #         zip_file.each do |entry|
        #             if entry.name.start_with?('word/media/') && !entry.directory?
        #                 file_extension = File.extname(entry.name)
        #                 image_data = entry.get_input_stream.read
        #                 extracted_images << { file_extension: file_extension, data: image_data }
        #             end
        #         end
        #     end
        #     if extracted_images.length > 1
        #         logo = extracted_images.last
        #         filename = @employer.employer_name_to_logo_filename(logo[:file_extension])
        #         employer_logo_binary = logo[:data]
                
        #         new_logo = @employer.card_logo_files.create(
        #             filename: filename,
        #             image: employer_logo_binary,
        #             logo_type: "employer"
        #         )

        #         @employer.employer_logo = new_logo.filename
        #         # same file logic
        #     end
        #     @employer.save
        # end
    end
end
