import {
    setupAdvertiserTracking,
    getAffiliateCookies,
} from "@/helpers/heritage-integration/cookie-helper";
import store from "@/store";
import { storeMutations } from "@/constants/store-mutations";
import { storeActions } from "@/constants/store-actions";
import { getQuerystringParameter } from "@/helpers/querystring-helper";
import { queryStrings } from "@/constants/query-strings";
import { externalParameterStatus } from "@/constants/external-parameters";

export async function initializeFromQueryStrings() {
    // Get advertiser info
    setupAdvertisersAndAffiliates();

    // Read all external paramter queries and write to store.
    updateExternalParameterState();
}

function setupAdvertisersAndAffiliates() {
    const affiliateCookies = getAffiliateCookies();
    if (affiliateCookies != null && affiliateCookies.length > 0) {
        store.commit(storeMutations.UPDATE_AFFILIATE_COOKIES, affiliateCookies);
    }
    setupAdvertiserTracking();
}

function updateExternalParameterState() {
    const externalParameterYear = getQuerystringParameter(queryStrings.VEHICLE_YEAR);
    const externalParameterMake = getQuerystringParameter(queryStrings.VEHICLE_MAKE);
    const externalParameterModel = getQuerystringParameter(queryStrings.VEHICLE_MODEL);
    const externalParameterStyle = getQuerystringParameter(queryStrings.VEHICLE_STYLE);
    const externalParameterDamage = getQuerystringParameter(queryStrings.VEHICLE_DAMAGE);
    const externalParameterZipCode = getQuerystringParameter(queryStrings.SERVICE_ZIP);
    const externalParameterEmail = getQuerystringParameter(queryStrings.EMAIL);
    const externalParameterIsInsurance = getQuerystringParameter(queryStrings.IS_INSURANCE);
    const externalParameterVinSelection = getQuerystringParameter(queryStrings.VIN_SELECTION);
    const externalParameterServicePackage = getQuerystringParameter(queryStrings.SERVICE_PACKAGE);
    const externalParameterNumberOfChips = getQuerystringParameter(queryStrings.NUMBER_OF_CHIPS);
    const externalParameterSource = getQuerystringParameter(queryStrings.EXPERIMENTS);
    const externalParameterPhoneNumber = getQuerystringParameter(queryStrings.PHONE_NUMBER);
    if (
        externalParameterYear &&
        externalParameterMake &&
        externalParameterModel &&
        externalParameterStyle
    ) {
        resetStoreForExternalParameter();
        store.commit(storeMutations.UPDATE_IS_EXTERNAL_PARAMETER, externalParameterStatus.ACTIVE);
        store.commit(storeMutations.UPDATE_EXTERNAL_PARAMETER_YEAR, externalParameterYear);
        store.commit(storeMutations.UPDATE_EXTERNAL_PARAMETER_MAKE, externalParameterMake);
        store.commit(storeMutations.UPDATE_EXTERNAL_PARAMETER_MODEL, externalParameterModel);
        store.commit(storeMutations.UPDATE_EXTERNAL_PARAMETER_STYLE, externalParameterStyle);
    }
    if (
        externalParameterDamage &&
        (externalParameterDamage.toUpperCase() != "WINDSHIELDREPAIR" ||
            externalParameterNumberOfChips?.length > 0)
    ) {
        store.commit(storeMutations.UPDATE_EXTERNAL_PARAMETER_DAMAGE_TYPE, externalParameterDamage);
        if (externalParameterDamage.toUpperCase() == "WINDSHIELDREPLACE") {
            store.commit(storeMutations.UPDATE_EXTERNAL_PARAMETER_IS_REPAIR, false);
            store.commit(storeMutations.UPDATE_EXTERNAL_PARAMETER_NUMBER_OF_CHIPS, null);
        } else if (externalParameterDamage.toUpperCase() == "WINDSHIELDREPAIR") {
            store.commit(storeMutations.UPDATE_EXTERNAL_PARAMETER_IS_REPAIR, true);
            store.commit(
                storeMutations.UPDATE_EXTERNAL_PARAMETER_NUMBER_OF_CHIPS,
                externalParameterNumberOfChips
            );
        }
    }
    if (externalParameterZipCode) {
        store.commit(storeMutations.UPDATE_EXTERNAL_PARAMETER_ZIP_CODE, externalParameterZipCode);
    }
    if (externalParameterEmail) {
        store.commit(
            storeMutations.UPDATE_EXTERNAL_PARAMETER_EMAIL_ADDRESS,
            externalParameterEmail
        );
    }
    if (externalParameterIsInsurance?.toUpperCase() == "TRUE") {
        store.commit(storeMutations.UPDATE_EXTERNAL_PARAMETER_IS_INSURANCE, true);
        store.commit(
            storeMutations.UPDATE_EXTERNAL_PARAMETER_SERVICE_PACKAGE,
            externalParameterServicePackage?.toLowerCase()
        );
    }
    if (externalParameterDamage?.toUpperCase() != "WINDSHIELDREPAIR") {
        store.commit(
            storeMutations.UPDATE_EXTERNAL_PARAMETER_VIN_SELECTION,
            externalParameterVinSelection
        );
    }
    if (externalParameterSource) {
        store.commit(storeMutations.UPDATE_EXTERNAL_PARAMETER_SOURCE, externalParameterSource);
    }
    if (externalParameterPhoneNumber) {
        store.commit(
            storeMutations.UPDATE_EXTERNAL_PARAMETER_PHONE_NUMBER,
            externalParameterPhoneNumber
        );
    }
}

// if there is an existing external parameter state then they have already been through from an external source(LeadGen) and
// are returning.  clear out prior related things that need to be selected again like parts and damage type.
function resetStoreForExternalParameter() {
    if (
        store.getters.externalParameterState?.isExternalParameter ==
        externalParameterStatus.INACTIVE
    ) {
        store.dispatch(storeActions.RESET_VEHICLE_STATE_AND_DEPENDENCIES);
        store.dispatch(storeActions.SAVE_SERVICE_ZIP_CODE_INFO, {
            state: null,
            zipCode: null,
            zipCodeCtu: null,
        });
        store.dispatch(storeActions.SAVE_EMAIL, null);
        store.dispatch(storeActions.RESET_DAMAGE_STATE_AND_DEPENDENCIES);
    }
}
