import request, { tenantRequest } from '@/utils/request'
import {
  add,
  update,
  del,
  confirm,
  messageSend,
  send,
  confirmAndSend,
  addTenant,
  addScope,
  addUser
} from './base'

export function getRatiosWithUser () {
  return tenantRequest({
    url: '/device/resolutionRatio',
    method: 'GET',
    params: addScope({})
  }).then(({ data }) => {
    return {
      data: Object.keys(data).map(key => {
        return {
          value: key,
          label: `${key} ${data[key].map(device => device.name).join(', ')}`
        }
      })
    }
  })
}

export function getRatios () {
  return tenantRequest({
    url: '/device/resolutionRatio',
    method: 'GET',
    params: addTenant({})
  }).then(({ data }) => {
    return {
      data: Object.keys(data).map(key => {
        return {
          value: key,
          label: key
        }
      })
    }
  })
}

export function getTimeline (deviceId, options) {
  return request({
    url: `/content/deviceCalender/${deviceId}`,
    method: 'GET',
    ...options
  }).then(({ data }) => JSON.parse(data.eventDetail) || [])
}

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

export function updateDevice (data) {
  return update({
    url: '/device',
    method: 'put',
    data
  })
}

export function deleteDevice ({ id, name }) {
  return send({
    url: `/device/${id}/standbyDevice`,
    method: 'GET',
    params: { pageNum: 1, pageSize: 1 }
  }).then(({ data }) => confirm(
    data.length
      ? `删除主设备后备份设备也将删除`
      : `删除 ${name} ？`,
    data.length
      ? `删除 ${name}`
      : null
  )).then(() => messageSend({
    url: `/device/${id}`,
    method: 'DELETE'
  }, '删除'))
}

export function getDevices (query, options) {
  const { pageNum: pageIndex, pageSize, ...params } = query
  return tenantRequest({
    url: '/device/relation/page',
    method: 'GET',
    params: addScope({
      pageIndex, pageSize,
      ...params
    }),
    ...options
  })
}

export function getDevicesByAdmin (query) {
  const { pageNum: pageIndex, pageSize, ...params } = query
  return tenantRequest({
    url: '/device/relation/page',
    method: 'GET',
    params: {
      pageIndex, pageSize,
      ...params
    }
  })
}

export function getDevicesByRelation (query) {
  const { pageNum: pageIndex, pageSize, ...params } = query
  return tenantRequest({
    url: '/device/queryRelation/page',
    method: 'GET',
    params: {
      pageIndex, pageSize,
      ...params
    }
  })
}

export function bindDeviceToObject (deviceId, org, user) {
  return request({
    url: `/device/relation/${deviceId}/save`,
    method: 'POST',
    data: {
      org,
      user
    }
  })
}

export function unbindDevice ({ deviceRelaId, name }) {
  return confirmAndSend('移除', name, {
    url: `/device/relation/${deviceRelaId}/remove`,
    method: 'DELETE'
  })
}

export function unbindDevices (params) {
  return request({
    url: '/device/relation/batchRemove',
    method: 'DELETE',
    params
  })
}

export function updateBindDevices (user, org) {
  return send({
    url: '/device/relation/update',
    method: 'POST',
    params: { user },
    data: { org }
  })
}

export function activateDevice ({ id, name }) {
  return confirmAndSend('激活', name, {
    url: '/device/batch/activate',
    method: 'put',
    data: [id]
  })
}

export function deactivateDevice ({ id, name }) {
  return confirmAndSend('停用', name, {
    url: '/device/batch/deactivate',
    method: 'put',
    data: [id]
  })
}

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

export function getSubDevices (query) {
  const { id, pageNum: pageIndex, pageSize, ...params } = query
  return request({
    url: `/device/${id}/standbyDevice`,
    method: 'GET',
    params: {
      pageIndex, pageSize,
      ...params
    }
  })
}

export function addSubDevice ({ id }, data) {
  return add({
    url: `/device/${id}/addStandby`,
    method: 'POST',
    data
  })
}

export function addProductType (data) {
  return add({
    url: '/productType',
    method: 'POST',
    data
  })
}

export function updateProductType (data) {
  return update({
    url: '/productType',
    method: 'put',
    data
  })
}

export function deleteProductType ({ id, name }) {
  return del({
    url: `/productType/${id}`,
    method: 'DELETE'
  }, name)
}

export function getProductTypes (query) {
  const { pageNum: pageIndex, pageSize, ...params } = query
  return tenantRequest({
    url: '/productType/list',
    method: 'GET',
    params: addTenant({
      pageIndex, pageSize,
      ...params
    })
  })
}

export function addProduct (data) {
  return add({
    url: '/product',
    method: 'POST',
    data
  })
}

export function updateProduct (data) {
  return update({
    url: '/product',
    method: 'put',
    data
  })
}

export function deleteProduct ({ id, name }) {
  return del({
    url: `/product/${id}`,
    method: 'DELETE'
  }, name)
}

export function getProducts (query) {
  const { pageNum: pageIndex, pageSize, ...params } = query
  return tenantRequest({
    url: '/product/list',
    method: 'GET',
    params: addTenant({
      pageIndex, pageSize,
      ...params
    })
  })
}

export function addDeviceGroup (data) {
  return add({
    url: '/deviceGroup',
    method: 'POST',
    data
  })
}

export function updateDeviceGroup (data) {
  return update({
    url: '/deviceGroup',
    method: 'PUT',
    data
  })
}

export function deleteDeviceGroup ({ id, name }) {
  return del({
    url: `/deviceGroup/${id}`,
    method: 'DELETE'
  }, name)
}

export function getDeviceGroups (query) {
  const { pageNum: pageIndex, pageSize, ...params } = query
  return tenantRequest({
    url: '/deviceGroup/list',
    method: 'GET',
    params: addUser({
      pageIndex, pageSize,
      ...params
    })
  })
}

export function getDevicesByGroup (id) {
  return tenantRequest({
    url: `/deviceGroup/${id}/device`,
    method: 'GET',
    params: addScope({})
  })
}

export function getDeviceTree () {
  return tenantRequest({
    url: '/deviceGroup/deviceTree',
    method: 'GET',
    params: addUser(addScope({}))
  })
}

export function addDeviceToGroup (id, deviceId) {
  return add({
    url: `/deviceGroup/${id}/device`,
    method: 'POST',
    data: [].concat(deviceId)
  })
}

export function deleteDeviceFromGroup (id, { id: deviceId, name }) {
  return confirmAndSend('移除', name, {
    url: `/deviceGroup/${id}/device`,
    method: 'DELETE',
    params: { deviceId }
  })
}

export function getDeviceStatistics (productId) {
  return tenantRequest({
    url: '/device/listDeviceTotal',
    method: 'GET',
    params: addScope({ productId })
  })
}

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

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

export function addTask (task) {
  return add({
    url: '/device/functionTask',
    method: 'POST',
    data: task
  })
}

export function updateTask (task) {
  return update({
    url: '/device/functionTask',
    method: 'PUT',
    data: task
  })
}

export function deleteTask ({ taskId }) {
  return del({
    url: `/device/functionTask/${taskId}`,
    method: 'DELETE'
  })
}

export function activateTask ({ taskId }) {
  return confirmAndSend('启用', null, {
    url: '/device/functionTask/resume',
    method: 'PUT',
    data: [taskId]
  })
}

export function deactivateTask ({ taskId }) {
  return confirmAndSend('停用', null, {
    url: '/device/functionTask/pause',
    method: 'PUT',
    data: [taskId]
  })
}
