import Vue from 'vue'
import uniqueId from 'lodash'
import HelperUtil from '../utils/HelperUtil'
import {i18n} from '../main'
import StatusFilter from '@/commands/StatusFilter'
import AuthorityFilter from '../commands/AuthorityFilter'
import DeviceService from '@/domain/services/DeviceService'
import LinkService from '@/domain/services/LinkService'
import ConfigFileService from '../domain/services/ConfigFileService'
import { EventBus } from '@/domain/EventBus'
import store from '@/store.js'
// 设备
import CreateAndCopyDeviceDialog from '@/components/DialogComponents/DiviceDialog/CreateAndCopyDeviceDialog.vue'
import DiscoveryDeviceDialog from '@/components/DialogComponents/DiviceDialog/DiscoveryDeviceDialog.vue'
import DeleteDeviceDialog from '@/components/DialogComponents/DiviceDialog/DeleteDeviceDialog.vue'
import UpdateDeviceDialog from '@/components/DialogComponents/DiviceDialog/UpdateDeviceDialog.vue'
import ImportDeviceDialog from '@/components/DialogComponents/DiviceDialog/ImportDeviceDialog.vue'
import DeviceBookUpdateDialog from '@/components/DialogComponents/DiviceDialog/DeviceBookUpdateDialog.vue'
import DownloadConfigToDeviceDialog from '@/components/DialogComponents/ConfigDialog/DownloadConfigToDevice.vue'
import HistoryConfigToDeviceDialog from '@/components/DialogComponents/ConfigDialog/HistoryConfigToDevice.vue'
import DeviceDetailsDialog from '@/components/DialogComponents/DiviceDialog/DeviceDeteils.vue'
import DevicePanelDialog from '@/components/DialogComponents/DevicePanelDialog/DevicePanelDialog.vue'
import ServerDevicePanelDialog from '@/components/DialogComponents/DevicePanelDialog/ServerDevicePanelDialog.vue'
import DeviceAddToSubnetDialog from '@/components/DialogComponents/DiviceDialog/DeviceAddToSubnetDialog.vue'
import DeviceRemoveFromSubnetDialog from '@/components/DialogComponents/DiviceDialog/DeviceRemoveFromSubnetDialog.vue'
import DevicePortFlowDialog from '@/components/DialogComponents/DevicePanelDialog/DevicePortFlowDialog.vue'
import TurnToSwitchesDialog from '@/components/DialogComponents/SwitchesConfigurationDialog/TurnToSwitchesDialog.vue'
import TurnToIpmiDialog from '@/components/DialogComponents/SwitchesConfigurationDialog/TurnToIpmiDialog.vue'
import UpdateTradDeviceDialog from '@/components/DialogComponents/TradDeviceDialog/UpdateTradDeviceDialog.vue'
// 链路
import CreateAndCopyLinkDialog from '@/components/DialogComponents/LinkDialog/CreateLinkDialog.vue'
import DiscoveryLinkDialog from '@/components/DialogComponents/LinkDialog/DiscoveryLinkDialog.vue'
import DeleteLinkDialog from '@/components/DialogComponents/LinkDialog/DeleteLinkDialog.vue'
import UpdateLinkDialog from '@/components/DialogComponents/LinkDialog/UpdateLinkDialog.vue'
import ImportLinkDialog from '@/components/DialogComponents/LinkDialog/ImportLinkDialog.vue'
import {config} from "@vue/test-utils";

// 设备
Vue.component('CreateAndCopyDeviceDialog', CreateAndCopyDeviceDialog)
Vue.component('DeleteDeviceDialog', DeleteDeviceDialog)
Vue.component('UpdateDeviceDialog', UpdateDeviceDialog)
Vue.component('ImportDeviceDialog', ImportDeviceDialog)
Vue.component('DownloadConfigToDeviceDialog', DownloadConfigToDeviceDialog)
Vue.component('HistoryConfigToDeviceDialog', HistoryConfigToDeviceDialog)
Vue.component('DeviceDetailsDialog', DeviceDetailsDialog)
Vue.component('DevicePanelDialog', DevicePanelDialog)
Vue.component('ServerDevicePanelDialog', ServerDevicePanelDialog)
Vue.component('DeviceAddToSubnetDialog', DeviceAddToSubnetDialog)
Vue.component('DeviceRemoveFromSubnetDialog', DeviceRemoveFromSubnetDialog)
Vue.component('DevicePortFlowDialog', DevicePortFlowDialog)
Vue.component('TurnToSwitchesDialog', TurnToSwitchesDialog)
Vue.component('TurnToIpmiDialog', TurnToIpmiDialog)
Vue.component('UpdateTradDeviceDialog', UpdateTradDeviceDialog)
Vue.component('DeviceBookUpdateDialog', DeviceBookUpdateDialog)
Vue.component('DiscoveryDeviceDialog', DiscoveryDeviceDialog)
// 链路
Vue.component('CreateAndCopyLinkDialog', CreateAndCopyLinkDialog)
Vue.component('DeleteLinkDialog', DeleteLinkDialog)
Vue.component('UpdateLinkDialog', UpdateLinkDialog)
Vue.component('ImportLinkDialog', ImportLinkDialog)
Vue.component('DiscoveryLinkDialog', DiscoveryLinkDialog)

