<template>
  <div class="container-fluid shadow rounded-3 p-0 position-relative">
    <funnelHeader ref="funnelHeader" />
    <div class="select-car">
      <div class="select-car-form rounded text-center">
        <vehicleBanner ref="vehicleBanner" />
        <funnelSubHeader
          ref="funnelSubHeader"
          :hasBackButton="true"
          backButtonAccessibleText="Change Vehicle Make"
          @click-event="backButtonAction"
        />
        <modelQuestion v-model="selectedModel" ref="modelQuestion" />
      </div>
    </div>
  </div>
</template>

<script>
// Components
import modelQuestion from "@/layouts/vehicle-model/model-question/model-question";
import funnelHeader from "@/common-components/funnel-header/funnel-header";
import funnelSubHeader from "@/common-components/funnel-sub-header/funnel-sub-header";
import vehicleBanner from "@/common-components/vehicle-banner/vehicle-banner";
// Supporting files
import { fetchCmsContentForPage } from "@/helpers/cms-content-helper";
import { storeMutations } from "@/constants/store-mutations";
import { storeActions } from "@/constants/store-actions";
import { settleAllPromises } from "@/helpers/layout-helper";
import store from "@/store";

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

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

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

    const resultMap = await settleAllPromises(promiseResultMap);

    // Call the "next" function to complete the transition to this page.
    next((vm) => {
      vm.$refs.funnelSubHeader.initializeComponent(
        resultMap.cmsContent.FunnelSubHeaderWidget
      );
      vm.$refs.funnelHeader.initializeComponent(
        resultMap.cmsContent.FunnelHeaderWidget
      );
      vm.$refs.vehicleBanner.initializeComponent(
        resultMap.cmsContent.VehicleBannerWidget
      );
      vm.$refs.modelQuestion.initializeComponent(
        resultMap.cmsContent.VehicleModelQuestion,
        resultMap.modelQuestionInitialData
      );
    });
  },

  methods: {
    backButtonAction() {
      // route to move backwards
      this.$router.navigate(
        this.navigationScenarios.CLICKED_BACK,
        this.$route
      );
    },
    arePagePrerequisitesValid() {
      return store.getters.vehicle.make !== null;
    },
    resetDependentState() {
      // Set
      store.commit(storeMutations.UPDATE_STYLE, null);
      store.commit(storeMutations.UPDATE_CAR_ID, null);
      store.commit(storeMutations.UPDATE_VEHICLE_CATEGORY, null);

      // Invokes
      store.dispatch(storeActions.RESET_DAMAGE_AND_DEPS);
      store.dispatch(storeActions.RESET_REGISTRATION_AND_DEPS);
    },
  },

  watch: {
    selectedModel(model) {
      this.$store.commit(this.storeMutations.UPDATE_MODEL, model);
      this.$router.navigateAfterSave(
        this.navigationScenarios.SELECTED_MODEL,
        this.$route
      );
    },
  },

  components: {
    modelQuestion,
    funnelHeader,
    funnelSubHeader,
    vehicleBanner,
  },
};
</script>
