<template>
    <Form
        ref="theForm"
        v-slot="{ meta }"
        @submit="onSubmit"
        @invalidSubmit="onInvalidSubmit">
        <div class="fade-on-route-transition replace-options-question">
            <div class="justify-content-center">
                <siteHeader cmsWidgetName="SiteHeaderWidget" />
            </div>
            <div class="iss-heritage-container-width">
                <div class="vehicle-damage-container iss-heritage-content-container-width">
                    <siteSubHeader
                        cmsWidgetName="SiteSubHeaderWidget"
                        class="mt-4" />
                    <alert
                        v-if="shouldDisplayVehicleChangeAlert"
                        ref="vehicleChangeAlert"
                        class="mt-5 mb-0"
                        cmsWidgetName="VehicleChangeAlert"
                        alertClass="alert-warning"
                        :isDismissible="false" />
                    <div class="justify-content-center">
                        <div class="vehicle-damage-question-container">
                            <damageLocationQuestion
                                ref="damageLocation"
                                v-model="selectedDamageLocations"
                                cmsWidgetName="DamageLocationQuestion"
                                groupName="DamageLocationQuestion" />
                        </div>
                    </div>
                    <div class="justify-content-center">
                        <div class="vehicle-damage-question-container">
                            <windshieldOptions
                                ref="windshieldOptions"
                                v-model="selectedWindshieldOptions"
                                :hasRepairReplaceConflict="hasRepairReplaceConflict"
                                :hasSplitSingleConflict="hasSplitSingleConflict"
                                :selectedDamageLocations="selectedDamageLocations" />
                        </div>
                    </div>
                    <div class="justify-content-center">
                        <div class="vehicle-damage-question-container">
                            <alert
                                v-if="hasRepairReplaceConflict"
                                class="my-5"
                                cmsWidgetName="HasReplacementConflict"
                                alertClass="alert-danger"
                                :isDismissible="false"
                                :isCollapsible="true" />
                        </div>
                    </div>
                    <div class="justify-content-center">
                        <div class="vehicle-damage-question-container">
                            <sideDoorOptions
                                v-show="!hasRepairReplaceConflict"
                                ref="sideDoorOptions"
                                v-model="sideDoorOptionsData"
                                cmsWidgetName="SideDoorSideQuestion"
                                groupName="SideDoorSideQuestion"
                                :selectedDamageLocations="selectedDamageLocations" />
                        </div>
                    </div>
                    <div class="justify-content-center">
                        <div class="vehicle-damage-question-container">
                            <replaceOptionsQuestion
                                ref="backGlassOptions"
                                v-model="selectedRearReplaceOptions"
                                cmsWidgetName="RearReplaceOptionsQuestion"
                                :isAvailable="
                                    isRearWindowDamageLocation &&
                                    !hasRepairReplaceConflict
                                "
                                groupName="BackGlassReplaceOptionsQuestion"
                                validationRules="replace-options-required" />
                        </div>
                    </div>
                    <div class="justify-content-center">
                        <div class="vehicle-damage-question-container">
                            <site-footer
                                ref="siteFooter"
                                class="mt-5"
                                cmsWidgetName="SiteFooterWidget"
                                :isForwardActionDisabled="!meta.valid"
                                @backClicked="navigateBack"
                                @forwardClicked="forwardButtonAction" />
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </Form>
</template>

<script>
// Components
import siteHeader from '@/iss-components/site-header/site-header.vue';
import siteFooter from '@/iss-components/site-footer/site-footer.vue';
import siteSubHeader from '@/iss-components/site-sub-header/site-sub-header.vue';
import sideDoorOptions from '@/layouts/vehicle-damage/side-door-options/side-door-options.vue';
import damageLocationQuestion from '@/layouts/vehicle-damage/damage-location-question/damage-location-question.vue';
import windshieldOptions from '@/layouts/vehicle-damage/windshield-options/windshield-options.vue';
import replaceOptionsQuestion from '@/layouts/vehicle-damage/replace-options-question/replace-options-question.vue';
import alert from '@/ux-components/alert/alert.vue';