let CommandTypes = {
  // 设备
  CREATE_COPY_DEVICE_CMD: {
    type: 'CreateAndCopyDevice',
    executeComponent: 'CreateAndCopyDeviceDialog',
    registerComponent: CreateAndCopyDeviceDialog
  },
  DELETE_DEVICE_CMD: {
    type: 'DeleteDevice',
    executeComponent: 'DeleteDeviceDialog',
    registerComponent: DeleteDeviceDialog // import对应的对话框组件
  },
  UPDATE_DEVICE_CMD: {
    type: 'UpdateDevice',
    executeComponent: 'UpdateDeviceDialog',
    registerComponent: UpdateDeviceDialog // import对应的对话框组件
  },
  CLOUN_PLATFORM_CMD: {
    type: 'CloudPlatform',
    executeComponent: 'null',
    registerComponent: null
  },
  STORAGE_CMD: {
    type: 'Storage',
    executeComponent: 'null',
    registerComponent: null
  },
  IMPORT_DEVICE_CMD: {
    type: 'ImportDevice',
    executeComponent: 'ImportDeviceDialog',
    registerComponent: ImportDeviceDialog // import对应的对话框组件
  },
  DOWNLOAD_CONFIG_TO_DEVICE_CMD: {
    type: 'DownloadConfigToDevice',
    executeComponent: 'DownloadConfigToDeviceDialog',
    registerComponent: DownloadConfigToDeviceDialog // import对应的对话框组件
  },
  COPY_TO_SERVER_CMD: {
    type: 'CopyToServer',
    executeComponent: null,
    registerComponent: null
  },
  DOWNLOAD_TO_LOCAL: {
    type: 'DownloadToLocal',
    executeComponent: null,
    registerComponent: null
  },
  HISTORY_CONFIG_TO_DEVICE_CMD: {
    type: 'HistoryConfigToDevice',
    executeComponent: 'HistoryConfigToDeviceDialog',
    registerComponent: HistoryConfigToDeviceDialog // import对应的对话框组件
  },
  UP_DEVICE_CMD: {
    type: 'upDevice',
    executeComponent: 'UpDeviceFunction',
    registerComponent: null // import对应的对话框组件
  },
  DOWN_DEVICE_CMD: {
    type: 'downDevice',
    executeComponent: 'DownDeviceFunction',
    registerComponent: null // import对应的对话框组件
  },
  DEVICE_ADD_PLAN_CMD: {
    type: 'deviceAddPlanDevice',
    executeComponent: 'DeviceAddPlanDeviceFunction',
    registerComponent: null // import对应的对话框组件
  },
  RT_PERFORMANCE_CMD: {
    type: 'RtPerformance',
    executeComponent: 'RtPerformance',
    registerComponent: null // import对应的对话框组件
  },
  DOWNLOAD_DEVICE_LOG_CMD: {
    type: 'DownloadDeviceLog',
    executeComponent: 'DownloadDeviceLogFunction',
    registerComponent: null // import对应的对话框组件
  },
  SHOW_DEVICE_DETAIL_CMD: {
    type: 'DeviceDetailsDialog',
    executeComponent: 'DeviceDetailsDialog',
    registerComponent: DeviceDetailsDialog // import对应的对话框组件
  },
  SHOW_DEVICE_PANEL_CMD: {
    type: 'DevicePanel',
    executeComponent: 'DevicePanelDialog',
    registerComponent: DevicePanelDialog // import对应的对话框组件
  },
  SHOW_SERVER_DEVICE_PANEL_CMD: {
    type: 'ServerDevicePanel',
    executeComponent: 'ServerDevicePanelDialog',
    registerComponent: ServerDevicePanelDialog // import对应的对话框组件
  },
  TURN_TO_SWITCHES_CMD: {
    type: 'TurnToSwitches',
    executeComponent: 'TurnToSwitchesDialog',
    registerComponent: TurnToSwitchesDialog // import对应的对话框组件
  },
  TURN_TO_IPMI_CMD: {
    type: 'TurnToIpmi',
    executeComponent: 'TurnToIpmiDialog',
    registerComponent: TurnToIpmiDialog // import对应的对话框组件
  },
  DEVICE_ADD_TO_SUBNET_CMD: {
    type: 'DeviceAddToSubnet',
    executeComponent: 'DeviceAddToSubnetDialog',
    registerComponent: DeviceAddToSubnetDialog // import对应的对话框组件
  },
  DEVICE_REMOVE_TO_SUBNET_CMD: {
    type: 'DeviceRemoveFromSubnet',
    executeComponent: 'DeviceRemoveFromSubnetDialog',
    registerComponent: DeviceRemoveFromSubnetDialog // import对应的对话框组件
  },
  DEVICE_PORT_FLOW_CMD: {
    type: 'DevicePortFlow',
    executeComponent: 'DevicePortFlowDialog',
    registerComponent: DevicePortFlowDialog // import对应的对话框组件
  },
  UPDATE_BOOK_CMD: {
    type: 'DeviceBookUpdateDialog',
    executeComponent: 'DeviceBookUpdateDialog',
    registerComponent: DeviceBookUpdateDialog // import对应的对话框组件
  },

  // 链路
  CREATE_COPY_LINK_CMD: {
    type: 'CreateAndCopyLink',
    executeComponent: 'CreateAndCopyLinkDialog',
    registerComponent: CreateAndCopyLinkDialog
  },
  DELETE_LINK_CMD: {
    type: 'DeleteLink',
    executeComponent: 'DeleteLinkDialog',
    registerComponent: DeleteLinkDialog // import对应的对话框组件
  },
  UPDATE_LINK_CMD: {
    type: 'UpdateLink',
    executeComponent: 'UpdateLinkDialog',
    registerComponent: UpdateLinkDialog // import对应的对话框组件
  },
  IMPORT_LINK_CMD: {
    type: 'ImportLink',
    executeComponent: 'ImportLinkDialog',
    registerComponent: ImportLinkDialog // import对应的对话框组件
  },
  LINK_ADD_PLAN_CMD: {
    type: 'LinkAddPlanDevice',
    executeComponent: 'LinkAddPlanDeviceFunction',
    registerComponent: null // import对应的对话框组件
  },
  DISCOVERY_DEVICE_CMD: {
    type: 'DiscoveryDevice',
    executeComponent: 'DiscoveryDeviceDialog',
    registerComponent: DiscoveryDeviceDialog // import对应的对话框组件
  },
  DISCOVERY_LINK_CMD: {
    type: 'DiscoveryLink',
    executeComponent: 'DiscoveryLinkDialog',
    registerComponent: DiscoveryLinkDialog // import对应的对话框组件
  }
}

