<template>
    <Form
        ref="theForm"
        @submit="onSubmit"
        @invalidSubmit="onInvalidSubmit">
        <div class="fade-on-route-transition provider-preference">
            <div class="justify-content-center">
                <siteHeader
                    class="mt-4 header"
                    cmsWidgetName="SiteHeaderWidget" />
            </div>
            <div class="iss-heritage-container-width">
                <div class="provider-preference-container iss-heritage-content-container-width">
                    <siteSubHeader
                        id="sub-header"
                        cmsWidgetName="SiteSubHeader"
                        class="mb-0 mt-4 text-center" />
                    <div
                        class="mt-4 mb-5 strong-text-override"
                        v-html="scheduleWithSafeliteText"></div>
                    <buttonMain
                        ref="buttonMain"
                        class="full-width-button"
                        variant="navigation"
                        buttonText="Schedule now"
                        @clickEvent="scheduleWithSafelite" />
                    <div
                        class="mt-5 mb-5"
                        v-html="scheduleWithOtherText"></div>
                    <textLink
                        class="underlined-text"
                        linkType="text"
                        text="Find another shop"    
                        href="#"
                        @clickEvent="findAnotherShopClicked" />
                    <siteFooter
                        :ref="SITE_FOOTER_REF_NAME"
                        class="mt-6"
                        cmsWidgetName="SiteFooterWidget"
                        :isForwardButtonHidden="true"
                        @backClicked="navigateBack" />
                </div>
            </div>
        </div>
        <steeringModal
            :ref="STEERING_MODAL_REF_NAME"
            cmsWidgetName="StateSteeringModal" />
        <tpaRecalModal
            :ref="TPA_RECAL_MODAL_REF_NAME"
            cmsWidgetName="TPARecalModal"
            buttonCmsWidgetName="TPARecalQuestion"
            :ackError="ackError"
            :noSelectionError="noSelectionError"
            @buttonClick="navigateWithTPARecalAnswer"/>
    </Form>
</template>
<script>
// Import Supporting Files
import {
    fetchCmsContentForPage,
    setupModalLinks
} from '@/helpers/cms-content-helper';
import settleAllPromises from '@/helpers/layout-helper';
import errorMessages from '@/constants/error-messages';
import buttonQuestion from '@/digital-components/button-question/button-question.vue';
import showIssLoadingModal from '@/helpers/loading-modal-helper';
import bailoutMessage from '@/constants/bailoutMessage';
import baseFormMixin from '@/mixins/base-form-mixin';
import PROVIDER_PREFERENCE_OPTIONS from '@/constants/provider-preference';

// Import Component
import { Form } from 'vee-validate';
import siteFooter from '@/iss-components/site-footer/site-footer.vue';
import siteHeader from '@/iss-components/site-header/site-header.vue';
import siteSubHeader from '@/iss-components/site-sub-header/site-sub-header.vue';
import steeringModal from '@/layouts/provider-preference/steering-modal/steering-modal.vue';
import tpaRecalModal from '@/layouts/provider-preference/tpa-recal-modal/tpa-recal-modal.vue';
import textLink from '@/ux-components/text-link/text-link.vue';
import buttonMain from '@/ux-components/button-main/button-main.vue';

const STEERING_MODAL_REF_NAME = 'StateSteeringModal';
const TPA_RECAL_MODAL_REF_NAME = 'TPARecalModal';
const SITE_FOOTER_REF_NAME = 'siteFooter';

