import { get, post } from '@/utils/request'
import type {
  IBaseResponse,
  IListResponse,
  IPageResponse,
  BaseResponse,
  CameraDTO,
  ChannelDTO,
  CameraInfoDTO,
  CameraAddRequest,
  CameraUpdateRequest,
  CameraListRequest,
  SwitchChannelRequest,
  PTZAction,
  PTZControlRequest,
  AdminPTZRequest
} from '@/types'

// ==================== Controller APIs (MVP) ====================

// 获取摄像头列表 (POST)
export function listCameras(machineId?: string): Promise<IListResponse<CameraDTO>> {
  return post('/camera/list', machineId ? { machineId } : {})
}

// 获取摄像头信息
export function getCamera(cameraId: string): Promise<IBaseResponse<CameraDTO>> {
  return get(`/camera/${cameraId}`)
}

// 切换摄像头通道 (MVP核心)
export function switchChannel(data: SwitchChannelRequest): Promise<IBaseResponse<ChannelDTO>> {
  return post('/camera/switch', data)
}

// 获取当前活动通道
export function getCurrentChannel(machineId: string): Promise<IBaseResponse<ChannelDTO>> {
  return get('/camera/current', { machineId })
}

// 开始PTZ控制 (后台专用)
export function ptzStart(cameraId: string, action: PTZAction, speed: number = 50): Promise<BaseResponse> {
  return post(`/camera/${cameraId}/ptz/start`, undefined, {
    params: { action, speed }
  })
}

// 停止PTZ控制 (后台专用)
export function ptzStop(cameraId: string): Promise<BaseResponse> {
  return post(`/camera/${cameraId}/ptz/stop`)
}

// PTZ 直接控制 (pan/tilt/zoom 方式)
export function ptzDirectControl(cameraId: string, data: PTZControlRequest): Promise<BaseResponse> {
  return post(`/camera/${cameraId}/ptz/control`, data)
}

// ==================== Admin APIs ====================

// 获取摄像头列表 (管理后台，分页)
export function adminListCameras(params?: CameraListRequest): Promise<IPageResponse<CameraInfoDTO>> {
  return post('/admin/cameras/list', params || {})
}

// 获取摄像头列表 (全部，不分页)
export function adminListAllCameras(machineId?: string): Promise<IListResponse<CameraInfoDTO>> {
  return get('/admin/cameras/listAll', machineId ? { machineId } : undefined)
}

// 获取摄像头详情
export function adminGetCamera(id: number): Promise<IBaseResponse<CameraInfoDTO>> {
  return get('/admin/cameras/detail', { id })
}

// 添加摄像头
export function adminAddCamera(data: CameraAddRequest): Promise<IBaseResponse<CameraInfoDTO>> {
  return post('/admin/cameras/add', data)
}

// 更新摄像头
export function adminUpdateCamera(data: CameraUpdateRequest): Promise<IBaseResponse<CameraInfoDTO>> {
  return post('/admin/cameras/update', data)
}

// 删除摄像头
export function adminDeleteCamera(id: number): Promise<BaseResponse> {
  return post('/admin/cameras/delete', undefined, {
    params: { id }
  })
}

// 检测摄像头连通性
export function adminCheckCamera(id: number): Promise<IBaseResponse<boolean>> {
  return post('/admin/cameras/check', undefined, {
    params: { id }
  })
}

// 获取摄像头快照
export function adminGetSnapshot(id: number): Promise<IBaseResponse<Blob>> {
  return get('/admin/cameras/snapshot', { id }, { responseType: 'blob' })
}

// PTZ 控制 (Admin)
export function adminPTZControl(data: AdminPTZRequest): Promise<IBaseResponse<boolean>> {
  return post('/admin/cameras/ptz', data)
}

// ==================== 兼容旧代码的别名 ====================

// 获取设备列表 (兼容 - 使用不分页接口)
export const listDevice = adminListAllCameras

// 获取设备详情 (兼容)
export const getDevice = (deviceId: string) => getCamera(deviceId)

// 添加设备 (兼容)
export const addDevice = adminAddCamera

// 修改设备 (兼容)
export const updateDevice = adminUpdateCamera

// 删除设备 (兼容)
export const delDevice = (id: number) => adminDeleteCamera(id)

// PTZ控制 (兼容旧API)
export function ptzControl(
  _deviceId: string,
  channelId: string,
  command: string,
  horizonSpeed?: number,
  _verticalSpeed?: number,
  _zoomSpeed?: number
): Promise<BaseResponse> {
  // 映射旧的命令到新的 action
  const actionMap: Record<string, PTZAction> = {
    up: 'up',
    down: 'down',
    left: 'left',
    right: 'right',
    zoomin: 'zoom_in',
    zoomout: 'zoom_out',
    stop: 'stop'
  }
  const action = actionMap[command.toLowerCase()] || 'stop'
  const speed = horizonSpeed || 50

  // 使用 channelId 关联的 cameraId 进行 PTZ 控制
  return ptzStart(channelId, action, speed)
}
