import Vue from 'vue'
import uniqueId from 'lodash'
import AuthorityFilter from '../commands/AuthorityFilter'
import DeleteFirmWareDialog from '@/components/DialogComponents/FirmWareDialog/DeleteFirmWareDialog.vue'
import UpdateFirmWareDialog from '@/components/DialogComponents/FirmWareDialog/UpdateFirmWareDialog.vue'
import UploadFirmWareDialog from '@/components/DialogComponents/FirmWareDialog/UploadFirmWareDialog.vue'
import RecoverFirmWareDialog from '@/components/DialogComponents/FirmWareDialog/RecoverFirmWareDialog.vue'

Vue.component('DeleteFirmWareDialog', DeleteFirmWareDialog)
Vue.component('UpdateFirmWareDialog', UpdateFirmWareDialog)
Vue.component('UploadFirmWareDialog', UploadFirmWareDialog)
Vue.component('RecoverFirmWareDialog', RecoverFirmWareDialog)

let CommandTypes = {
  UPLOAD_FIRMWARE__CMD: {
    type: 'UploadFirmWareType',
    executeComponent: 'UploadFirmWareDialog',
    registerComponent: UploadFirmWareDialog // 对应的对话框组件
  },
  DElETE_FIRMWARE__CMD: {
    type: 'DeleteFirmWareType',
    executeComponent: 'DeleteFirmWareDialog',
    registerComponent: DeleteFirmWareDialog // 对应的对话框组件
  },
  UPDATE_FIRMWARE__CMD: {
    type: 'UpdateFirmWareType',
    executeComponent: 'UpdateFirmWareDialog',
    registerComponent: UpdateFirmWareDialog
  },
  RECOVER_FIRMWARE__CMD: {
    type: 'RecoverFirmWareType',
    executeComponent: 'RecoverFirmWareDialog',
    registerComponent: RecoverFirmWareDialog
  }
}

class FirmWareCommands {
  static uploadFireWareCommand (commandContext) {
    let command = {
      id: uniqueId(CommandTypes.UPLOAD_FIRMWARE__CMD.type),
      visible: true,
      disabled: false,
      executeComponent: CommandTypes.UPLOAD_FIRMWARE__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.firmWareAuthorityFilter(command)
  }

  static deleteFirmWareCommand (deleteList, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.DElETE_FIRMWARE__CMD.type),
      visible: true,
      disabled: false,
      target: deleteList,
      executeComponent: CommandTypes.DElETE_FIRMWARE__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.firmWareAuthorityFilter(command)
  }
  static updateFirmWareCommand (updateFirmWareKey, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.UPDATE_FIRMWARE__CMD.type),
      visible: true,
      disabled: false,
      target: updateFirmWareKey,
      executeComponent: CommandTypes.UPDATE_FIRMWARE__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.firmWareAuthorityFilter(command)
  }
  static recoverFirmWareCommand (recoverFirmWareKey, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.RECOVER_FIRMWARE__CMD.type),
      visible: true,
      disabled: false,
      target: recoverFirmWareKey,
      executeComponent: CommandTypes.RECOVER_FIRMWARE__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.firmWareAuthorityFilter(command)
  }
}

FirmWareCommands.CommandTypes = CommandTypes;

export default FirmWareCommands
