<template>
  <div class="page-container-grouped-styles">
    <funnelHeader cmsWidgetName="FunnelHeaderWidget" />
    <div class="select-car">
      <div class="select-car-form rounded text-center">
        <vehicleBanner cmsWidgetName="VehicleBannerWidget" displayGenericVehicleImage />
        <funnelSubHeader cmsWidgetName="FunnelSubHeaderWidget" />
        <div class="fade-on-route-transition">
          <yearQuestion 
            class="fade-on-route-transition"
            v-model="selectedYear" 
            ref="yearQuestion" 
            cmsWidgetName="VehicleYearQuestion" 
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// Components
import yearQuestion from "@/layouts/vehicle-year/year-question/year-question";
import funnelHeader from "@/common-components/funnel-header/funnel-header";
import vehicleBanner from "@/common-components/vehicle-banner/vehicle-banner";
import funnelSubHeader from "@/common-components/funnel-sub-header/funnel-sub-header";

// Supporting files
import { fetchCmsContentForPage } from "@/helpers/cms-content-helper";
import { settleAllPromises } from "@/helpers/layout-helper";
import { storeActions } from "@/constants/store-actions";
import { experimentUniverses } from "@/constants/experiments";
import { getDeviceIdValue, getSessionKeyValue } from "@/helpers/heritage-integration/cookie-helper";
import store from "@/store";

import baseMixin from "@/mixins/base-mixin";

export default {
  name: "vehicle-year",
  data() {
    return {
      selectedYear: null,
    };
  },
  computed: {},

  async beforeRouteEnter(to, from, next) {
    // Call APIs
    const cmsContentPromise = fetchCmsContentForPage(to.query.fmgPage);
    const yearQuestionInitialDataPromise = yearQuestion.methods.loadInitialData();

    const experimentForLogging = store.getters.applicationUser.experiments.find(e => e.universeName === experimentUniverses.CONCEPT_FUNNEL);

    // If the concept funnel experiment is found, as it should be when coming from safelite.com, then log the experiment exposure.
    if (experimentForLogging !== undefined) {
      // Log experiment exposure
      baseMixin.methods.dispatchStoreAction(storeActions.LOG_EXPERIMENT_EXPOSURE, 
      {
        userId: getDeviceIdValue(),
        sessionKey: getSessionKeyValue(),
        pageName: to.query.fmgPage,
        experiment: experimentForLogging
      }, false);
    }

    // Settle promises and get results
    const promiseResultMap = [
      {
        resultKey: "cmsContent",
        promise: cmsContentPromise,
      },
      {
        resultKey: "yearQuestionInitialData",
        promise: yearQuestionInitialDataPromise,
      },
    ];

    let resultMap = await settleAllPromises(promiseResultMap);

    // Call the "next" function to complete the transition to this page.
    next((vm) => {
      vm.setCmsContent(resultMap.cmsContent);
      vm.$refs.yearQuestion.initializeComponent(
        resultMap.yearQuestionInitialData
      );
    });
  },

  watch: {
    selectedYear(year) {
      const parsedYear = parseInt(year);
      this.dispatchStoreAction(storeActions.SAVE_VEHICLE_YEAR, parsedYear);
      this.$router.navigate(
        this.navigationScenarios.SELECTED_YEAR,
        this.$route
      );
    },
  },
  methods: {
    arePagePrerequisitesValid() {
      return true;
    },
  },
  components: {
    yearQuestion,
    funnelHeader,
    vehicleBanner,
    funnelSubHeader,
  },
};
</script>
