import { addModel, addModelPlugin, debugModelPlugin, delModel, delModelTemplate, getModel, getModelById, getModelCount, getModelList, getModelPlugin, getModelTemplate, getModelTemplateById, postModelTemplate, updateModelPlugin } from '@/api/iot/model'
import { message } from 'ant-design-vue'

export class ModelController {
  static forwardStatisticMap = new Map([
    ['MQTT', { label: 'MQTT', color: 'green' }],
    ['TOTAL', { label: '总数', color: 'grey' }],
    ['HTTP', { label: 'HTTP', color: 'orange' }],
    ['COAP', { label: 'COAP', color: 'skyblue' }]
  ])

  static async list (params: {modelLabel?: string, limit?: number, lastId?: string} = {
    modelLabel: '',
    limit: 20000,
    lastId: ''
  }) {
    return await getModelList(params)
  }

  static async page (params: IOT.API.MODEL.QueryPamars) {
    return await getModel(params)
  }

  static async post (data: IOT.API.MODEL.Model) {
    await addModel(data)
    message.success('新增成功')
  }

  static async count () {
    return await getModelCount()
  }

  static put () {

  }

  static async detail (id: string) {
    const { data } = await getModelById(id)
    return data
  }

  static async del (id: string) {
    const { data } = await delModel(id)
    if (data) {
      message.success('删除成功')
    }
  }

  static async getPlugin (params: {modelId: string}) {
    return await getModelPlugin(params)
  }

  static async postPlugin (data: {modelPluginType: string, modelPluginBody: string, modelId: string}) {
    await addModelPlugin(data)
    message.success('保存成功')
  }

  static async updatePlugin (data: {modelPluginType: string, modelPluginBody: string, modelId: string, id: string}) {
    await updateModelPlugin(data)
    message.success('保存成功')
  }

  static async debugPlugin (data: {input: string, pluginBody: string, decode: boolean}) {
    return await debugModelPlugin(data)
  }

  static async modelTemplate (params: {page: number, pageSize: number, templateLabel?: string}) {
    return getModelTemplate(params)
  }

  static async modelTemplateById (id: string) {
    return getModelTemplateById(id)
  }

  static async postModelTemplate (data: {modelId: string, templateLabel: string}) {
    await postModelTemplate(data)
    message.success('加入产品模型库列表成功')
  }

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