import {
  makeAutoObservable
} from 'mobx';
import {

} from '../api/api';
import {
  delUrlParam,
  getUrlParam,
  isIos,
} from '../utils';
import Toast from '../components/toast';
import { getQRCode, randomStr, url2Base64, waitTime } from '../utils/utils';
import CryptoJS from 'crypto-js'
import modalStore from './modal';
import { GetCurrSkinId, getCustomShareId } from "../utils/utils";
import { PAGE_MAP } from '../utils/constants';
import { indexApi } from "../api/api.js";
export const skinId = GetCurrSkinId() || getCustomShareId();
const store = makeAutoObservable({

  /** 当前页面 */
  curPage:
    {
      // index: PAGE_MAP.HOME_PAGE,
    }[skinId] || PAGE_MAP.HOME_PAGE,

  pageData: {},

  changePage(val) {
    this.curPage = val
  },

  indexInfo: {},
  async getIndexInfo() {
    const res = await indexApi({
      channel: CFG.channel
    })
    console.log(res)
    if (res.ok) {
      const resData = res.data || {}
      store.indexInfo = resData

      let { medalInfoList, newGetMedalInfoList } = resData
      if (newGetMedalInfoList && newGetMedalInfoList.length > 0) {
        newGetMedalInfoList = newGetMedalInfoList.map(item => {
          const medalInfo = medalInfoList.find(medal => medal.medalId === item.medalId)
          return {
            ...item,
            ...medalInfo
          }
        })
        modalStore.pushPop('LightNewPop', { newGetMedalInfoList })
      }
    }
  },

  // 活动时间拦截
  // 结束不拦截 brakeEnd为false
  // 规则奖品始终可以点击 不用调方法 
  checkActivityStatus(brakeStart = true, brakeEnd = true) {
    const activityBaseInfo = store.indexInfo.activityBaseInfo;
    if (activityBaseInfo && activityBaseInfo.startTime && activityBaseInfo.endTime) {
      const startTime = activityBaseInfo.startTime;
      const endTime = activityBaseInfo.endTime;
      const now = new Date().getTime();
      if (now >= startTime && now <= endTime) {
        return true;
      }
      if (brakeStart && now < startTime) {
        Toast('活动暂未开始')
      }
      if (brakeEnd && now > endTime) {
        Toast('活动已经结束')
      }
    }
    Toast('活动异常~')
    return false;
  },

  // 跳转至【官微大转盘】
  jumpDraw() {
    wx.miniProgram.navigateTo({
      url: ' /packageTbzy/pages/lottery/lottery?activityCode=DRAWFARM&source_channel=nianjianhuodong'
    })
  }
});

export default store;

