import emitter from "@/utils/eventBus";
import ToolsBase from './tools/ToolsBase.js';
import router from '@/router';
import storage, { actions } from "@/utils/storage";
import { useUserStoreWithOut } from "@/stores/modules/user";
import { useBaseStoreWithOut } from '@/stores/modules/base';
import { useNoticeStoreWithOut } from '@/stores/modules/notice';
import { useGameListStoreWithOut } from '@/stores/modules/game-list';
import { useGameStoreWithOut } from '@/stores/modules/game'

export default class extends ToolsBase {
  constructor(props) {
    super()
    // console.log('---开始接收通知---', props)
    this.props = props

    //拼单单开奖通知
    this.props.on(this.cfg.global.EVENT_NAME.PDD_AWARD, (res) => {
      console.log(res, 'EVENT_NAME.PDD_AWARD')
      if (res.uid === useUserStoreWithOut().userInfo.uid) {
        console.log("已中奖")
        useUserStoreWithOut().getUserInfo();
        useUserStoreWithOut().setPddAward(res.propsList);
      }
    })
    // 其他通知
    this.props.on(this.cfg.global.EVENT_NAME.GAME_NOTIFY, (res) => {
      console.log(res, 'EVENT_NAME.GAME_NOTIFYEVENT_NAME.GAME_NOTIFY')
      // 刷新用户信息
      if (res.isUpdateMyInfo) {
        console.log('--------------refresh userinfo--------------')
        useUserStoreWithOut().getUserInfo();
      }
      // 有新站内信
      if (res.newNotice) {
        useUserStoreWithOut().hasNewNotice(res.newNotice);
      }
      // 有新邮件
      if (res.newMail) {
        useUserStoreWithOut().hasNewMail(res.newMail);
        useBaseStoreWithOut().setRefreshTurntable(false)
        /**
         * 是否有转盘任务
         * 刷新转盘
         * 刷新帮助列表
         */
        // if (useBaseStoreWithOut().turntableConfig.helpTurntable2TaskList && useBaseStoreWithOut().turntableConfig.helpTurntable2TaskList.rows.length) {
        //   useBaseStoreWithOut().getHelpTurntable()
        //   useBaseStoreWithOut().setRefreshTurntable(true)
        //   useBaseStoreWithOut().refreshHelpList({
        //     _id: useBaseStoreWithOut().turntableConfig.helpTurntable2TaskList.rows[0]._id,
        //     uid: useUserStoreWithOut().userInfo.uid,
        //     pageIndex: 1,
        //     pageSize: 10,
        //   })
        // }
      }
    })
    // 广播通知
    this.props.on(this.cfg.global.EVENT_NAME.BROADCAST, (res) => {
      useNoticeStoreWithOut().setBroadcast(res)
      if (!res.url && !res.urlType) {
        useNoticeStoreWithOut().setNotice(res)
      }
    })
    // 转盘通知
    this.props.on(this.cfg.global.EVENT_NAME.NEW_RECEIVE_HELP_TURNTABLE2, (res) => {
      // console.log(res, '-----NEW_RECEIVE_HELP_TURNTABLE2-----')
    })
    // 下线通知
    this.props.on(this.cfg.global.EVENT_NAME.ON_KICK, (res) => {
      console.log(res, '-----下线通知-----2');
      storage.clearStorage();
      storage.removeSessionStorage(actions.HOME_POSITION);
      useUserStoreWithOut().clearUser();
      useGameStoreWithOut().clearGameUrl();
      emitter.emit("closeGame");
      router.replace("/login");
    })

    // GAME_INFO_CHANGE 游戏列表数据更新通知
    this.props.on(this.cfg.global.EVENT_NAME.GAME_INFO_CHANGE, (res) => {
      console.log(res, '---GAME_INFO_CHANGE---')
      if (res.code !== 0) return
      let originalList = useGameListStoreWithOut().originalList;
      let newGameData = [...originalList]
      if (!newGameData.find(item => item._id === res.data._id)) {
        newGameData.unshift(res.data)
        return useGameListStoreWithOut().setAllGameList(newGameData)
      }
      for (let i = 0; i < originalList.length; i++) {
        if (originalList[i]._id === res.data._id) {
          newGameData[i] = res.data
        }
      }
      newGameData = newGameData.filter(item=>item.state !== -1);
      useGameListStoreWithOut().setAllGameList(newGameData)
    })
    // 轮播状态更新 
    this.props.on(this.cfg.global.EVENT_NAME.ROTATION_CHART_CHANGE, async (res) => {
      if (res.code !== 0) return
      await useGameListStoreWithOut().setCarouselData()
    })
    // 游戏厂商跟新
    this.props.on(this.cfg.global.EVENT_NAME.GAME_PROVIDER_CHANGE, async (res) => {
      if (res.code !== 0) return
      await useGameListStoreWithOut().setProviderListData()
    })
    // 游戏类数据跟新
    this.props.on(this.cfg.global.EVENT_NAME.GAME_CLASS_CHANGE, async (res) => {
      if (res.code !== 0) return
      await useGameListStoreWithOut().setGameClassDataData(true)
    })
    // 功能状态更新
    this.props.on(this.cfg.global.EVENT_NAME.SWITCH_CONFIG, async (res) => {
      useBaseStoreWithOut().globalConfig.switchConfig[res.key] = res.value
    })
    // 平台配置更新
    this.props.on(this.cfg.global.EVENT_NAME.PLATFORM_CONFIG, async (res) => {
      useBaseStoreWithOut().globalConfig.platformConfig[res.key] = res.value
    })

    this.props.on('close', (res) => {
      console.log(res, 'close-------socket')
    })

    this.props.on('disconnect', (res) => {
      console.log(res, 'disconnect-------socket')
    })
  }
}