module IdCardPrinterService
    class SampleDataFormatter

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

        def call
            @base_card = IdCard::PrintData.new(sample: true)

            set_employer_fields()
            set_sample_fields()
            set_rx_fields()
            set_network_fields()
            set_dependent_fields()
            sample_cards = set_plan_fields()
            sample_cards.each(&:save!)

        end

        private

        def set_employer_fields
            selected_attributes = {
                employer_name: @card_setup.print_name,
                group_number: @employer.group_number.present? ? @employer.group_number : "999999",
                medical_eff_date: @employer.effective_date
            }

            @base_card.assign_attributes(selected_attributes)
        end

        def set_plan_fields
            plans_base_cards = []
            @card_setup.plans.each do |plan|
                @base_card.sample_key = @card_setup.print_name.titleize.split.map(&:first).push(plan.id).join
                plan_base_card = @base_card.dup
                plan_name = plan.title.split(/(?<=\d[kK])/).first
                plan.plan_benefits.each do |bene|
                    plan_base_card["benefit_desc_#{bene.sequence}".to_sym] = bene.benefit_desc
                    plan_base_card["benefit_#{bene.sequence}".to_sym] = bene.benefit
                end
                plan_base_card.sample_plan_title = plan_name
                plans_base_cards.push(plan_base_card)
            end
            plans_base_cards
        end

        def set_sample_fields
            selected_attributes = {
                full_name: "JANE DOE",
                primary_mb_member_key: "888888",
                rx_group: @employer.group_number.present? ? @employer.group_number : "999999"
            }

            @base_card.assign_attributes(selected_attributes)
        end

        def set_network_fields
            selected_attributes = @card_setup.provider_section.attributes.with_indifferent_access.slice(
                    :provider_line_1, :provider_line_2, :provider_line_3, :provider_line_4, :provider_line_5, :provider_line_6,
                    :provider_line_7, :provider_line_8, :provider_line_9, :provider_line_10, :provider_line_11, :provider_line_12,
                    :claim_to_1, :claim_to_2, :claim_to_3, :claim_to_4, :claim_to_5, :claim_to_6,
                    :claim_to_7, :claim_to_8, :claim_to_9, :claim_to_10, :claim_to_11, :claim_to_12
                )
            # if @card_setup.network_provider == "Cigna"
            #     @base_card.provider_code = "5"
            # end
            @base_card.network_provider = @card_setup.network_provider
            
            @base_card.assign_attributes(selected_attributes)
        end

        def set_rx_fields
            # fairos_information = Vhcs::HlrxCrosRef.where(pl_plan_key: 52).first
            selected_attributes = @card_setup.rx_section.attributes.with_indifferent_access.slice(
                    :customer_service, 
                    :web_url
                )

            @base_card.assign_attributes(selected_attributes)
        end

        def set_dependent_fields
            @base_card.dependent_1 = "John Doe"
            # @base_card.dependent_2 = "Molly Doe"
            # @base_card.dependent_3 = "Jonathan Doe"
            # @base_card.dependent_4 = "Calvin Doe"
            # @base_card.dependent_5 = "Richard Doe"
            # @base_card.dependent_6 = "Jannet Doe"
            # @base_card.dependent_7 = "Longername Doe"
            # @base_card.dependent_8 = "Robbert Doe"
        end
    end
end