import { closeStream, createRtmpPull, createRtmpPush, createRtspPull, createRtspPush, getConfig, getPullList, getPushList, getRecording, getRecordList, getStreams, getSummary, playRecord, postConfig, postRecord, stopPull, stopPush, stopRecord, updateConfig, controlGB28181, getGB28181Device, getGB28181Record } from '@/api/rts/stream'
import { message } from 'ant-design-vue'

export class RtsController {
  static StateMap = new Map([
    [0, { name: '等待发布', key: 0 }],
    [1, { name: '发布中', key: 1 }],
    [2, { name: '等待关闭', key: 2 }],
    [3, { name: '已经关闭', key: 3 }]
  ])

  static protocolList = ['RTSP', 'RTMP', 'HLS', 'HDL', 'GB28181']

  static async getStreams () {
    return getStreams()
  }

  static async cloeStreams (streamPath: string) {
    await closeStream(streamPath)
    message.success('关闭成功')
  }

  static async listPullList () {
    return await getPullList()
  }

  static async stopPull (remoteurl: string) {
    await stopPull(remoteurl)
    message.success('停止拉流成功')
  }

  static async createPull (params: {streamPath: string, target: string, save: number, name: 'RTSP' | 'RTMP'}) {
    if (params.name === 'RTMP') {
      await createRtmpPull(params)
    } else {
      await createRtspPull(params)
    }
    message.success('新增成功')
  }

  static async listPush () {
    return await getPushList()
  }

  static async stopPush (remoteurl: string) {
    await stopPush(remoteurl)
    message.success('停止推流成功')
  }

  static async createPush (params: {streamPath: string, target: string, save: number, name: 'RTSP' | 'RTMP'}) {
    if (params.name === 'RTSP') {
      await createRtspPush(params)
    } else {
      await createRtmpPush(params)
    }
    message.success('新增成功')
  }

  static async listRecord (type: 'mp4' | 'ts' | 'flv') {
    return await getRecordList(type)
  }

  static async listRecording () {
    return await getRecording()
  }

  /** 开始录制 */
  static async recordVideo (params: {type: 'mp4' | 'ts' | 'flv', streamPath: string}) {
    await postRecord(params)
    message.success('录制成功')
  }

  static async playRecord (id: string, format: 'flv'| 'mp4' | 'm3u8' | 'h264'| 'h265') {
    return await playRecord(id, format)
  }

  static async stopRecord (id: string) {
    await stopRecord(id)
    message.success('停止录制成功')
  }

  static async protocol (name: 'RTSP' | 'RTMP' | 'HLS' | 'HDL' | 'GB28181') {
    return await getConfig(name)
  }

  static async postProtocol (name: 'RTSP' | 'RTMP' | 'HLS' | 'HDL' | 'GB28181', data: string) {
    await postConfig(name, data)
    message.success('修改协议成功')
  }

  static async updateProtocol (name: 'RTSP' | 'RTMP' | 'HLS' | 'HDL' | 'GB28181') {
    await updateConfig(name)
    message.success('更新成功')
  }

  static async summary () {
    return await getSummary()
  }

  static async listGB28181 () {
    return await getGB28181Device()
  }

  static async getGB28181Record (params: {id: string, channel: string, startTime:string, endTime:string}) {
    return await getGB28181Record(params)
  }

  static async controlGB28181 (params: {id: string, channel: string, ptzcmd:string}) {
    return await controlGB28181(params)
  }
}
