import request from '@/utils/request'
import {
  add,
  update,
  del,
  messageSend
} from './base'
import { ThirdPartyDevice } from '@/constant'

export function bind (deviceId, deviceType, thirdPartyDeviceId) {
  return messageSend({
    url: '/device/bind',
    method: 'POST',
    data: {
      deviceId,
      deviceType,
      thirdPartyDeviceId
    }
  }, '绑定')
}

export function unbind (id) {
  return messageSend({
    url: `/device/bind/${id}`,
    method: 'DELETE'
  }, '解绑')
}

export function getThirdPartyDeviceTypes () {
  return request({
    url: '/device/bind/thirdPartyType',
    method: 'GET'
  })
}

export function getThirdPartyDevices (query) {
  const { pageNum: pageIndex, pageSize, ...params } = query
  return request({
    url: '/device/bind/thirdPartyDevice',
    method: 'GET',
    params: {
      pageIndex, pageSize,
      ...params
    }
  })
}

export function getBoundThirdPartyDevices (deviceId, chain = false) {
  return request({
    url: `/device/bind/${deviceId}`,
    method: 'GET',
    params: { chain }
  })
}

export function getSensors (options) {
  return request({
    url: '/device/sensorType',
    method: 'GET',
    ...options
  })
}

export function getReceivingCardManufacturers () {
  return request({
    url: '/device/manufacturer/list',
    method: 'GET'
  })
}

export function addReceivingCard (fromData, onUploadProgress) {
  return request({
    url: `/device/information`,
    method: 'POST',
    timeout: 0,
    data: fromData,
    onUploadProgress
  })
}

export function updateReceivingCard (fromData, onUploadProgress) {
  return update({
    url: `/device/information`,
    method: 'PUT',
    timeout: 0,
    data: fromData,
    onUploadProgress
  })
}

export function getReceivingCard (id) {
  return request({
    url: `/device/information/${id}`,
    method: 'GET'
  })
}

export function getReceivingCardTopology (id, options) {
  return request({
    url: `/device/topology/${id}`,
    method: 'GET',
    ...options
  })
}

export function getSendingCardManufacturers () {
  return request({
    url: '/device/thirdPartySendingCard/manufacturer',
    method: 'GET'
  })
}

export function getSendingCards (query) {
  const { pageNum: pageIndex, pageSize, ...params } = query
  return request({
    url: '/device/thirdPartySendingCard/list',
    method: 'GET',
    params: {
      pageIndex, pageSize,
      ...params
    }
  })
}

export function addSendingCard (data) {
  return add({
    url: '/device/thirdPartySendingCard',
    method: 'POST',
    data
  })
}

export function updateSendingCard (data) {
  return update({
    url: '/device/thirdPartySendingCard',
    method: 'PUT',
    data
  })
}

export function deleteSendingCard ({ id, name }) {
  return del({
    url: `/device/thirdPartySendingCard/${id}`,
    method: 'DELETE'
  }, name)
}

export function getSendingCard (deviceId) {
  return request({
    url: `/device/bind/thirdPartySendingCard/${deviceId}`,
    method: 'GET'
  }).then(({ data }) => data?.[0])
}

export function bindSendingCard (deviceId, thirdPartyDeviceId) {
  return bind(deviceId, ThirdPartyDevice.SENDING_CARD, thirdPartyDeviceId)
}

export function getContentProtection (deviceId, options) {
  return request({
    url: `/device/contentProtect/${deviceId}`,
    method: 'GET',
    ...options
  })
}

const contentProtectConfig = {
  x: 1, y: 1,
  inputCards: [
    { cardId: 1, srcId: 0 }
  ],
  colors: [
    { order: 0, r: 0, g: 0, b: 0 },
    { order: 1, r: 0, g: 0, b: 0 },
    { order: 2, r: 0, g: 0, b: 0 },
    { order: 3, r: 0, g: 0, b: 0 },
    { order: 4, r: 0, g: 0, b: 0 },
    { order: 5, r: 0, g: 0, b: 0 }
  ]
}

export function updateContentProtection (deviceId, protect) {
  return messageSend({
    url: '/device/contentProtect',
    method: 'POST',
    data: {
      deviceId, protect,
      ...contentProtectConfig
    }
  }, protect ? '开启' : '关闭')
}

export function getCameras (query) {
  const { deviceId, ...params } = query
  return request({
    url: `/device/bind/thirdPartyCamera/${deviceId}`,
    method: 'GET',
    params
  })
}
