class WordDocProcessorOld

    def initialize(word_doc, process=nil)
        @word_doc = word_doc
        if process
            @process = process
        else
            @process = EmployerSetupProcess.new
        end
    end

    def call
        doc = Docx::Document.open(@word_doc)

        index = 40

        general_information = doc.paragraphs.slice(0, index)
        plan_information = doc.paragraphs.slice(index + 1..)


        general_fields(general_information)
        plans(plan_information)
        employer_logo()

        @process

    end

    private

    def general_fields(general_information)
        fields = ['Group Number:', 'Employer Name:', 'Group Effective Date:']
        general_information.each do |gi|
            field_string = gi.to_s
            if field_string.include?('Group Number:')
                value = field_string.delete_prefix('Group Number:').strip
                if is_number?(value)
                    @process.group_number = value
                end
            elsif field_string.include?('Employer Name:')
                value = field_string.delete_prefix('Employer Name:').strip
                @process.name = value
            elsif field_string.include?('Group Effective Date:')
                value = field_string.delete_prefix('Group Effective Date:').strip
                @process.effective_date = value
            end
        end
    end

    def employer_logo
        extracted_images = []
        Zip::File.open(@word_doc) do |zip_file|
            zip_file.each do |entry|
                if entry.name.start_with?('word/media/') && !entry.directory?
                    filename = File.basename(entry.name)
                    image_data = entry.get_input_stream.read
                    extracted_images << { filename: filename, data: image_data }
                end
            end
        end
        if extracted_images.length > 1
            @process.employer_logo = @process.name.titleize.gsub(/\s+/, '').concat("Logo.png")
            # same file logic
        end
    end

    def plans(plan_information)
        plans_array = []
        plan_information.each_with_index do |pin, i|
            if pin.to_s.match?(/\d*\.?\d+k/i)
                new_plan = {title: pin.to_s.strip, start_index: i, benefits: []}
                plans_array.push(new_plan)
            end
        end

        plans_array.each do |pl|
            new_plan = Plan.create(title: pl[:title], employer_setup_process: @process)
            plan_fields = plan_information.slice(pl[:start_index] + 1, 14)
            plan_fields.each_with_index do |pf, i|
                field_string = pf.to_s
                if field_string.strip.length > 1
                    case field_string
                    when ->(string) { string.include?("Physician Visit") }
                        value = field_string.delete_prefix("Physician Visit")
                        new_benefit = new_plan.plan_benefits.find_by(benefit_desc: "Primary Visit")
                        new_benefit.benefit = value
                        new_benefit.save
                    when ->(string) { string.include?("Specialist Visit") }
                        value = field_string.delete_prefix("Specialist Visit")
                        new_benefit = new_plan.plan_benefits.find_by(benefit_desc: "Specialist Visit")
                        new_benefit.benefit = value
                        new_benefit.save
                    when ->(string) { string.include?("Urgent Care") }
                        value = field_string.delete_prefix("Urgent Care")
                        new_benefit = new_plan.plan_benefits.find_by(benefit_desc: "Urgent Care")
                        new_benefit.benefit = value
                        new_benefit.save
                    when ->(string) { string.include?("Individual Deductible (in network )") }
                        value = field_string.delete_prefix("Individual Deductible (in network )")
                        new_benefit = new_plan.plan_benefits.find_by(benefit_desc: "INN–Ind Ded")
                        new_benefit.benefit = value
                        new_benefit.save
                    when ->(string) { string.include?("Family Deductible(in network )") }
                        value = field_string.delete_prefix("Family Deductible(in network )")
                        new_benefit = new_plan.plan_benefits.find_by(benefit_desc: "INN–Family Ded")
                        new_benefit.benefit = value
                        new_benefit.save
                    when ->(string) { string.include?("Individual Deductible (out of network)") }
                        value = field_string.delete_prefix("Individual Deductible (out of network)")
                        new_benefit = new_plan.plan_benefits.find_by(benefit_desc: "OON–Ind Ded")
                        new_benefit.benefit = value
                        new_benefit.save
                    when ->(string) { string.include?("Family Deductible      (out of network) ") }
                        value = field_string.delete_prefix("Family Deductible      (out of network) ")
                        new_benefit = new_plan.plan_benefits.find_by(benefit_desc: "OON–Family Ded")
                        new_benefit.benefit = value
                        new_benefit.save
                    when ->(string) { string.include?("Co-Insurance") }
                        value = field_string.delete_prefix("Co-Insurance")
                        new_benefit = new_plan.plan_benefits.find_by(benefit_desc: "Co-Insurance")
                        new_benefit.benefit = value
                        new_benefit.save
                    when ->(string) { string.include?("Out-of-Pocket(in network) ") }
                        value = field_string.delete_prefix("Out-of-Pocket(in network) ")
                        new_benefit = new_plan.plan_benefits.find_by(benefit_desc: "INN–Ind OOP")
                        new_benefit.benefit = value
                        new_benefit.save
                    when ->(string) { string.include?("Out-of-Pocket Family(in network) ") }
                        value = field_string.delete_prefix("Out-of-Pocket Family(in network) ")
                        new_benefit = new_plan.plan_benefits.find_by(benefit_desc: "INN–Family OOP")
                        new_benefit.benefit = value
                        new_benefit.save
                    when ->(string) { string.include?("Out-of-Pocket(out of network) ") }
                        value = field_string.delete_prefix("Out-of-Pocket(out of network) ")
                        new_benefit = new_plan.plan_benefits.find_by(benefit_desc: "OON–Ind OOP")
                        new_benefit.benefit = value
                        new_benefit.save
                    when ->(string) { string.include?("Out-of-Pocket Family (out of network) ") }
                        value = field_string.delete_prefix("Out-of-Pocket Family (out of network) ")
                        new_benefit = new_plan.plan_benefits.find_by(benefit_desc: "OON–Family OOP")
                        new_benefit.benefit = value
                        new_benefit.save
                    when ->(string) { string.include?("Emergency Room ") }
                        value = field_string.delete_prefix("Emergency Room ")
                        new_benefit = new_plan.plan_benefits.find_by(benefit_desc: "Emergency Room")
                        new_benefit.benefit = value
                        new_benefit.save
                    when ->(string) { string.include?("Preventive Care") }
                        value = field_string.delete_prefix("Preventive Care")
                        new_benefit = new_plan.plan_benefits.find_by(benefit_desc: "Preventive Care")
                        new_benefit.benefit = value
                        new_benefit.save
                    end
                end
            end
        end
    end

    def is_number?(string)
        true if Float(string) rescue false
    end
    
end