import { addDevice, addSpace, delSpace, getDeviceAiRetById, getDeviceById, getDevicePage, getDeviceRecordById, getDeviceThumbById, getSpace, getSpaceById, getSpaceConfigById, getSpaceList, updateSpace, updateStatus } from '@/api/cvs/video'
import { message } from 'ant-design-vue'

export class SpaceController {
  static type = [
    { key: 'RTMP', value: 'RTMP', label: 'RTMP' },
    { key: 'GB28181', value: 'GB28181', label: 'GB28181' },
    { key: 'RTSP', value: 'RTSP', label: 'RTSP' }
  ]

  static area = [
    { key: '华北', value: '华北', label: '华北' },
    { key: '华南', value: '华南', label: '华南' },
    { key: '华东', value: '华东', label: '华东' },
    { key: '华中', value: '华中', label: '华中' }
  ]

  static statusMap = new Map([
    ['RUNNING', { key: 'RUNNING', label: '运行中', color: 'green' }],
    ['STOPPED', { key: 'STOPPED', label: '已停止', color: 'red' }]
  ])

  static deviceStatusMap = new Map([
    ['ONLINE', { key: 'ONLINE', label: '在线', color: 'green' }],
    ['OFFLINE', { key: 'OFFLINE', label: '离线', color: 'grey' }],
    ['ERROR', { key: 'ERROR', label: '异常', color: 'red' }],
    ['PUSHING', { key: 'PUSHING', label: '推流中', color: 'blue' }],
    ['PULLING', { key: 'PULLING', label: '拉流中', color: 'yellow' }]
  ])

  static async page (params: COMMON.API.QueryParams) {
    return await getSpace(params)
  }

  static async list () {
    const { code, data } = await getSpaceList('', 200)
    return data
  }

  static async add (data: Partial<CVS.space>) {
    const { code, msg } = await addSpace(data)
    code === 200 ? message.success('新增成功') : message.error(msg)
    return code === 200
  }

  static async update (data: CVS.space) {
    const { code, msg } = await updateSpace(data)
    code === 200 ? message.success('编辑成功') : message.error(msg)
  }

  static async del (id: string) {
    const { code, msg } = await delSpace(id)
    code === 200 ? message.success('删除成功') : message.error(msg)
  }

  static async updateStatue (spaceId, status) {
    const { code, msg } = await updateStatus(spaceId, status)
    code === 200 ? message.success('修改成功') : message.error(msg)
  }

  static async byId (spaceId: string) {
    const { code, data } = await getSpaceById(spaceId)
    return data
  }

  static async spaceConfigById (spaceId: string) {
    const { code, data } = await getSpaceConfigById(spaceId)
    return data
  }

  static async devicePage (params: COMMON.API.QueryParams & {deviceName: string}) {
    return await getDevicePage(params)
  }

  static async deviceById (id: string) {
    const { code, data } = await getDeviceById(id)
    if (code === 200) {
      return data
    }
  }

  static async addDevice (data: Partial<CVS.device>) {
    const { code, msg } = await addDevice(data)
    code === 200 ? message.success('新增成功') : message.error(msg)
  }

  static async deviceRecordById (
    params: {
      deviceId: string
      startTs?: number,
      endTs?: number,
      recordFormat: 'FLV' | 'TS' | 'MP4' | 'RAW'
    }
  ) {
    const { code, data } = await getDeviceRecordById(params)
    if (code === 200) {
      return data
    }
  }

  static async deviceThumbById (
    params: {
      deviceId: string
      startTs?: number,
      endTs?: number
    }
  ) {
    const { code, data } = await getDeviceThumbById(params)
    if (code === 200) {
      return data
    }
  }

  static async deviceAiRetById (
    params: {
      deviceId: string
      startTs?: number,
      endTs?: number
      aiId?: string
    }
  ) {
    const { code, data } = await getDeviceAiRetById(params)
    if (code === 200) {
      return data
    }
  }
}