class DeviceCommands {
  // 设备
  static createAndCopyDeviceCommand (title, device, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.CREATE_COPY_DEVICE_CMD.type),
      visible: true,
      disabled: false,
      target: device,
      title: title,
      executeComponent: CommandTypes.CREATE_COPY_DEVICE_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.copyDeviceInfo')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return AuthorityFilter.deviceAuthorityFilter(command)
  }
  static blankCreateAndCopyDeviceCommand (title, device, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.CREATE_COPY_DEVICE_CMD.type),
      visible: true,
      disabled: false,
      target: device,
      title: title,
      executeComponent: CommandTypes.CREATE_COPY_DEVICE_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('TopologicalGraphShow.addDevice')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return AuthorityFilter.deviceAuthorityFilter(command)
  }
  static DiscoveryDeviceCommand (title, device, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.DISCOVERY_DEVICE_CMD.type),
      visible: true,
      disabled: false,
      target: device,
      title: title,
      executeComponent: CommandTypes.DISCOVERY_DEVICE_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.updateDeviceInfo')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return AuthorityFilter.deviceAuthorityFilter(command)
  }
  static DiscoveryLinkCommand (title, device, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.DISCOVERY_LINK_CMD.type),
      visible: true,
      disabled: false,
      target: device,
      title: title,
      executeComponent: CommandTypes.DISCOVERY_LINK_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.updateLinkInfo')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return AuthorityFilter.linkAuthorityFilter(command)
  }

  static deleteDeviceCommand (device, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.DELETE_DEVICE_CMD.type),
      visible: true,
      disabled: false,
      target: device,
      executeComponent: CommandTypes.DELETE_DEVICE_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.deleteDeviceInfo')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return AuthorityFilter.deviceAuthorityFilter(command)
  }

  static updateDeviceCommand (device, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.UPDATE_DEVICE_CMD.type),
      visible: true,
      disabled: false,
      target: device,
      executeComponent: CommandTypes.UPDATE_DEVICE_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.updateDeviceInfo')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return AuthorityFilter.deviceAuthorityFilter(command)
  }

  // 云平台
  static cloudPlatformCommand (device, flag, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.CLOUN_PLATFORM_CMD.type),
      visible: false,
      disabled: false,
      target: device,
      executeComponent: CommandTypes.CLOUN_PLATFORM_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: flag === '1' ? '应用云平台' : '接口云平台'
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
      var url = 'https://'.concat(device).concat(':8443/')
      window.open(url, '_blank')
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return command
  }
  // 存储设备
  static storageCommand (device, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.STORAGE_CMD.type),
      visible: false,
      disabled: false,
      target: device,
      executeComponent: CommandTypes.STORAGE_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: '存储监控'
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
      var url = 'https://'.concat(device).concat(':8443/')
      window.open(url, '_blank')
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return command
  }

  static importDeviceCommand (commandContext) {
    let command = {
      id: uniqueId(CommandTypes.IMPORT_DEVICE_CMD.type),
      visible: true,
      disabled: false,
      target: null,
      executeComponent: CommandTypes.IMPORT_DEVICE_CMD.executeComponent,
      execute: undefined,
      done: undefined
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return AuthorityFilter.deviceAuthorityFilter(command)
  }

  // 配置文件更新
  static downloadConfigToDeviceCommand (device, commandContext, v) {
    let command = {
      id: uniqueId(CommandTypes.DOWNLOAD_CONFIG_TO_DEVICE_CMD.type),
      visible: true,
      disabled: false,
      target: device,
      executeComponent: CommandTypes.DOWNLOAD_CONFIG_TO_DEVICE_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: '更新配置文件'
    }
    if (v === 2) {
      command.disabled = true
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return AuthorityFilter.deviceAuthorityFilter(command)
  }
  static copyConfigToServer (device, commandContext, v) {
    let command = {
      id: uniqueId(CommandTypes.COPY_TO_SERVER_CMD.type),
      visible: true,
      disabled: false,
      target: device,
      execute: undefined,
      done: undefined,
      name: '备份配置文件'
    }
    if (v === 2) {
      command.disabled = true
    }
    command.execute = function () {
      let vue = new Vue({
        i18n
      })
      Vue.set(commandContext, command.id, command)
      let loadingInstance = vue.Loading.openLoading()
      ConfigFileService.batchUploadConfigFile(device).then(result => {
        // 关闭loading
        vue.Loading.closeLoading(loadingInstance)
        // 成功
        vue.InfoTip.successTip(vue, HelperUtil.getStatusCodeObjectByCode(vue.successCode.OTHERS_CODE))
      }).catch(err => {
        // 关闭loading
        vue.Loading.closeLoading(loadingInstance)
        // 失败
        vue.InfoTip.errorTip(vue, err)
      })
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return AuthorityFilter.deviceAuthorityFilter(command)
  }

  static downloadToLocalCommand (device, commandContext, v) {
    let command = {
      id: uniqueId(CommandTypes.DOWNLOAD_TO_LOCAL.type),
      visible: true,
      disabled: false,
      target: device,
      executeComponent: CommandTypes.DOWNLOAD_TO_LOCAL.executeComponent,
      execute: undefined,
      done: undefined,
      name: '下载配置文件'
    }
    if (v === 2) {
      command.disabled = true
    }
    command.execute = function () {
      let vue = new Vue({
        i18n
      })
      Vue.set(commandContext, command.id, command)
      let loadingInstance = vue.Loading.openLoading()
      ConfigFileService.putDownConfFileDevice(device.deviceKey, '网元配置文件.conf').then(result => {
        vue.InfoTip.successTip(vue, HelperUtil.getStatusCodeObjectByCode(vue.successCode.OTHERS_CODE))
        vue.Loading.closeLoading(loadingInstance)
      }).catch(err => {
        // 关闭loading
        vue.Loading.closeLoading(loadingInstance)
        // 失败
        vue.InfoTip.errorTip(vue, err)
      })
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return AuthorityFilter.deviceAuthorityFilter(command)
  }
  // 历史配置文件
  static historyConfigToDeviceCommand (device, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.HISTORY_CONFIG_TO_DEVICE_CMD.type),
      visible: true,
      disabled: false,
      target: device,
      executeComponent: CommandTypes.HISTORY_CONFIG_TO_DEVICE_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: '历史配置文件'
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return AuthorityFilter.deviceAuthorityFilter(command)
  }

  // 启用设备
  static upDeviceCommand (device, commandContext) {
    let deviceTemp = HelperUtil.getNewDeviceObject(device)
    deviceTemp.deviceStatus = 0
    let command = {
      id: uniqueId(CommandTypes.UP_DEVICE_CMD.type),
      visible: true,
      disabled: false,
      target: deviceTemp,
      executeComponent: null,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.enableDeviceValue')
    }
    command.execute = function () {
      let vue = new Vue({
        i18n
      })
      Vue.set(commandContext, command.id, command)
      let loadingInstance = vue.Loading.openLoading()
      DeviceService.modifyDevice(command.target).then(result => {
        // 成功
        vue.Loading.closeLoading(loadingInstance)
        vue.InfoTip.successTip(vue, HelperUtil.getStatusCodeObjectByCode(vue.successCode.OTHERS_CODE))
        command.done()
      }).catch(err => {
        // 失败
        vue.Loading.closeLoading(loadingInstance)
        vue.InfoTip.errorTip(vue, err)
        command.done()
      })
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return command
  }

  // 停用设备
  static downDeviceCommand (device, commandContext) {
    let deviceTemp = HelperUtil.getNewDeviceObject(device)
    deviceTemp.deviceStatus = 1
    let command = {
      id: uniqueId(CommandTypes.UPDATE_DEVICE_CMD.type),
      visible: true,
      disabled: false,
      target: deviceTemp,
      executeComponent: null,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.disableDeviceValue')
    }
    command.execute = function () {
      let vue = new Vue({
        i18n
      })
      Vue.set(commandContext, command.id, command)
      let loadingInstance = vue.Loading.openLoading()
      DeviceService.modifyDevice(command.target).then(result => {
        // 成功
        vue.Loading.closeLoading(loadingInstance)
        vue.InfoTip.successTip(vue, HelperUtil.getStatusCodeObjectByCode(vue.successCode.OTHERS_CODE))
        command.done()
      }).catch(err => {
        // 失败
        vue.Loading.closeLoading(loadingInstance)
        vue.InfoTip.errorTip(vue, err)
        command.done()
      })
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return command
  }

  // 设备加入规划
  static deviceAddPlanDeviceCommand (device, commandContext) {
    let deviceTemp = HelperUtil.getNewDeviceObject(device)
    deviceTemp.deviceStatus = 0
    deviceTemp.startTime = HelperUtil.getCurrentTime()
    let command = {
      id: uniqueId(CommandTypes.DEVICE_ADD_PLAN_CMD.type),
      visible: true,
      disabled: false,
      target: deviceTemp,
      executeComponent: null,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.activateDevice')
    }
    command.execute = function () {
      let vue = new Vue({
        i18n
      })
      Vue.set(commandContext, command.id, command)
      let loadingInstance = vue.Loading.openLoading()
      DeviceService.modifyDevice(command.target).then(result => {
        // 成功
        vue.Loading.closeLoading(loadingInstance)
        vue.InfoTip.successTip(vue, HelperUtil.getStatusCodeObjectByCode(vue.successCode.OTHERS_CODE))
        command.done()
      }).catch(err => {
        // 失败
        vue.Loading.closeLoading(loadingInstance)
        vue.InfoTip.errorTip(vue, err)
        command.done()
      })
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return command
  }

  // 实时性能
  static rtPerformance (device, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.RT_PERFORMANCE_CMD.type),
      visible: true,
      disabled: false,
      target: device,
      executeComponent: null,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.rtPerformance')
    }
    command.execute = function () {
      store.commit('setPassDev', device)
      window.sessionStorage.setItem('dev', JSON.stringify(device))
      EventBus.$emit('rtperformance')
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return command
  }
  // 下载设备日志
  static downloadDeviceLogCommand (device, commandContext, v) {
    let command = {
      id: uniqueId(CommandTypes.DOWNLOAD_DEVICE_LOG_CMD.type),
      visible: true,
      disabled: false,
      target: device,
      executeComponent: null,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.loadDeviceLog')
    }
    if (v === 2) {
      command.disabled = true
    }
    command.execute = function () {
      let vue = new Vue({
        i18n
      })
      let loadingInstance = vue.Loading.openLoading()
      DeviceService.downloadDeviceLog(command.target.deviceKey).then(result => {
        // 成功
        vue.Loading.closeLoading(loadingInstance)
        vue.InfoTip.successTip(vue, HelperUtil.getStatusCodeObjectByCode(vue.successCode.OTHERS_CODE))
        command.done()
      }).catch(err => {
        // 失败
        vue.Loading.closeLoading(loadingInstance)
        vue.InfoTip.errorTip(vue, err)
        command.done()
      })
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return command
  }

  // 设备详情
  static showDeviceDetailsCommand (device, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.SHOW_DEVICE_DETAIL_CMD.type),
      visible: true,
      disabled: false,
      target: device,
      executeComponent: CommandTypes.SHOW_DEVICE_DETAIL_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.showDeviceInfo')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return command
  }

  // 设备面板图
  static showDevicePanelCommand (device, commandContext) {
    let command = {}
    if (device.deviceTypeKey === 'SWITCH' || device.deviceTypeKey === 'ROUTER' || device.deviceTypeKey === 'ROUTERSWITCH') {
      command.id = uniqueId(CommandTypes.SHOW_DEVICE_PANEL_CMD.type),
      command.visible = true,
      command.disabled = false,
      command.target = device,
      command.executeComponent = CommandTypes.SHOW_DEVICE_PANEL_CMD.executeComponent,
      command.execute = undefined,
      command.done = undefined,
      command.name = i18n.tc('DeviceCommands.showDevicePanel')
      command.title = i18n.tc('devicePanel.devicePanel')
    } else if (device.deviceTypeKey === 'SERVER') {
      command.id = uniqueId(CommandTypes.SHOW_DEVICE_PANEL_CMD.type),
      command.visible = true,
      command.disabled = false,
      command.target = device,
      command.executeComponent = CommandTypes.SHOW_DEVICE_PANEL_CMD.executeComponent,
      command.execute = undefined,
      command.done = undefined,
      command.name = i18n.tc('DeviceCommands.shouSourcePanel')
      command.title = i18n.tc('devicePanel.sourcePanel')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return command
  }
  static showServerDevicePanelCommand (device, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.SHOW_SERVER_DEVICE_PANEL_CMD.type),
      visible: true,
      disabled: false,
      target: device,
      executeComponent: CommandTypes.SHOW_SERVER_DEVICE_PANEL_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.shouSourcePanel'),
      title: i18n.tc('devicePanel.sourcePanel')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return command
  }

  // 查看交换机配置
  static showSwitchesConfigCommand (device, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.TURN_TO_SWITCHES_CMD.type),
      visible: true,
      disabled: false,
      target: device,
      executeComponent: CommandTypes.TURN_TO_SWITCHES_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.showSwitchConfiguration')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return command
  }
  static showIpmiConfigCommand (device, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.TURN_TO_IPMI_CMD.type),
      visible: true,
      disabled: false,
      target: device,
      executeComponent: CommandTypes.TURN_TO_IPMI_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.ipmiConfiguration')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return command
  }

  // 设备查看端口光功率
  static showDevicePortFlowCommand (port, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.DEVICE_PORT_FLOW_CMD.type),
      visible: true,
      disabled: false,
      target: port,
      executeComponent: CommandTypes.DEVICE_PORT_FLOW_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.watchPortPower')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return command
  }

  // 设备加入子网
  static deviceAddToSubnetCommand (device, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.DEVICE_ADD_TO_SUBNET_CMD.type),
      visible: true,
      disabled: false,
      target: device,
      executeComponent: CommandTypes.DEVICE_ADD_TO_SUBNET_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.deviceAddToSubnet')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return command
  }

  // 设备移出子网
  static deviceRemoveToSubnetCommand (device, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.DEVICE_REMOVE_TO_SUBNET_CMD.type),
      visible: true,
      disabled: false,
      target: device,
      executeComponent: CommandTypes.DEVICE_REMOVE_TO_SUBNET_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.deviceRemoveFromSubnet')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return command
  }
  static updateBookCommand (device, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.UPDATE_BOOK_CMD.type),
      visible: true,
      disabled: false,
      target: device,
      executeComponent: CommandTypes.UPDATE_BOOK_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.updateBookInfo')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return AuthorityFilter.bookAuthorityFilter(command)
  }

  // 链路
  static createAndCopyLinkCommand (title, link, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.CREATE_COPY_LINK_CMD.type),
      visible: true,
      disabled: false,
      target: link,
      title: title,
      executeComponent: CommandTypes.CREATE_COPY_LINK_CMD.executeComponent,
      execute: undefined,
      name: i18n.tc('TopologicalGraphShow.addLink'),
      done: undefined
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return AuthorityFilter.linkAuthorityFilter(command)
  }

  static deleteLinkCommand (link, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.DELETE_LINK_CMD.type),
      visible: true,
      disabled: false,
      target: link,
      executeComponent: CommandTypes.DELETE_LINK_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.deleteLinkInfo')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return AuthorityFilter.linkAuthorityFilter(command)
  }

  static updateLinkCommand (link, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.UPDATE_LINK_CMD.type),
      visible: true,
      disabled: false,
      target: link,
      executeComponent: CommandTypes.UPDATE_LINK_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.updateLinkInfo')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return AuthorityFilter.linkAuthorityFilter(command)
  }
  static importLinkCommand (commandContext) {
    let command = {
      id: uniqueId(CommandTypes.IMPORT_LINK_CMD.type),
      visible: true,
      disabled: false,
      target: null,
      executeComponent: CommandTypes.IMPORT_LINK_CMD.executeComponent,
      execute: undefined,
      done: undefined
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return AuthorityFilter.linkAuthorityFilter(command)
  }

  // 链路加入规划
  static linkAddPlanDeviceCommand (link, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.LINK_ADD_PLAN_CMD.type),
      visible: true,
      disabled: false,
      target: link,
      executeComponent: null,
      execute: undefined,
      done: undefined,
      name: i18n.tc('DeviceCommands.activateLink')
    }
    command.execute = function () {
      let vue = new Vue({
        i18n
      })
      Vue.set(commandContext, command.id, command)
      let linkList = []
      if (!Array.isArray(command.target)) {
        linkList.push(command.target)
      } else {
        linkList = command.target
      }
      for (let i = 0; i < linkList.length; i++) {
        let loadingInstance = vue.Loading.openLoading()
        let linkTemp = HelperUtil.getNewLinkObject(linkList[i])
        linkTemp.linkStatus = 0
        LinkService.modifyLink(linkTemp).then(result => {
          // 成功
          vue.Loading.closeLoading(loadingInstance)
          vue.InfoTip.successTip(vue, HelperUtil.getStatusCodeObjectByCode(vue.successCode.OTHERS_CODE))
          command.done()
        }).catch(err => {
          // 失败
          vue.Loading.closeLoading(loadingInstance)
          vue.InfoTip.errorTip(vue, err)
          command.done()
        })
      }
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return command
  }

  // 设备右键
  static generateCommandsForDevice (device, commandContext, addPlanFlag = {}, link = {}) {
    let cmdForDev = []
    // 删除设备
    let deleteList = []
    var obj = {'deviceKey': device.deviceKey}
    deleteList.push(obj)
    if (DeviceCommands.deleteDeviceCommand(deleteList, commandContext).visible === true) {
      // cmdForDev.push(StatusFilter.unsolvedStatusFilter(device.deviceStatus, DeviceCommands.deleteDeviceCommand(deleteList, commandContext)))
      cmdForDev.push(DeviceCommands.deleteDeviceCommand(deleteList, commandContext))
    }
    // 修改设备
    if (DeviceCommands.updateDeviceCommand(device, commandContext).visible === true) {
      // cmdForDev.push(StatusFilter.unsolvedStatusFilter(device.deviceStatus, DeviceCommands.updateDeviceCommand(device, commandContext)))
      cmdForDev.push(DeviceCommands.updateDeviceCommand(device, commandContext))
    }
    // 启用/停用设备
    if (device.deviceStatus === 1) {
      cmdForDev.push(DeviceCommands.upDeviceCommand(device, commandContext))
    } else {
      cmdForDev.push(DeviceCommands.downDeviceCommand(device, commandContext))
    }
    // 设备详情
    cmdForDev.push(DeviceCommands.showDeviceDetailsCommand(device, commandContext))
    // 设备加入子网
    if (!Array.isArray(device)) {
      let deviceList = []
      deviceList.push(device)
      cmdForDev.push(StatusFilter.unsolvedStatusFilter(device.deviceStatus, DeviceCommands.deviceAddToSubnetCommand(deviceList, commandContext)))
    }
    // 设备移出子网
    if (!Array.isArray(device)) {
      let deviceList = []
      deviceList.push(device)
      cmdForDev.push(StatusFilter.devAddToSubnetStatusFilter(device, StatusFilter.unsolvedStatusFilter(device.deviceStatus, DeviceCommands.deviceRemoveToSubnetCommand(deviceList, commandContext))))
    }
    // 设备面板图
    if (device.deviceTypeKey === 'SWITCH' || device.deviceTypeKey === 'ROUTERSWITCH' || device.deviceTypeKey === 'ROUTER') {
      // cmdForDev.push(StatusFilter.unsolvedStatusFilter(device.status, StatusFilter.StatusFilter(device.deviceStatus, DeviceCommands.showDevicePanelCommand(device, commandContext))))
      cmdForDev.push(StatusFilter.allStatus(device, DeviceCommands.showDevicePanelCommand(device, commandContext), addPlanFlag))
      cmdForDev.push(StatusFilter.allStatus(device, DeviceCommands.rtPerformance(device, commandContext), addPlanFlag))
    }
    // 资源状态图
    if (device.deviceTypeKey === 'SERVER') {
      cmdForDev.push(StatusFilter.allStatus(device, DeviceCommands.showServerDevicePanelCommand(device, commandContext), addPlanFlag))
      // cmdForDev.push(DeviceCommands.showServerDevicePanelCommand(device, commandContext))
    }
    if (device.deviceModel.startsWith('CZ.T-T65')) {
      // 下载设备日志
      cmdForDev.push(StatusFilter.allStatus(device, DeviceCommands.downloadDeviceLogCommand(device, commandContext), addPlanFlag))
      // 查看交换机配置
      cmdForDev.push(StatusFilter.allStatus(device, DeviceCommands.showSwitchesConfigCommand(device, commandContext), addPlanFlag))
    }
    // ipmi跳转
    // if (device.deviceTypeKey === 'SERVER' || device.deviceTypeKey === 'STORAGE') {
    // if (device.deviceModel === 'SR650' || device.deviceModel === 'V5000' || device.deviceModel === 'V5030' || device.deviceModel === 'V7000' || device.deviceModel === 'H3C R6800G2') {
    //   cmdForDev.push(StatusFilter.allStatus(device, DeviceCommands.showIpmiConfigCommand(device, commandContext), addPlanFlag))
    // }
    let a = configSelf.ipmi.split(',')
    for (let i = 0; i < a.length; i++) {
      if (a[i] === device.deviceModel) {
        cmdForDev.push(StatusFilter.allStatus(device, DeviceCommands.showIpmiConfigCommand(device, commandContext), addPlanFlag))
        break
      }
    }
    // 应用云平台
    if (device.ipAddress === configSelf.applicationCloudPlatform.ipAddress) {
      cmdForDev.push(StatusFilter.allStatus(device, DeviceCommands.cloudPlatformCommand(device.ipAddress, '1', commandContext), addPlanFlag))
    }
    // 接口云平台
    if (device.ipAddress === configSelf.interfaceCloudPlatform.ipAddress) {
      cmdForDev.push(StatusFilter.allStatus(device, DeviceCommands.cloudPlatformCommand(device.ipAddress, '0', commandContext), addPlanFlag))
    }
    // 存储设备
    if (device.deviceTypeKey === 'STORAGE') {
      cmdForDev.push(StatusFilter.allStatus(device, DeviceCommands.storageCommand(device.ipAddress, commandContext), addPlanFlag))
    }
    if (device.deviceModel.indexOf('CZ.T-T65D') !== -1) {
      var arr = []
      arr.push(device.deviceKey)
      cmdForDev.push(StatusFilter.allStatus(device, DeviceCommands.copyConfigToServer(arr, commandContext), addPlanFlag))
    }
    return cmdForDev
  }

  // 多选设备右键
  static generateCommandsForDeviceList (deviceList, commandContext) {
    let cmdForDev = []
    let command = {}
    // 设备加入子网
    command = DeviceCommands.deviceAddToSubnetCommand(deviceList, commandContext)
    for (let i = 0; i < deviceList.length; i++) {
      StatusFilter.unsolvedStatusFilter(deviceList[i].deviceStatus, command)
    }
    cmdForDev.push(command)
    // 设备移出子网
    command = DeviceCommands.deviceRemoveToSubnetCommand(deviceList, commandContext)
    for (let i = 0; i < deviceList.length; i++) {
      StatusFilter.unsolvedStatusFilter(deviceList[i].deviceStatus, command)
    }
    cmdForDev.push(command)
    return cmdForDev
  }

  // 链路右键
  static generateCommandsForLink (link, commandContext, addPlanFlag = {}) {
    let cmdForLink = []
    let deleteList = []
    deleteList.push({linkKey: link.linkKey})
    if (DeviceCommands.deleteLinkCommand(deleteList, commandContext).visible === true) {
      cmdForLink.push(StatusFilter.linkUnsolvedStatusFilter(link.linkStatus, DeviceCommands.deleteLinkCommand(deleteList, commandContext)))
    }
    if (DeviceCommands.updateLinkCommand(link, commandContext).visible === true) {
      cmdForLink.push(StatusFilter.linkUnsolvedStatusFilter(link.linkStatus, DeviceCommands.updateLinkCommand(link, commandContext)))
    }
    // cmdForLink.push(StatusFilter.linkSolvedStatusFilter(link.linkStatus, DeviceCommands.linkAddPlanDeviceCommand(link, commandContext)))
    return cmdForLink
  }

  // 端口右键
  static generateCommandsForPort (target, commandContext) {
    let cmdForPort = []
    cmdForPort.push(StatusFilter.portDownStatusFilter(target[1], DeviceCommands.showDevicePortFlowCommand(target, commandContext)))
    return cmdForPort
  }
}

DeviceCommands.CommandTypes = CommandTypes

export default DeviceCommands
