import { createSlice, type PayloadAction } from '@reduxjs/toolkit'

/**
 * 连接状态
 */
export type ConnectionStatus = 'disconnected' | 'connecting' | 'connected' | 'error'

/**
 * 网络质量
 */
export interface NetworkQuality {
  latency: number
  bandwidth: number
  packetLoss: number
  quality: 'excellent' | 'good' | 'fair' | 'poor'
}

/**
 * 连接状态管理
 */
interface ConnectionState {
  status: ConnectionStatus
  webSocket: any | null
  serverUrl: string
  isReconnecting: boolean
  reconnectAttempts: number
  maxReconnectAttempts: number
  networkQuality: NetworkQuality | null
  lastConnectedAt: number | null
  error: string | null
}

const initialState: ConnectionState = {
  status: 'disconnected',
  webSocket: null,
  serverUrl: '',
  isReconnecting: false,
  reconnectAttempts: 0,
  maxReconnectAttempts: 5,
  networkQuality: null,
  lastConnectedAt: null,
  error: null,
}

/**
 * 连接管理 Slice
 */
const connectionSlice = createSlice({
  name: 'connection',
  initialState,
  reducers: {
    // 设置连接状态
    setConnectionStatus: (state, action: PayloadAction<ConnectionStatus>) => {
      state.status = action.payload
      
      if (action.payload === 'connected') {
        state.lastConnectedAt = Date.now()
        state.reconnectAttempts = 0
        state.isReconnecting = false
        state.error = null
      }
    },
    
    // 设置WebSocket实例
    setWebSocket: (state, action: PayloadAction<any | null>) => {
      state.webSocket = action.payload
    },
    
    // 设置服务器URL
    setServerUrl: (state, action: PayloadAction<string>) => {
      state.serverUrl = action.payload
    },
    
    // 开始重连
    startReconnecting: (state) => {
      state.isReconnecting = true
      state.status = 'connecting'
    },
    
    // 停止重连
    stopReconnecting: (state) => {
      state.isReconnecting = false
      state.reconnectAttempts = 0
    },
    
    // 增加重连次数
    incrementReconnectAttempts: (state) => {
      state.reconnectAttempts += 1
    },
    
    // 重置重连次数
    resetReconnectAttempts: (state) => {
      state.reconnectAttempts = 0
    },
    
    // 更新网络质量
    updateNetworkQuality: (state, action: PayloadAction<NetworkQuality>) => {
      state.networkQuality = action.payload
    },
    
    // 设置连接错误
    setConnectionError: (state, action: PayloadAction<string>) => {
      state.error = action.payload
      state.status = 'error'
      state.isReconnecting = false
    },
    
    // 清除连接错误
    clearConnectionError: (state) => {
      state.error = null
    },
    
    // 重置连接状态
    resetConnection: (state) => {
      state.status = 'disconnected'
      state.webSocket = null
      state.isReconnecting = false
      state.reconnectAttempts = 0
      state.networkQuality = null
      state.lastConnectedAt = null
      state.error = null
    },
  },
})

export const {
  setConnectionStatus,
  setWebSocket,
  setServerUrl,
  startReconnecting,
  stopReconnecting,
  incrementReconnectAttempts,
  resetReconnectAttempts,
  updateNetworkQuality,
  setConnectionError,
  clearConnectionError,
  resetConnection,
} = connectionSlice.actions

export default connectionSlice.reducer 