import { ACT_STATUS, ACT_STATUS_TOAST, PAGE_MAP, TOPIC_LIST } from '@src/utils/constants';
import { makeAutoObservable } from 'mobx';
import API from '../api/index';
import { GetCurrSkinId, getCustomShareId } from "@src/utils/utils";
import { initSensors } from '@src/utils/sensorMd';
import { Toast } from '@spark/ui';

const skinId = GetCurrSkinId() || getCustomShareId();

const store = makeAutoObservable({
  /** 活动规则 */
  ruleInfo: '',

  /** 前端开发配置 */
  frontVariable: {},

  /** 当前页面 */
  curPage: {
    index: PAGE_MAP.HOME_PAGE,
    myPrize: PAGE_MAP.PRIZE_PAGE,
    // 免登中间页-线上测试
    Did1NjA4NjM: PAGE_MAP.MID_PAGE,
    // 免登中间页-线上正式
    Did1NjA4NzE: PAGE_MAP.MID_PAGE,
  }[skinId] || PAGE_MAP.LIFE_SETTING_PAGE,

  pageData: {},

  /** 场景切换 */
  changePage(page, data = {}) {
    this.pageData = data;
    this.curPage = page;
  },

  /** 获取活动规则 */
  async initRule() {
    // 模拟获取远程的数据
    const { data } = await API.getRule();
    this.ruleInfo = data;
  },

  /** 获取前端配置项 */
  async getFrontVariable() {
    // 获取前端开发配置
    const { data } = await API.getFrontVariable();
    const resData = data || {};
    if (resData.shareInfo) resData.shareInfo = JSON.parse(resData.shareInfo);
    this.frontVariable = resData;
    console.log('前端开发配置', data)
  },

  /** 首页信息 */
  homeInfo: {},
  async getHomeInfo() {
    let params = {}
    const shareCode = getUrlParam("shareCode") && getUrlParam("shareCode") !== 'null' ? getUrlParam("shareCode") : '';
    if (shareCode) {
      params.shareCode = shareCode
    }
    const { success, data, timeStamp } = await API.home(params);
    if (success) {
      const resData = data || {}
      const { startTime, endTime } = resData
      if (timeStamp >= startTime && timeStamp < endTime) {
        resData._actStatus = ACT_STATUS.ING;
      } else if (timeStamp < startTime) {
        resData._actStatus = ACT_STATUS.NOT_START;
      } else {
        resData._actStatus = ACT_STATUS.ENDED;
      }
      this.homeInfo = resData;
      console.log('首页数据', this.homeInfo);
    }
  },
  /**
  * 校验活动状态
  */
  checkActStatus(isSpecial) {
    if (this.homeInfo._actStatus && this.homeInfo._actStatus !== ACT_STATUS.ING) {
      // 活动已结束：【奖品】、【规则】按钮可点
      if (isSpecial && this.homeInfo._actStatus === ACT_STATUS.ENDED) return true;

      Toast(ACT_STATUS_TOAST[this.homeInfo._actStatus]);
      return false;
    }
    return true;
  },

  startInfo: {},
  roleId: 'a', // 人设id
  async startTest() {
    this.roleId = 'a'
    const { success, data } = await API.startAnswer();
    if (success) {
      this.startInfo = data || {}
      // 从题库随机出一条数据
      this.startInfo.topicList = TOPIC_LIST[Math.floor(Math.random() * TOPIC_LIST.length)]
      this.changePage(PAGE_MAP.QUES_PAGE)
    }
  },
  async submitTest(answer) {
    let roleId = ''
    answer.map((item, index) => {
      const itemRuleId = store.startInfo.topicList[index]?.options?.[item]?.split("$$$")?.[1]
      if (itemRuleId) roleId = itemRuleId
    })
    this.roleId = roleId
    const { success, data } = await API.submitAnswer({
      chooses: roleId, // 人设Id
      startId: this.startInfo.startId
    });
    if (success) {
      this.changePage(PAGE_MAP.RESU_PAGE, {
        awardTimes: data.awardTimes || 0
      })
    }
  },

  userInfo: {},
  setUserInfo(userInfo) {
    this.userInfo = userInfo;
  },
  /** 初始化神策埋点 */
  async initSensorsMd() {
    const res = await API.queryUserInfo();
    if (res.success) {
      store.setUserInfo(res.data || {});
      const { unionId, openId, friendEmpNo, friendOpenId, newEmpNo } = res.data || {};
      // initSensors(unionId, openId, friendEmpNo, friendOpenId);
      initSensors(unionId, openId, newEmpNo);
    }
  },
})
export default store;
