import { addForwardRule, addLink, delForward, delLink, forwardDebug, getForwardById, getForwardCount, getForwardRulePage, getLinkCount, getLinkPage, getLinkPageById, linkDebug, updateForward, updateForwardStatus, updateLink, updateLinkStatus } from '@/api/iot/rule'
import { SubjectEventEnum, SubjectResourceEnum } from '@/enum/common'
import { message } from 'ant-design-vue'

export class RuleController {
  static SubjectResourceMap = new Map([
    [SubjectResourceEnum.DEVICE, { name: '设备', key: SubjectResourceEnum.DEVICE }],
    [SubjectResourceEnum.DEVICE_ATTRIBUTE, { name: '设备属性', key: SubjectResourceEnum.DEVICE_ATTRIBUTE }],
    [SubjectResourceEnum.DEVICE_STATUS, { name: '设备状态', key: SubjectResourceEnum.DEVICE_STATUS }],
    [SubjectResourceEnum.MODEL, { name: '模型', key: SubjectResourceEnum.MODEL }]
  ])

  static SubjectEventMap = new Map([
    [SubjectEventEnum.DEVICE_CREATE, { name: '设备创建', key: SubjectEventEnum.DEVICE_CREATE }],
    [SubjectEventEnum.DEVICE_DELETE, { name: '设备删除', key: SubjectEventEnum.DEVICE_DELETE }],
    [SubjectEventEnum.DEVICE_ATTRIBUTE_REPORT, { name: '设备属性上报', key: SubjectEventEnum.DEVICE_ATTRIBUTE_REPORT }],
    [SubjectEventEnum.DEVICE_STATUS_UPDATE, { name: '设备状态变更', key: SubjectEventEnum.DEVICE_STATUS_UPDATE }],
    [SubjectEventEnum.MODEL_CREATE, { name: '模型创建', key: SubjectEventEnum.MODEL_CREATE }],
    [SubjectEventEnum.MODEL_DELETE, { name: '模型删除', key: SubjectEventEnum.MODEL_DELETE }]
  ])

  static forwardStatisticMap = new Map([
    ['TOTAL', { label: '总数', color: 'green' }],
    ['DISABLE', { label: '禁用数', color: 'grey' }],
    ['ENABLE', { label: '开始数', color: 'orange' }]
  ])

  static linkStatisticMap = new Map([
    ['TOTAL', { label: '总数', color: 'green' }],
    ['DISABLE', { label: '禁用数', color: 'grey' }],
    ['ENABLE', { label: '开始数', color: 'orange' }]
  ])

  /** 转发规则 分页获取转发规则 */
  static async pageForward (params: IOT.API.RULE.ForwardRuleQueryParams) {
    return await getForwardRulePage(params)
  }

  /** 转发规则 新增 */
  static async postForward (data: IOT.API.RULE.ForwardRuleBody) {
    await addForwardRule(data)
    message.success('新增转发规则成功')
  }

  static async delForwardRule (id: string) {
    await delForward(id)
    message.success('删除成功')
  }

  static async forwardById (id: string) {
    return await getForwardById(id)
  }

  static async updateForward (data: any) {
    await updateForward(data)
    message.success('编辑成功')
  }

  static async forwardCount () {
    return await getForwardCount()
  }

  static async updateForwardStatus (params: { id: string, status: boolean}) {
    await updateForwardStatus(params)
    params.status ? message.success('规则已启动') : message.success('规则已停止')
  }

  /** 联动规则 分页 */
  static async pageLink (params: {page: number, pageSize: number, ruleLabel: string}) {
    return await getLinkPage(params)
  }

  static async updateLinkStatus (params: { id: string, status: boolean}) {
    await updateLinkStatus(params)
    params.status ? message.success('规则已启动') : message.success('规则已停止')
  }

  static async delLink (id: string) {
    await delLink(id)
    message.success('删除成功')
  }

  static async updateLink (data: any) {
    await updateLink(data)
    message.success('修改成功')
  }

  static async addLink (data: any) {
    await addLink(data)
    message.success('新增成功')
  }

  static async getLinkById (id: string) {
    return await getLinkPageById(id)
  }

  static async linkCount () {
    return await getLinkCount()
  }

  static async forwardDebug (data: any) {
    await forwardDebug(data)
    message.success('调试成功')
  }

  static async linkDebug (data: any) {
    await linkDebug(data)
    message.success('调试成功')
  }
}