export default {
    name: 'provider-preference',
    components: {
        siteFooter,
        siteHeader,
        siteSubHeader,
        // eslint-disable-next-line vue/no-reserved-component-names
        Form,
        buttonQuestion,
        buttonMain,
        steeringModal,
        tpaRecalModal,
        textLink
    },
    mixins: [baseFormMixin],
    async beforeRouteEnter(to, from, next) {
        const cmsContentPromise = fetchCmsContentForPage(to.query.issPage);
        // Settle promises and get results
        const promiseResultMap = [
            {
                resultKey: 'cmsContent',
                promise: cmsContentPromise
            }
        ];
        const resultMap = await settleAllPromises(promiseResultMap);
        next((vm) => {
            vm.setCmsContent(resultMap.cmsContent);
            // open steering modal if it has body text, the state is defined in the CMS content and doesn't
            //   populate if the state is not listed in the CMS content
            if (!!vm.$refs[STEERING_MODAL_REF_NAME].ModalBodyText) {
                vm.openStateSteeringModal();
            }
        });
    },
    data() {
        return {
            providerPreferenceOptions: PROVIDER_PREFERENCE_OPTIONS,
            STEERING_MODAL_REF_NAME,
            TPA_RECAL_MODAL_REF_NAME,
            SITE_FOOTER_REF_NAME
        };
    },
    computed: {
        ackError() {
            return errorMessages.ACKNOWLEDGEMENT_REQUIRED;
        },
        noSelectionError() {
            return errorMessages.NO_SELECTION_REQUIRED;
        },
        scheduleWithSafeliteText() {
            return this.getCmsContent('ScheduleWithSafeliteText', 'BodyText');
        },
        scheduleWithOtherText() {
            return this.getCmsContent('ScheduleWithOtherText', 'BodyText');
        }
    },
    mounted() {
        setupModalLinks(this);
    },
    methods: {
        getHeaderTextFromCms(cmsWidgetName) {
            const headerText = this.getCmsContent(cmsWidgetName, 'HeaderText');
            return headerText?.replace(
                '{custom:SafeliteLogo}',
                "<span class='safeliteLogo'></span>"
            );
        },
        getSubheaderTextFromCms(cmsWidgetName) {
            return this.getCmsContent(cmsWidgetName, 'SubheaderText');
        },
        getBodyTextFromCms(cmsWidgetName) {
            return this.getCmsContent(cmsWidgetName, 'BodyText');
        },
        arePagePrerequisiteValid() {
            return true;
        },
        navigateForward(scenario) {
            showIssLoadingModal(true);
            this.$router.navigate(scenario, this.$route);
        },
        navigateWithTPARecalAnswer(answer) {
            if (answer === this.providerPreferenceOptions.TPA) {
                this.scheduleWithTPA();
            } else {
                this.scheduleWithSafelite();
            }
        },
        scheduleWithSafelite() {
            this.mainStore.updateIsSafeliteProvider(true);
            const scenario = this.navigationScenarios.CLICKED_FORWARD_WITH_SAFELITE;
            this.navigateForward(scenario);
        },
        scheduleWithTPA() {
            this.mainStore.updateIsSafeliteProvider(false);
            const scenario = this.navigationScenarios.CLICKED_FORWARD_WITH_TPA_ENABLED;
            this.navigateForward(scenario);
        },
        findAnotherShopClicked() {
            if (this.mainStore.issConfig.enableTPAFlow) {
                if (this.mainStore.hasRecalibrationPart) {
                    this.$refs[TPA_RECAL_MODAL_REF_NAME].openModal();
                    return;
                } else {
                    this.scheduleWithTPA();
                }
            } else {
                this.mainStore.setBailout(bailoutMessage.TPANotEnabled());
                const scenario = this.navigationScenarios.CLICKED_FORWARD_WITH_TPA_DISABLED;
                this.navigateForward(scenario);
            }
        },
        openStateSteeringModal() {
            this.$refs[STEERING_MODAL_REF_NAME].openModal();
        }
    }
};
</script>
<style lang="scss" scoped>
.iss-heritage-container-width {
    .provider-preference-container {
        position: relative;
        min-height: 1px;
        padding-left: .9375rem;
        padding-right: .9375rem;
    }
}

.provider-preference {
    :deep(.strong-text-override) {
        strong {
            font-weight: $font-weight-bold;
            color: $black;
        }
    }
    #sub-header span {
        color: $black;
    }
    .full-width-button {
        width: 100%;
    }
    .underlined-text {
        text-decoration: underline;
    }
    :deep(.safeliteLogo) {
        background-image: url(~@/assets/img/icons/logo.svg);
        background-repeat: no-repeat;
        background-size: 4.5rem;
        margin: 0 0.25rem 0 0.25rem;
        padding: 0 2.5rem 0 2.5rem;
        background-position: center;
    }
}
</style>
