import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { createScopeSelectorHook } from './helper'

export type MainState = {
  panel: {
    userUnionId: string
    streamerUnionId: string
    roomid: number
    gameTitle: string
    gameDesc: string
    gameIcon: string
    gameLaunchParam: GameLaunchParam
    gameImgUrlList: string[]
    localStatus: GameLaunchStatus
    cloudStatus: GameLaunchStatus
  }
}

export type GameLaunchParam = {
  gameUrl: string
  gameMd5: string
  gameName: string
  gameExeName: string
  env: string
  programParams: string
  exeVersion: string
}

export type GameLaunchStatus = {
  isAvailable: boolean
  status: 'idle' | 'launching' | 'launchSuccessed' | 'launchFailed'
  statusMsg: string
  progressList: number[]
}

const defaultState: MainState = {
  panel: {
    userUnionId: '',
    streamerUnionId: '',
    roomid: 0,
    gameTitle: '',
    gameDesc: '',
    gameIcon: '',
    gameImgUrlList: [],
    localStatus: {
      isAvailable: true,
      status: 'idle',
      statusMsg: '',
      progressList: [],
    },
    cloudStatus: {
      isAvailable: true,
      status: 'idle',
      statusMsg: '',
      progressList: [],
    },
    gameLaunchParam: {
      gameUrl: '',
      gameMd5: '',
      gameName: '',
      gameExeName: '',
      env: '',
      programParams: '',
      exeVersion: '',
    },
  },
}

export const mainSlice = createSlice({
  name: 'main',
  initialState: defaultState,
  reducers: {
    initPanel(state, action: PayloadAction<{ data: MainState['panel'] }>) {
      state.panel = action.payload.data
    },
    launching(
      state,
      action: PayloadAction<{
        data: {
          type: 'local' | 'cloud'
          progressList: number[]
        }
      }>
    ) {
      const { type, progressList } = action.payload.data
      if (type === 'local') {
        state.panel.localStatus.status = 'launching'
        state.panel.localStatus.progressList = progressList
      } else {
        state.panel.cloudStatus.status = 'launching'
        state.panel.cloudStatus.progressList = progressList
      }
    },
    launchSuccessed(
      state,
      action: PayloadAction<{
        data: {
          type: 'local' | 'cloud'
        }
      }>
    ) {
      const { type } = action.payload.data
      if (type === 'local') {
        state.panel.localStatus.status = 'launchSuccessed'
      } else {
        state.panel.cloudStatus.status = 'launchSuccessed'
      }
    },
    launchFailed(
      state,
      action: PayloadAction<{
        data: {
          type: 'local' | 'cloud'
          msg: string
        }
      }>
    ) {
      const { type, msg } = action.payload.data
      if (type === 'local') {
        state.panel.localStatus.status = 'launchFailed'
        state.panel.localStatus.statusMsg = msg
      } else {
        state.panel.cloudStatus.status = 'launchFailed'
        state.panel.cloudStatus.statusMsg = msg
      }
    },
    launchIdle(
      state,
      action: PayloadAction<{ data: { type: 'local' | 'cloud' } }>
    ) {
      const { type } = action.payload.data
      if (type === 'local') {
        state.panel.localStatus.status = 'idle'
        state.panel.localStatus.progressList = []
        state.panel.localStatus.statusMsg = ''
      } else {
        state.panel.cloudStatus.status = 'idle'
        state.panel.cloudStatus.progressList = []
        state.panel.cloudStatus.statusMsg = ''
      }
    },
  },
})

export const useMainSelector = createScopeSelectorHook('main')
