module IdCardPrinterService
    class SampleDataFormatter

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

        def call

            base_card = initialize_sample_base_card()
            create_plan_base_cards(base_card)

        end

        private

        def initialize_sample_base_card
            sample_member_attributes = {
                full_name: "JANE DOE",
                primary_mb_member_key: "888888"
            }

            employer_attributes = {
                employer_name: @card_setup.print_name,
                employer_id: @employer.id,
                group_number: @employer.group_number.present? ? @employer.group_number : "999999",
                rx_group: @employer.group_number.present? ? @employer.group_number : "999999",
                network_provider: @card_setup.network_provider,
                network_logo_filename: @card_setup.network_logo.filename,
                employer_logo_filename: @card_setup.employer_logo.filename,
                medical_eff_date: @employer.effective_date
            }

            rx_attributes = @card_setup.rx_section.attributes.with_indifferent_access.slice(
                :customer_service, 
                :web_url
            )

            provider_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,
                :precert_1, :precert_2, :precert_3, :precert_4
            )

            selected_attributes = sample_member_attributes.merge(employer_attributes).merge(rx_attributes).merge(provider_attributes)
            IdCard::PrintData.new(selected_attributes)
        end

        def create_plan_base_cards(common_fields_card)
            plans = @card_setup.plans
            plans.each do |plan|
                selected_attributes = { 
                    plan_id: plan.id,
                    sample_plan_title: plan.title
                }
                plan.plan_benefits.each do |bene|
                    selected_attributes["benefit_desc_#{bene.sequence}".to_sym] = bene.benefit_desc
                    selected_attributes["benefit_#{bene.sequence}".to_sym] = bene.benefit
                end

                plan_base_card = common_fields_card.dup
                plan_base_card.assign_attributes(selected_attributes)
                plan_base_card.save
            end
        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