import { message } from 'ant-design-vue';
import {
  postPayloadAuth,
  postPayloadCommands,
  PayloadCommandsEnum,
  PostCameraModeBody,
  PostCameraFocalLengthBody,
  PostGimbalResetBody,
  PostCameraAimBody,
} from '/@/api/drone-control/payload';
import { changeLivestreamLens } from '/@/api/manage';
import { ControlSource } from '/@/types/device';
import { VideoType } from '/@/types/live-stream';

export function usePayloadControl() {
  function checkPayloadAuth(controlSource?: ControlSource) {
    if (controlSource !== ControlSource.A) {
      // message.error('请先获取有效的控制权限')
      return false;
    }
    return true;
  }

  async function authPayload(sn: string, payloadIndx: string) {
    const { code } = await postPayloadAuth(sn, {
      payload_index: payloadIndx
    })
    if (code === 0) {
      return true;
    }
    return false;
  }

  async function changeLiveLens(data: { video_id: string, video_type: 'wide' | 'zoom' }) {
    const { code } = await changeLivestreamLens(data);
    if (code === 0) {

    }
  }

  async function resetGimbal(sn: string, data: PostGimbalResetBody) {
    const { code } = await postPayloadCommands(sn, {
      cmd: PayloadCommandsEnum.GimbalReset,
      data: data
    })
    if (code === 0) {

    }
  }

  async function switchCameraMode(sn: string, data: PostCameraModeBody) {
    const { code } = await postPayloadCommands(sn, {
      cmd: PayloadCommandsEnum.CameraModeSwitch,
      data: data
    })
    if (code === 0) {

    }
  }

  async function startTakeCameraPhoto(sn: string, payloadIndx: string) {
    const { code } = await postPayloadCommands(sn, {
      cmd: PayloadCommandsEnum.CameraPhotoTakeStart,
      data: {
        payload_index: payloadIndx
      }
    })
    if (code === 0) {
      message.success('开始拍照')
      return true;
    }
    return false;
  }

  async function stopTakeCameraPhoto(sn: string, payloadIndx: string) {
    const { code } = await postPayloadCommands(sn, {
      cmd: PayloadCommandsEnum.CameraPhotoTakeStop,
      data: {
        payload_index: payloadIndx
      }
    })
    if (code === 0) {
      message.success('结束拍照')
      return true;
    }
    return false;
  }

  async function startCameraRecording(sn: string, payloadIndx: string) {
    const { code } = await postPayloadCommands(sn, {
      cmd: PayloadCommandsEnum.CameraRecordingStart,
      data: {
        payload_index: payloadIndx
      }
    })
    if (code === 0) {
      message.success('开始录像')
      return true;
    }
    return false;
  }

  async function stopCameraRecording(sn: string, payloadIndx: string) {
    const { code } = await postPayloadCommands(sn, {
      cmd: PayloadCommandsEnum.CameraRecordingStop,
      data: {
        payload_index: payloadIndx
      }
    })
    if (code === 0) {
      message.success('结束录像')
      return true;
    }
    return false;
  }

  async function changeCameraFocalLength(sn: string, data: PostCameraFocalLengthBody) {
    const { code } = await postPayloadCommands(sn, {
      cmd: PayloadCommandsEnum.CameraFocalLengthSet,
      data: data,
    })
    if (code === 0) {
      // message.success('变焦成功')
    }
  }

  async function cameraAim(sn: string, data: PostCameraAimBody) {
    const { code } = await postPayloadCommands(sn, {
      cmd: PayloadCommandsEnum.CameraAim,
      data: data,
    })
    if (code === 0) {
      message.success('Zoom Aim successfully')
    }
  }

  return {
    checkPayloadAuth,
    authPayload,
    changeLiveLens,
    resetGimbal,
    switchCameraMode,
    startTakeCameraPhoto,
    stopTakeCameraPhoto,
    startCameraRecording,
    stopCameraRecording,
    changeCameraFocalLength,
    cameraAim,
  }
}
