import { EventEmitter } from 'events'
import { GetExtVersionInfoRsp } from '../typings/panel'

const bus = new EventEmitter()

const MOCK_IMG = 'https://huyaimg.msstatic.com/cdnimage/game/1001-MS.jpg'

const mockSdk: {
  initGameEnv: typeof hyExt.exe.initGameEnv
  downloadBatchRes: typeof hyExt.fs.downloadBatchRes
  on: typeof hyExt.context.on
  off: typeof hyExt.context.off
  launchGame: typeof hyExt.exe.launchGame
  onGameMessage: typeof hyExt.exe.onGameMessage
  getExtInfo: typeof hyExt.env.getExtInfo
  commonQuery: typeof hyExt.backend.commonQuery
  getStreamerInfo: typeof hyExt.context.getStreamerInfo
} = {
  async initGameEnv(req) {
    console.log('mock initGameEnv', req)
    // 自动触发成功的gameMessage
    setTimeout(() => {
      bus.emit('gameMessage', {
        name: 'GameEnvEvent',
        message: JSON.stringify({
          res: 0,
          progress: 100,
        }),
      })
    }, 1000)
  },
  async downloadBatchRes(req) {
    console.log('mock downloadBatchRes', req)
    // 触发下载消息
    setTimeout(() => {
      bus.emit('downloadProgress', {
        res: 1,
        bytesTotal: 1000,
        bytesLoaded: 200,
        url: req[0].url,
        md5: req[0].md5,
      })
    }, 1000)
    setTimeout(() => {
      bus.emit('downloadProgress', { res: 0, url: req[0].url, md5: req[0].md5 })
    }, 2000)
  },
  async on(eventName, callback) {
    bus.addListener(eventName, callback)
  },
  async off(eventName, callback) {
    if (callback) {
      bus.removeListener(eventName, callback)
    }
  },
  async launchGame(req) {
    // return Promise.reject(new Error('launchGame mock error'))
  },
  async onGameMessage(req) {
    console.log('mock onGameMessage', req)
    bus.addListener('gameMessage', req.callback)
  },
  async getExtInfo() {
    return {
      extUuid: 'extUuid',
      extName: 'extName',
      extType: '1',
      extVersion: 'extVersion',
      extVersionType: 1,
    }
  },
  async commonQuery(req) {
    if (req.key === 'getExtVersionInfo') {
      const rsp: GetExtVersionInfoRsp = {
        extUuid: 'extUuid',
        extName: '游戏名最长八个字',
        extVersion: 'extVersion',
        extVersionType: 1,
        extLogo: MOCK_IMG,
        extIcon: MOCK_IMG,
        extMarketSnapshots: [MOCK_IMG, MOCK_IMG],
        extMarketVideoList: [MOCK_IMG],
        extBrief: 'extBrief',
        extDescription:
          '我是游戏介绍我是游戏介绍我是游戏介绍我是游戏介绍 我是游戏介绍我是游戏介绍',
        extAuthorName: 'extAuthorName',
        enableCloudGame: '1',
        cppGameinfo: {
          cppGameAppName: '游戏Demo',
          packageUrl:
            'https://g1c972e2182629c7-59ku7pqm.hyext.com/extResource/GameDemo.zip',
          programPath: 'GameDemo.exe',
          packageRemark: 'packageRemark',
          packageMD5: 'f5fc57a45bdb293d2e298de50022bc2e',
          env: 'test',
          programParams: '--foo=bar',
          exeVersion: '',
        },
      }
      return rsp
    }
  },
  async getStreamerInfo() {
    return {
      streamerAvatarUrl: '',
      streamerNick: '',
      streamerSex: 0,
      streamerRoomId: `${1234}`,
      streamerLevel: 1,
      streamerUnionId: '',
    }
  },
}

export default mockSdk
