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

<script>
// Components
import makeQuestion from "@/layouts/vehicle-make/make-question/make-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 { storeMutations } from "@/constants/store-mutations";
import { storeActions } from "@/constants/store-actions";
import store from "@/store";

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

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

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

    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.makeQuestion.initializeComponent(
        resultMap.cmsContent.VehicleMakeQuestion,
        resultMap.makeQuestionInitialData
      );
    });
  },

  methods: {
    backButtonAction() {
      // route to move backwards
      this.$router.navigate(
        this.navigationScenarios.CLICKED_BACK,
        this.$route
      );
    },
    arePagePrerequisitesValid() {
      return store.getters.vehicle.year !== null;
    },
    resetDependentState() {
      // Set
      store.commit(storeMutations.UPDATE_MODEL, null);
      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_STATE);
      store.dispatch(storeActions.RESET_REGISTRATION_STATE);
    },
  },

  watch: {
    selectedMake(make) {
      this.$store.commit(this.storeMutations.UPDATE_MAKE, make);
      this.$router.navigateAfterSave(
        this.navigationScenarios.SELECTED_MAKE,
        this.$route
      );
    },
  },

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