import store from '@/store'
import StoreApi from '@/api/store'
import storage from '@/utils/storage'
import { isWeixinOfficial } from '@/core/platform'
import SettingModel from '@/common/model/Setting'
import WxofficialSettingModel from '@/common/model/wxofficial/Setting'

const CACHE_KEY = 'Store'

// 写入缓存, 到期时间10分钟
const setStorage = data => {
  const expireTime = 10 * 60
  storage.set(CACHE_KEY, data, expireTime)
}

// 获取缓存中的数据
const getStorage = () => {
  return storage.get(CACHE_KEY)
}

// 获取后端接口商城设置 (最新)
const getApiData = () => {
  return new Promise((resolve, reject) => {
    StoreApi.data().then(result => {
      // 初始化商城数据
      initStoreData(result.data)
      resolve(result.data)
    })
  })
}

// 初始化商城数据
const initStoreData = data => {
  // 将商城基本信息写入缓存
  setStorage(data)
  // 设置商城设置缓存
  SettingModel.setStorage(data.setting)
  // 设置全局自定义主题
  SettingModel.setAppTheme()
  // 设置微信公众号链接分享卡片内容（全局）
  if (isWeixinOfficial) {
    WxofficialSettingModel.setGlobalShareCard(false)
  }
  // 设置开启的功能模块
  store.dispatch('SetModules', data.modules)
}

/**
 * 获取商城基础信息
 * 有缓存的情况下返回缓存, 没有缓存从后端api获取
 */
const data = (isCache = true) => {
  return new Promise((resolve, reject) => {
    const cacheData = getStorage()
    if (isCache && cacheData) {
      resolve(cacheData)
    } else {
      getApiData().then(data => {
        resolve(data)
      })
    }
  })
}

// 获取商城基本信息
const storeInfo = () => {
  return new Promise((resolve, reject) => {
    data().then(data => resolve(data.storeInfo))
  })
}

// 获取H5端访问地址
const h5Url = () => {
  return new Promise((resolve, reject) => {
    data().then(data => {
      const h5Url = data.clientData.h5.setting.baseUrl
      resolve(h5Url)
    })
  })
}

// 是否开启分销中心
const isEnabledDealer = () => {
  return new Promise((resolve, reject) => {
    data().then(data => {
      const isEnabledDealer = Boolean(data.dealer.setting.is_open)
      resolve(isEnabledDealer)
    })
  })
}

export default {
  data,
  storeInfo,
  h5Url,
  isEnabledDealer
}