import { defineStore } from "pinia";
import verConfig from "@/ver.config";
import { GTE_BANNER, RATE_LIST, OTHER_CONFIG, CONTENT_WINNOTICE, CONTENT_COUPON, CONTENT_BOXES } from "@/api";
import { getStorage, setStorage } from "@/utils";
import { nextTick } from "vue"
import { openUrl } from "@/utils"
import { setLan } from "@/locale"
import { useShopStore } from "./shop"

const state = () => ({
  banner: [],
  rateList: [],
  langeuageList: [],
  countryCodesList: [],
  currency: getStorage(verConfig.symbol).code || verConfig.currency,
  lang: getStorage(verConfig.langName) || verConfig.lang,
  symbol: getStorage(verConfig.symbol) || {},
  winnotice: {},
  coupon: {},
  count: 0,
  networkStatus: true,
  isConnected: true,
  isFirstConnectNetwork: true,
  isHomeFirstConnect: false,
  appinfo: {
    update: 0
  }
})

export const useSystemStore = defineStore('system', {
  state,
  getters: {
    getBanner() {
      // if (!this.banner.length) {
      //   this.setBanner()
      // }
      return this.banner || []
    },
    getRateList() {
      return this.rateList;
    },
    getLangeuage() {
      return this.langeuageList
    },
    getCountryCodes() {
      return this.countryCodesList
    },
    getCurrency() {
      return this.currency
    },
    getLang() {
      return this.lang
    },
    getSymbol() {
      return this.symbol
    },
    getWinnotice() {
      return this.winnotice
    },
    getCoupon() {
      return this.coupon
    },
    getAppinfo() {
      return this.appinfo
    },
    getCount() {
      return this.count
    }
  },
  actions: {
    // 检查网络状态
    checkNetworkStatus() {
      uni.getNetworkType({
        success: (result) => {
          const { networkType, errMsg } = result
          if (networkType != 'none') {
            if (this.isFirstConnectNetwork) {
              this.setFirstConnectStatus(false)
            }
            this.setNetworkStatus(true)
          }
          else {
            this.setNetworkStatus(false)
          }
        }
      })
    },
    // 设置网络和连接状态
    setNetworkStatus(status) {
      this.networkStatus = status
      this.isConnected = status
    },
    setFirstConnectStatus(flag) {
      this.isFirstConnectNetwork = flag
    },
    setIsHomeFirstConnect(flag) {
      this.isHomeFirstConnect = flag
    },

    setBanner() {
      return new Promise(async (resolve, reject) => {
        try {
          const res = await GTE_BANNER();
          this.banner = res.data || []
        } catch (error) { }
      })
    },
    setLan(_lan_) {
      this.lang = _lan_
    },
    setCurrency(currency) {
      this.currency = currency;
      this.setSymbol();
    },
    setAppinfo(info) {
      this.appinfo = info
    },
    async setRateList() {
      try {
        const res = await RATE_LIST();
        this.rateList = res.data.currencys;
        this.langeuageList = res.data.langs;
        this.countryCodesList = res.data.countryCodes

        nextTick(() => {
          const lang = res.data.language
          this.initLanguage(lang)
          !getStorage(verConfig.symbol) && this.setSymbol();
        })
      } catch (error) { }
    },
    initLanguage(lang) {
      const useShop = useShopStore();
      const savedLang = getStorage('userLanguage');
      if (!savedLang) {
        setStorage(verConfig.langName, lang);
        this.setLan(lang)
        setLan(lang, () => {
          useShop.setHotLink();
        });
        setStorage('userLanguage', lang);
      }
    },
    setSymbol() {
      let findItem = this.rateList.find((item) => item.code == this.currency);
      setStorage(verConfig.symbol, findItem);
      this.currency = findItem.code;
      this.symbol = findItem
    },
    async service() {
      try {
        const res = await OTHER_CONFIG('system');
        if (!res.data.service_url) return;
        openUrl(res.data.service_url);
      } catch (error) { }
    },
    async setWinnotice() {
      try {
        const res = await CONTENT_WINNOTICE();
        this.winnotice = res.data && res.data[0]
      } catch (error) { }
    },
    async setCoupon(params = {}, other = {}) {
      try {
        const res = await CONTENT_COUPON(params, other);
        this.coupon = res.data && res.data[0]
      } catch (error) { }
    },
    async setBoxes(params = {}) {
      try {
        const res = await CONTENT_BOXES(params);
        this.count = res.data && res.data.count || 0
      } catch (error) { }
    },

  }
})