module BenefitsWordDocService
    class MapPlansInformation

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

        def call
            search_fields = mapping_hash.keys.to_set
            plans_indexes = @word_doc_section.each_index.select { |index| @word_doc_section[index].match?(/\d*\.?\d+k/i) }

            plans_indexes.each do |plan_index|
                new_plan = @card_setup.plans.build(title: @word_doc_section[plan_index])
                plan_lines = @word_doc_section.slice(plan_index + 1, 14)
                plan_lines.each_with_index do |line, i|
                    field_mapping = mapping_array[i]
                    if line.match(/(?<=:).+/)
                        field_value = line.match(/(?<=:).+/)[0].strip
                    else
                        field_regex = field_mapping[:doc_to_employer_regex]
                        if line.match(field_regex)
                            field_value = line.match(field_regex)[0].strip
                        elsif line.match(default_benefit_regex(field_mapping[:employer_benefit_desc_field]))
                            field_value = line.match(default_benefit_regex(field_mapping[:employer_benefit_desc_field]))[0].strip
                        else
                            field_value = line
                        end
                    end
                    employer_benefit_desc_field = field_mapping[:employer_benefit_desc_field]
                    new_benefit = new_plan.plan_benefits[i]
                    new_benefit.benefit = field_value


                    # matching_field = search_fields.detect { |field| line.include?(field) }
                    # if matching_field
                    #     field_mapping = mapping_hash[matching_field]
                    #     field_regex = field_mapping[:doc_to_employer_regex]
                    #     if line.match(field_regex)
                    #         field_value = line.match(field_regex)[0].strip
                    #     elsif line.match(default_benefit_regex(field_mapping[:employer_benefit_desc_field]))
                    #         field_value = line.match(default_benefit_regex(field_mapping[:employer_benefit_desc_field]))[0].strip
                    #     else
                    #         field_value = ""
                    #     end
                    #     employer_benefit_desc_field = field_mapping[:employer_benefit_desc_field]
                    #     new_benefit = new_plan.plan_benefits.find_by(benefit_desc: employer_benefit_desc_field)
                    #     new_benefit.benefit = field_value
                    #     new_benefit.save
                    # end
                end
            end
            @employer
        end

        private

        def mapping_array
            [
                {
                    employer_benefit_sequence: 1,
                    employer_benefit_desc_field: 'Primary Visit',
                    doc_field_desc: 'Follows pattern - Physician Visit$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                {
                    employer_benefit_sequence: 2,
                    employer_benefit_desc_field: 'Specialist Visit',
                    doc_field_desc: 'Follows pattern - Specialist Visit$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                {
                    employer_benefit_sequence: 3,
                    employer_benefit_desc_field: 'Urgent Care',
                    doc_field_desc: 'Follows pattern - Urgent Care$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                {
                    employer_benefit_sequence: 4,
                    employer_benefit_desc_field: 'INN–Ind Ded',
                    doc_field_desc: 'Follows pattern - Individual Deductible (in network )$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                {
                    employer_benefit_sequence: 5,
                    employer_benefit_desc_field: 'INN–Family Ded',
                    doc_field_desc: 'Follows pattern - Family Deductible(in network )$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                {
                    employer_benefit_sequence: 6,
                    employer_benefit_desc_field: 'OON–Ind Ded',
                    doc_field_desc: 'Follows pattern - Individual Deductible (out of network)$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                {
                    employer_benefit_sequence: 7,
                    employer_benefit_desc_field: 'OON–Family Ded',
                    doc_field_desc: 'Follows pattern - Family Deductible (out of network)$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                {
                    employer_benefit_sequence: 8,
                    employer_benefit_desc_field: 'Co-Insurance',
                    doc_field_desc: 'Follows pattern - Co-Insurance70%/30%',
                    doc_to_employer_regex: /(?<=Co-Insurance).*/,
                    regex_desc: 'Grabs everything after field name'
                },
                {
                    employer_benefit_sequence: 9,
                    employer_benefit_desc_field: 'INN–Ind OOP',
                    doc_field_desc: 'Follows pattern - Out-of-Pocket(in network)$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                {
                    employer_benefit_sequence: 10,
                    employer_benefit_desc_field: 'INN–Family OOP',
                    doc_field_desc: 'Follows pattern - Out-of-Pocket Family(in network)$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                {
                    employer_benefit_sequence: 11,
                    employer_benefit_desc_field: 'OON–Ind OOP',
                    doc_field_desc: 'Follows pattern - Out-of-Pocket(out of network)$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                {
                    employer_benefit_sequence: 12,
                    employer_benefit_desc_field: 'OON–Family OOP',
                    doc_field_desc: 'Follows pattern - Out-of-Pocket Family (out of network)$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                {
                    employer_benefit_sequence: 13,
                    employer_benefit_desc_field: 'Emergency Room',
                    doc_field_desc: 'Follows pattern - Emergency RoomXxxxx',
                    doc_to_employer_regex: /(?<=Emergency Room).*/,
                    regex_desc: 'Grabs everything after field name'
                },
                {
                    employer_benefit_sequence: 14,
                    employer_benefit_desc_field: 'Preventive Care',
                    doc_field_desc: 'Follows pattern - Preventive Care100%',
                    doc_to_employer_regex: /(?<=Preventive Care).*/,
                    regex_desc: 'Grabs everything after field name'
                }
            ]
        end

        def mapping_hash
            {
                'Physician Visit' => {
                    employer_benefit_sequence: 1,
                    employer_benefit_desc_field: 'Primary Visit',
                    doc_field_desc: 'Follows pattern - Physician Visit$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                'Specialist Visit' => {
                    employer_benefit_sequence: 2,
                    employer_benefit_desc_field: 'Specialist Visit',
                    doc_field_desc: 'Follows pattern - Specialist Visit$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                'Urgent Care' => {
                    employer_benefit_sequence: 3,
                    employer_benefit_desc_field: 'Urgent Care',
                    doc_field_desc: 'Follows pattern - Urgent Care$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                'Individual Deductible' => {
                    employer_benefit_sequence: 4,
                    employer_benefit_desc_field: 'INN–Ind Ded',
                    doc_field_desc: 'Follows pattern - Individual Deductible (in network )$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                'Family Deductible' => {
                    employer_benefit_sequence: 5,
                    employer_benefit_desc_field: 'INN–Family Ded',
                    doc_field_desc: 'Follows pattern - Family Deductible(in network )$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                'Individual Deductible' => {
                    employer_benefit_sequence: 6,
                    employer_benefit_desc_field: 'OON–Ind Ded',
                    doc_field_desc: 'Follows pattern - Individual Deductible (out of network)$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                'Family Deductible' => {
                    employer_benefit_sequence: 7,
                    employer_benefit_desc_field: 'OON–Family Ded',
                    doc_field_desc: 'Follows pattern - Family Deductible (out of network)$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                'Co-Insurance' => {
                    employer_benefit_sequence: 8,
                    employer_benefit_desc_field: 'Co-Insurance',
                    doc_field_desc: 'Follows pattern - Co-Insurance70%/30%',
                    doc_to_employer_regex: /(?<=Co-Insurance).*/,
                    regex_desc: 'Grabs everything after field name'
                },
                'Out-of-Pocket' => {
                    employer_benefit_sequence: 9,
                    employer_benefit_desc_field: 'INN–Ind OOP',
                    doc_field_desc: 'Follows pattern - Out-of-Pocket(in network)$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                'Out-of-Pocket Family' => {
                    employer_benefit_sequence: 10,
                    employer_benefit_desc_field: 'INN–Family OOP',
                    doc_field_desc: 'Follows pattern - Out-of-Pocket Family(in network)$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                'Out-of-Pocket' => {
                    employer_benefit_sequence: 11,
                    employer_benefit_desc_field: 'OON–Ind OOP',
                    doc_field_desc: 'Follows pattern - Out-of-Pocket(out of network)$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                'Out-of-Pocket Family' => {
                    employer_benefit_sequence: 12,
                    employer_benefit_desc_field: 'OON–Family OOP',
                    doc_field_desc: 'Follows pattern - Out-of-Pocket Family (out of network)$x,xxx',
                    doc_to_employer_regex: /\$.*/,
                    regex_desc: 'Grabs dollar sign and everything after'
                },
                'Emergency Room' => {
                    employer_benefit_sequence: 13,
                    employer_benefit_desc_field: 'Emergency Room',
                    doc_field_desc: 'Follows pattern - Emergency RoomXxxxx',
                    doc_to_employer_regex: /(?<=Emergency Room).*/,
                    regex_desc: 'Grabs everything after field name'
                },
                'Preventive Care' => {
                    employer_benefit_sequence: 14,
                    employer_benefit_desc_field: 'Preventive Care',
                    doc_field_desc: 'Follows pattern - Preventive Care100%',
                    doc_to_employer_regex: /(?<=Preventive Care).*/,
                    regex_desc: 'Grabs everything after field name'
                },
            }
        end

        def default_benefit_regex(field)
            /(?<=#{field}).*/
        end

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

        def is_date?(string)
            true if Date.parse rescue false
        end

    end
end