module SampleCard
    class DataFormatter

        def initialize(employer)
            @employer = employer
        end

        def call
            @sample_card = SampleIdCard.new()

            set_employer_fields()
            set_generic_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: @employer.name,
                group_number: @employer.group_number.present? ? @employer.group_number : "999999",
                medical_eff_date: @employer.effective_date
            }

            @sample_card.assign_attributes(selected_attributes)
        end

        def set_plan_fields
            plans_sample_cards = []
            @employer.id_card_setup.plans.each do |plan|
                plan_sample_card = @sample_card.dup
                plan_name = plan.title.split(/(?<=\d[kK])/).first
                plan_sample_card.family_id = plan_name
                plan.plan_benefits.each do |bene|
                    plan_sample_card["benefit_desc_#{bene.sequence}".to_sym] = bene.benefit_desc
                    plan_sample_card["benefit_#{bene.sequence}".to_sym] = bene.benefit
                end
                plans_sample_cards.push(plan_sample_card)
            end
            plans_sample_cards
        end

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

            @sample_card.assign_attributes(selected_attributes)
        end

        def set_network_fields
            selected_attributes = @employer.id_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 @employer.network_provider == "Cigna"
                @sample_card.provider_code = "5"
            end
            
            @sample_card.assign_attributes(selected_attributes)
        end

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

            @sample_card.assign_attributes(selected_attributes)
        end

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