module EmployerCards
    class DataFormatter

        def initialize(employer)
            @employer = employer
        end

        def call
            @members = @employer.members.order(:name)
            @employer_cards = []

            init_cards_and_set_member_fields
            set_plan_fields
            set_common_fields

            @employer_cards.each(&:save!)

        end

        private

        def init_cards_and_set_common_fields
            employer_attributes = {
                employer_name: @employer.id_card_display_name,
                group_number: @employer.group_number,
                rx_group: @employer.rx_group_number
            }

            rx_attributes = @employer.card_rx.attributes.with_indifferent_access.slice(
                :customer_service, 
                :web_url
            )

            provider_attributes = @employer.card_provider.attributes.with_indifferent_access.slice(
                :provider_code, :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
            )

            selected_attributes = employer_attributes.merge(rx_attributes).merge(provider_attributes)
            @employer_cards.each do |card|
                card.assign_attributes(selected_attributes)
            end
        end

        def set_common_fields
            employer_attributes = {
                employer_name: @employer.id_card_display_name,
                group_number: @employer.group_number,
                rx_group: @employer.rx_group_number
            }

            rx_attributes = @employer.card_rx.attributes.with_indifferent_access.slice(
                :customer_service, 
                :web_url
            )

            provider_attributes = @employer.card_provider.attributes.with_indifferent_access.slice(
                :provider_code, :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
            )

            selected_attributes = employer_attributes.merge(rx_attributes).merge(provider_attributes)
            @employer_cards.each do |card|
                card.assign_attributes(selected_attributes)
            end
        end

        def set_plan_fields
            @employer.plans.each do |plan|
                selected_attributes = {}
                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
                @employer_cards.find_all { |card| card.plan_id == plan.id.to_s }.each do |card|
                    card.assign_attributes(selected_attributes)
                end
            end
        end

        def init_cards_and_set_member_fields
            @group_dependents = Vhcs::VwmbMember.where(pl_plan_key: @employer.pl_plan_key)
            @members.each do |me|
                effect_date = determine_eff_date(me)
                if effect_date
                    member_card = SampleIdCard.new()
                    member_attributes = {
                        full_name: me.id_card_display_name,
                        name: me.name,
                        primary_mb_member_key: me.pb_entity_key,
                        family_id: me.family_id,
                        plan_id: me.plan_id,
                        medical_eff_date: effect_date.strftime("%m/%d/%Y")
                    }

                    dependent_attributes = get_dependent_fields(me)

                    selected_attributes = member_attributes.merge(dependent_attributes)
                    member_card.assign_attributes(selected_attributes)
                    @employer_cards.push(member_card)

                    if dependent_attributes.present?
                        @employer_cards.push(member_card)
                    end
                end
            end
        end

        # def set_network_fields
        #     selected_attributes = @employer.card_provider.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
        #     )
        #     @employer_cards.all do |card|
        #         card.assign_attributes(selected_attributes)
        #     end
        # end

        # def set_rx_fields
        #     # fairos_information = Vhcs::HlrxCrosRef.where(pl_plan_key: 52).first
        #     selected_attributes = @employer.card_rx.attributes.with_indifferent_access.slice(
        #         :customer_service, 
        #         :web_url
        #     )
        #     @employer_cards.all do |card|
        #         card.assign_attributes(selected_attributes)
        #     end
        # end

        def determine_eff_date(member)

            participation = Vhcs::PbProductParticipation.joins('INNER JOIN "PBCoveredEntities" ON "PBProductParticipation"."PBProductParticipationKey" = "PBCoveredEntities"."PBProductParticipationKey"').where('"PBCoveredEntities"."PBEntityKey" = ?', member.pb_entity_key).last
            in_effect = participation.in_effect
            out_of_effect = participation.out_of_effect

            if in_effect <= (Date.today + 90.days) && (out_of_effect - 1.day) > Date.today && out_of_effect > in_effect
                in_effect
            else
                false
            end

        end

        def get_dependent_fields(member)
            dependent_attributes = {}
            dependents = @group_dependents.where(family_id: member.family_id).where.not(pb_entity_key: member.pb_entity_key)
            dependents.each do |dep|
                dependent_name = dep.first_name + ' ' + dep.last_name
                dependent_attributes["dependent_#{dep.sequence_number - 1}".to_sym] = dependent_name
            end
            dependent_attributes
        end
    end
end