import lodash from 'lodash';
import queryString from 'query-string';
import { getCurrentUrl } from '@/browser/tab';
import { appSetting, indentifySetting } from '@/plugins/setting';
import { fetchExtensionInfo, fetchPlatformConfig } from '@/api/autoshopee/cms';
import {
  getBrowserName, getBrowserAgent, getPlatformInfo, getIpAddress, getHashProfileId, getFingerprintId,
} from '@/lib/get';

/** @type {VuexStoreModule} */
export const appStore = {
  /** @returns {StateApp} */
  state() {
    const extensionInfo = {
      notify: {
        uuid: '',
        is_show: false,
        title: '',
        content: '',
      },
      ...appSetting.extensionInfo.get(),
    };

    const platformShopConfig = {
      shopee: {},
      lazada: {},
      tiktok: {},
      ...appSetting.platformShopConfig.get(),
    };

    const indentify = {
      appId: indentifySetting.appUuid.get(),
      profileId: indentifySetting.profileId.get(),
      agent: indentifySetting.browserAgent.get(),
      browser: indentifySetting.browserName.get(),
      ip: indentifySetting.browserIp.get(),
      platform: indentifySetting.browserPlatform.get(),
      fingerprint: indentifySetting.fingerprintId.get(),
    };

    return {
      menuActive: '',
      loading: false,
      url: '',
      extensionInfo,
      platformShopConfig,
      indentify,
      title: '',
    };
  },
  mutations: {
    SET_MENU_ACTIVE(state, value) {
      state.menuActive = value;
    },
    SET_APP_LOADING(state, value) {
      state.loading = value;
    },
    SET_APP_TITLE(state, title) {
      state.title = title;
    },
    SET_URL_CURRENT(state, url) {
      state.url = url;
    },
    SET_APP_EXTENSION_INFO(state, value) {
      appSetting.extensionInfo.set(value);
      state.extensionInfo = value;
    },
    SET_SHOP_PLATFORM_CONFIG(state, value) {
      appSetting.platformShopConfig.set(value);
      state.platformShopConfig = value;
    },
    /** @param {StateApp} state */
    SET_APP_INDENTIFY(state) {
      state.indentify.profileId = indentifySetting.profileId.get();
      state.indentify.agent = indentifySetting.browserAgent.get();
      state.indentify.browser = indentifySetting.browserName.get();
      state.indentify.ip = indentifySetting.browserIp.get();
      state.indentify.platform = indentifySetting.browserPlatform.get();
      state.indentify.fingerprint = indentifySetting.fingerprintId.get();
    },
  },
  getters: {
    getMenuActive(state) {
      return state.menuActive;
    },
    getAppLoading(state) {
      return state.loading;
    },
    getUrlCurrent(state) {
      return state.url;
    },
    getNotifyApp(state) {
      return state.extensionInfo.notify;
    },
    getNotifyBanner(state) {
      return lodash.get(state.extensionInfo.notify, 'json.banner-popup', null);
    },
    getAppInfo(state) {
      return state.extensionInfo;
    },
    getAppTitle(state) {
      return state.title;
    },
    getProfileId(state) {
      return state.indentify.profileId;
    },
    getBrowserIp(state) {
      return state.indentify.ip;
    },
    getPlatformShopeeConfig(state) {
      return state.platformShopConfig.shopee;
    },
    getPlatformTiktokConfig(state) {
      return state.platformShopConfig.tiktok;
    },
    getPlatformLazadaConfig(state) {
      return state.platformShopConfig.lazada;
    },
    getExtensionInfo(state) {
      return state.extensionInfo;
    },
  },
  actions: {
    async AppIndentify({ commit }) {
      const browserIp = await getIpAddress();
      const browserOs = (await getPlatformInfo()).os;
      const profileId = await getHashProfileId(browserIp);
      const fingerprintId = await getFingerprintId();

      indentifySetting.fingerprintId.set(fingerprintId);
      indentifySetting.profileId.set(profileId);
      indentifySetting.browserAgent.set(getBrowserAgent());
      indentifySetting.browserName.set(getBrowserName());
      indentifySetting.browserIp.set(browserIp);
      indentifySetting.browserPlatform.set(browserOs);

      commit('SET_APP_INDENTIFY');
    },
    async FetchExtensionInfo({ commit }) {
      const extensionInfo = await fetchExtensionInfo();
      const platformConfig = await fetchPlatformConfig();

      commit('SET_APP_EXTENSION_INFO', extensionInfo);
      commit('SET_SHOP_PLATFORM_CONFIG', platformConfig);

      return {
        extensionInfo,
        platformConfig,
      };
    },
    async AppInitUrl({ commit, getters }) {
      if (getters.getUrlCurrent) {
        return getters.getUrlCurrent;
      }

      let url = await getCurrentUrl();

      const urlParser = queryString.parse(url);

      if (urlParser && urlParser.refUrl) {
        url = urlParser.refUrl;
      }

      if (!url) {
        return '';
      }

      commit('SET_URL_CURRENT', url);

      return url;
    },
    SetAppLoading({ commit }, value) {
      commit('SET_APP_LOADING', value);
    },
    SetMenuActive({ commit }, value) {
      commit('SET_MENU_ACTIVE', value);
    },
    SetAppTitle({ commit }, title) {
      document.title = title;
      commit('SET_APP_TITLE', title);
    },
  },
};