// Supporting files
import BaseFormMixin from '@/mixins/base-form-mixin.js';
import vehicleQuestionsMixin from '@/mixins/vehicle-questions-mixin';
import { fetchCmsContentForPage } from '@/helpers/cms-content-helper';
import settleAllPromises from '@/helpers/layout-helper';
import { Form, defineRule } from 'vee-validate';
import { required } from '@/helpers/validation-rules';
import errorMessages from '@/constants/error-messages';
import damageLocationsCms from '@/constants/damage-locations-cms.js';
import damageLocationsSelected from '@/constants/damage-locations-selected.js';
import { useMainStore } from '@/store';
import bailoutMessage from '@/constants/bailoutMessage';

// DEFINE VALIDATION RULES
defineRule(
    'replace-options-required',
    required(errorMessages.REPLACE_OPTIONS_REQUIRED)
);

export default {
    name: 'vehicle-damage',

    components: {
        siteHeader,
        siteFooter,
        siteSubHeader,
        sideDoorOptions,
        damageLocationQuestion,
        windshieldOptions,
        replaceOptionsQuestion,
        Form,
        alert
    },
    mixins: [BaseFormMixin, vehicleQuestionsMixin],
    async beforeRouteEnter(to, from, next) {
        const store = useMainStore();

        // Call APIs
        const cmsContentPromise = fetchCmsContentForPage(to.query.issPage);
        const damageOptionsPromise = store.getDamageOptions(store.order.vehicle.carId);

        // Settle promises and get results
        const promiseResultMap = [
            {
                resultKey: 'cmsContent',
                promise: cmsContentPromise
            },
            {
                resultKey: 'damageOptions',
                promise: damageOptionsPromise
            }
        ];

        const resultMap = await settleAllPromises(promiseResultMap);

        // Call the "next" function to complete the transition to this page.
        next((vm) => {
            vm.setCmsContent(resultMap.cmsContent);
            vm.$refs.damageLocation.initializeComponent(resultMap.damageOptions);
            vm.$refs.sideDoorOptions.initializeComponent(
                resultMap.damageOptions.driverSideOptions
                    .availableReplacementOptions,
                resultMap.damageOptions.passengerSideOptions
                    .availableReplacementOptions
            );
            vm.$refs.windshieldOptions.initializeComponent(resultMap.damageOptions.windshieldOptions
                .availableReplacementOptions);
            vm.$refs.backGlassOptions.initializeComponent(resultMap.damageOptions.backGlassOptions
                .availableReplacementOptions);
        });
    },
    setup() {
        const mainStore = useMainStore();

        return { mainStore };
    },
    data() {
        return {
            selectedDamageLocations: this.getDamageLocationsFromStore(),
            sideDoorOptionsData: {
                selectedDoorSides: this.getDoorSidesFromStore(),
                selectedDriverSideReplaceOptions:
                    this.getDriverSideReplaceOptionsFromStore(),
                selectedPassengerSideReplaceOptions:
                    this.getPassengerSideReplaceOptionsFromStore()
            },
            selectedWindshieldOptions: this.getWindshieldOptionsFromStore(),
            selectedRearReplaceOptions: this.getRearReplaceOptionsFromStore()
        };
    },
    computed: {
        isWindshieldDamageLocation() {
            return this.selectedDamageLocations.some((selectedDamages) =>
                selectedDamages.toUpperCase()
                    === damageLocationsCms.WINDSHIELD);
        },
        isSideDoorDamageLocation() {
            return this.selectedDamageLocations.some((selectedDamages) =>
                selectedDamages.toUpperCase()
                    === damageLocationsCms.SIDEDOOR);
        },
        isRearWindowDamageLocation() {
            return this.selectedDamageLocations.some((selectedDamages) =>
                selectedDamages.toUpperCase()
                    === damageLocationsCms.REARWINDOW);
        },
        isWindshieldRepair() {
            return (
                this.isWindshieldDamageLocation
                && this.selectedWindshieldOptions.selectedWindshieldDamageType
                    === damageLocationsSelected.REPAIR
            );
        },
        isWindshieldReplace() {
            return (
                this.isWindshieldDamageLocation
                && this.selectedWindshieldOptions.selectedWindshieldDamageType
                    === damageLocationsSelected.REPLACE
            );
        },
        isDriverSideReplace() {
            if (!this.isSideDoorDamageLocation) return false;

            return this.sideDoorOptionsData.selectedDoorSides.some((selectedDriverSide) =>
                selectedDriverSide.toUpperCase()
                    === damageLocationsCms.DRIVERSIDE);
        },
        isPassengerSideReplace() {
            if (!this.isSideDoorDamageLocation) return false;

            return this.sideDoorOptionsData.selectedDoorSides.some((selectedPassengerSide) =>
                selectedPassengerSide.toUpperCase()
                    === damageLocationsCms.PASSENGERSIDE);
        },
        hasRepairReplaceConflict() {
            return (
                this.isWindshieldDamageLocation
                && this.selectedDamageLocations.length > 1
                && this.isWindshieldRepair
            );
        },
        hasSplitSingleConflict() {
            if (
                !this.selectedDamageLocations?.includes('Windshield')
                || this.selectedWindshieldOptions.selectedWindshieldDamageType
                    === damageLocationsSelected.REPAIR
                || !this.selectedWindshieldOptions.selectedWindshieldReplaceOptions
            ) return false;

            return (
                this.selectedWindshieldOptions.selectedWindshieldReplaceOptions?.some((selectedSingleWindshield) =>
                    selectedSingleWindshield.toUpperCase()
                        === damageLocationsSelected.SINGLE.toUpperCase())
                && (this.selectedWindshieldOptions.selectedWindshieldReplaceOptions?.some((selectedDriverWindshield) =>
                    selectedDriverWindshield.toUpperCase()
                        === damageLocationsSelected.DRIVER.toUpperCase())
                    || this.selectedWindshieldOptions.selectedWindshieldReplaceOptions?.some((selectedPassengerWindshield) =>
                        selectedPassengerWindshield.toUpperCase()
                            === damageLocationsSelected.PASSENGER.toUpperCase()))
            );
        },
        shouldDisplayVehicleChangeAlert() {
            return this.$router?.options?.history?.state[this.routerParams.DISPLAY_VEHICLE_CHANGE_ALERT] ?? false;
        }
    },
    methods: {
        arePagePrerequisitesValid() {
            if (useMainStore().order.vehicle.carId) {
                return true;
            }
            return false;
        },
        getDamageLocationsFromStore() {
            const glassSelections = [];

            if (
                this.mainStore.order.damage.glassToReplace?.some((glass) =>
                    glass.glassLocation
                        === damageLocationsSelected.WINDSHIELD)
                || this.mainStore.order.damage.isRepair
            ) {
                glassSelections.push(damageLocationsSelected.WINDSHIELD);
            }
            if (
                this.mainStore.order.damage.glassToReplace?.some((glass) =>
                    glass.glassLocation
                            === damageLocationsSelected.DRIVER
                        || glass.glassLocation
                            === damageLocationsSelected.PASSENGER)
            ) {
                glassSelections.push(damageLocationsSelected.SIDEDOOR);
            }

            if (
                this.mainStore.order.damage.glassToReplace?.some((glass) =>
                    glass.glassLocation === damageLocationsSelected.REAR)
            ) {
                glassSelections.push(damageLocationsSelected.REARWINDOW);
            }

            return glassSelections;
        },
        getWindshieldOptionsFromStore() {
            const windShieldOptions = {
                selectedWindshieldDamageType: '',
                selectedWindshieldChipCount: null,
                selectedWindshieldReplaceOptions: []
            };

            if (this.mainStore.order.damage.isRepair === undefined) return windshieldOptions;

            if (this.mainStore.order.damage.isRepair) {
                windShieldOptions.selectedWindshieldDamageType =
                    damageLocationsSelected.REPAIR;
                windShieldOptions.selectedWindshieldChipCount =
                    this.mainStore.order.damage.numberOfChips;
            } else {
                if (
                    this.mainStore.order.damage.glassToReplace?.some((glass) =>
                        glass.glassLocation
                                === damageLocationsSelected.WINDSHIELD
                            && glass.glassName === damageLocationsSelected.SINGLE)
                ) {
                    windShieldOptions.selectedWindshieldDamageType =
                        damageLocationsSelected.REPLACE;
                    windShieldOptions.selectedWindshieldReplaceOptions.push(damageLocationsSelected.SINGLE);
                }

                if (
                    this.mainStore.order.damage.glassToReplace?.some((glass) =>
                        glass.glassLocation
                                === damageLocationsSelected.WINDSHIELD
                            && glass.glassName === damageLocationsSelected.DRIVER)
                ) {
                    windShieldOptions.selectedWindshieldDamageType =
                        damageLocationsSelected.REPLACE;
                    windShieldOptions.selectedWindshieldReplaceOptions.push(damageLocationsSelected.DRIVER);
                }

                if (
                    this.mainStore.order.damage.glassToReplace?.some((glass) =>
                        glass.glassLocation
                                === damageLocationsSelected.WINDSHIELD
                            && glass.glassName
                                === damageLocationsSelected.PASSENGER)
                ) {
                    windShieldOptions.selectedWindshieldDamageType =
                        damageLocationsSelected.REPLACE;
                    windShieldOptions.selectedWindshieldReplaceOptions.push(damageLocationsSelected.PASSENGER);
                }
            }

            return windShieldOptions;
        },
        getDoorSidesFromStore() {
            const doorSides = [];
            if (
                this.mainStore.order.damage.glassToReplace?.some((glass) =>
                    glass.glassLocation === damageLocationsSelected.DRIVER)
            ) {
                doorSides.push(damageLocationsSelected.DRIVERSIDE);
            }

            if (
                this.mainStore.order.damage.glassToReplace?.some((glass) =>
                    glass.glassLocation
                        === damageLocationsSelected.PASSENGER)
            ) {
                doorSides.push(damageLocationsSelected.PASSENGERSIDE);
            }

            return doorSides;
        },
        getDriverSideReplaceOptionsFromStore() {
            const driverSideReplaceOptions = [];

            this.mainStore.order.damage.glassToReplace?.forEach((glass) => {
                if (glass.glassLocation === damageLocationsSelected.DRIVER) {
                    driverSideReplaceOptions.push(glass.glassName);
                }
            });

            return driverSideReplaceOptions;
        },
        getPassengerSideReplaceOptionsFromStore() {
            const passengerSideReplaceOptions = [];

            this.mainStore.order.damage.glassToReplace?.forEach((glass) => {
                if (glass.glassLocation === damageLocationsSelected.PASSENGER) {
                    passengerSideReplaceOptions.push(glass.glassName);
                }
            });

            return passengerSideReplaceOptions;
        },
        getRearReplaceOptionsFromStore() {
            const rearReplaceOptions =
                this.mainStore.order.damage.glassToReplace?.filter((glass) =>
                    glass.glassLocation === damageLocationsSelected.REAR)[0]?.glassName;

            return rearReplaceOptions;
        },
        async forwardButtonAction() {
            this.mainStore.saveVehicleDamage(
                this.isWindshieldRepair,
                this.selectedGlassToReplace(),
                this.selectedWindshieldOptions.selectedWindshieldChipCount
            );

            this.logEvents();

            if (this.isWindshieldRepair) {
                const results = await Promise.allSettled([useMainStore().getSupportingItems(), useMainStore().getRecalParts()]);
                const supportingItems = results[0].value;
                useMainStore().updateSupportingItems(supportingItems.data);
            } else {
                useMainStore().updateSupportingItems([]);
            }

            if (this.mainStore.damage.isRepair) {
                this.$router.navigate(
                    this.navigationScenarios.CLICKED_FORWARD_WITH_REPAIR,
                    this.$route
                );
            } else if (this.mainStore.order.vehicle.vin || !this.isWindshieldReplace) {
                // If vin already exists or not replacing windshield, get parts/questions and navigate forward

                const partsOrQuestionsResponse = await this.getPartsOrQuestions();

                // Comes from vehicleQuestionsMixin.navigateForward()
                await this.navigateForward(
                    partsOrQuestionsResponse.data.partsOrQuestions,
                    this
                );
            } else {
                this.$router.navigate(
                    this.navigationScenarios.CLICKED_FORWARD_WITHOUT_VIN,
                    this.$route
                );
            }

            return null;
        },
        selectedGlassToReplace() {
            const selectedGlassToReplace = [];
            if (this.isWindshieldDamageLocation && !this.isWindshieldRepair) {
                this.selectedWindshieldOptions.selectedWindshieldReplaceOptions.forEach((wsItem) => {
                    selectedGlassToReplace.push({
                        glassLocation: damageLocationsSelected.WINDSHIELD,
                        glassName: wsItem
                    });
                });
            }

            if (this.isDriverSideReplace) {
                this.sideDoorOptionsData.selectedDriverSideReplaceOptions.forEach((driverItem) => {
                    selectedGlassToReplace.push({
                        glassLocation: damageLocationsSelected.DRIVER,
                        glassName: driverItem
                    });
                });
            }

            if (this.isPassengerSideReplace) {
                this.sideDoorOptionsData.selectedPassengerSideReplaceOptions.forEach((passengerItem) => {
                    selectedGlassToReplace.push({
                        glassLocation: damageLocationsSelected.PASSENGER,
                        glassName: passengerItem
                    });
                });
            }

            if (this.isRearWindowDamageLocation) {
                selectedGlassToReplace.push({
                    glassLocation: damageLocationsSelected.REAR,
                    glassName: this.selectedRearReplaceOptions
                });
            }

            return selectedGlassToReplace;
        },
        logEvents() {
            const vehicleString = this.mainStore.order.vehicle.make + '_' + this.mainStore.order.vehicle.model + '_' + this.mainStore.order.vehicle.style;
            this.pushEventToGA("CAR SUBMISSION", this.mainStore.order.vehicle.year?.toString(), vehicleString, true, null, 0);
            this.pushEventToGA('forward_progress', 'continue_clicked', 'vehicle_damage_2', true);

            if (this.isWindshieldRepair) {
                this.pushEventToGA("damage", "selected", "repair", true, null, null);
                this.pushEventToGA("VehicleDamage", "Repair", this.mainStore.damage.numberOfChips?.toString(), true, null, this.mainStore.damage.numberOfChips);
            } else {
                this.pushEventToGA("damage", "selected", "replace", true, null, null);
                this.selectedGlassToReplace().forEach((glass) => {
                    this.logEventForPart(glass);
                });
            }
        },
        logEventForPart(glass) {
            let eventGlassLocation = '';
            let eventGlassName = '';
            switch (glass.glassLocation) {
                case damageLocationsSelected.WINDSHIELD:
                    eventGlassLocation = 'Windshield Replace';
                    break;
                case damageLocationsSelected.DRIVER:
                    eventGlassLocation = "Driver's Side";
                    break;
                case damageLocationsSelected.PASSENGER:
                    eventGlassLocation = "Passenger's Side";
                    break;
                case damageLocationsSelected.REAR:
                    eventGlassLocation = 'Back Glass';
                    break;
                default:
                    eventGlassLocation = glass.glassLocation;
            }

            switch (glass.glassName) {
                // Windshield options
                case damageLocationsSelected.DRIVER:
                    eventGlassName = "Left piece";
                    break;
                case damageLocationsSelected.PASSENGER:
                    eventGlassName = "Right piece";
                    break;
                // Bottom two cases here are for rear options, but they are handled the same as single windshield
                case damageLocationsSelected.SINGLE:
                case damageLocationsSelected.STATIONARY:
                case damageLocationsSelected.SLIDER:
                    eventGlassName = '';
                    break;
                // Side door options
                case damageLocationsSelected.FRONT:
                    eventGlassName = "Front door";
                    break;
                case damageLocationsSelected.BACK:
                    eventGlassName = "Rear door";
                    break;
                case damageLocationsSelected.QUARTER:
                    eventGlassName = "Quarter";
                    break;
                case damageLocationsSelected.VENT:
                    eventGlassName = "Vent";
                    break;
                case damageLocationsSelected.SIDEDOOR:
                    eventGlassName = "Sliding door";
                    break;
                default:
                    eventGlassName = glass.glassName;
            }
            const label = eventGlassName ? `${eventGlassLocation} - ${eventGlassName}` : eventGlassLocation;
            this.pushEventToGA("VehicleDamage", "Replacement", label, true, null, 1);
        }
    }
};
</script>

<style lang="scss" scoped>
.iss-heritage-container-width {
    .vehicle-damage-container {
        position: relative;
        min-height: 1px;
        padding-left: .9375rem;
        padding-right: .9375rem;

        .vehicle-damage-question-container {
            margin-top: 0.625rem; // 10px
        }
    }
}

.replace-options-question {
    .question-text {
        span {
            padding: 1rem 0;
            text-align: center;
        }
    }
    .two-list-card-width {
        width: 100%;
        flex: 0 auto;
    }
}
.button-question,
.windshield-damage-type-question,
.damage-location-question {
    .col {
        margin-top: 0;
    }
}
</style>
