import Vue from 'vue'
import uniqueId from 'lodash'
import AuthorityFilter from '../commands/AuthorityFilter'
import CreateAndCopyDevicePort from '@/components/DialogComponents/DevicePortDialog/CreateAndCopyDevicePort.vue'
import DeleteDevicePortDialog from '@/components/DialogComponents/DevicePortDialog/DeleteDevicePortDialog.vue'
import UpdateDevicePort from '@/components/DialogComponents/DevicePortDialog/UpdateDevicePort.vue'
import ImportDevicePortDialog from '@/components/DialogComponents/DevicePortDialog/ImportDevicePortDialog.vue'

Vue.component('CreateAndCopyDevicePort', CreateAndCopyDevicePort)
Vue.component('DeleteDevicePortDialog', DeleteDevicePortDialog)
Vue.component('UpdateDevicePort', UpdateDevicePort)
Vue.component('ImportDevicePortDialog', ImportDevicePortDialog)

let CommandTypes = {
  CREATE_COPY_DEVICE_PORT_CMD: {
    type: 'CreateAndCopyDevicePort',
    executeComponent: 'CreateAndCopyDevicePort',
    registerComponent: CreateAndCopyDevicePort
  },
  DELETE_DEVICE_PORT_CMD: {
    type: 'DeleteDevicePortDialog',
    executeComponent: 'DeleteDevicePortDialog',
    registerComponent: DeleteDevicePortDialog // import对应的对话框组件
  },
  UPDATE_DEVICE_PORT_CMD: {
    type: 'UpdateDevicePort',
    executeComponent: 'UpdateDevicePort',
    registerComponent: UpdateDevicePort // import对应的对话框组件
  },
  IMPORT_DEVICE_PORT_CMD: {
    type: 'ImportDevicePortDialog',
    executeComponent: 'ImportDevicePortDialog',
    registerComponent: ImportDevicePortDialog // import对应的对话框组件
  }
}

class PortCommands {
  static createAndCopyPortCommand (title, eLine, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.CREATE_COPY_DEVICE_PORT_CMD.type),
      visible: true,
      disabled: false,
      target: eLine,
      title: title,
      executeComponent: CommandTypes.CREATE_COPY_DEVICE_PORT_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.accessServiceAuthorityFilter(command)
  }

  static deletePortCommand (eLine, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.DELETE_DEVICE_PORT_CMD.type),
      visible: true,
      disabled: false,
      target: eLine,
      executeComponent: CommandTypes.DELETE_DEVICE_PORT_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.accessServiceAuthorityFilter(command)
  }

  static updatePortCommand (eLine, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.UPDATE_DEVICE_PORT_CMD.type),
      visible: true,
      disabled: false,
      target: eLine,
      executeComponent: CommandTypes.UPDATE_DEVICE_PORT_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.accessServiceAuthorityFilter(command)
  }

  static importPortCommand (commandContext) {
    let command = {
      id: uniqueId(CommandTypes.IMPORT_DEVICE_PORT_CMD.type),
      visible: true,
      disabled: false,
      target: null,
      executeComponent: CommandTypes.IMPORT_DEVICE_PORT_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.accessServiceAuthorityFilter(command)
  }
}

PortCommands.CommandTypes = CommandTypes

export default PortCommands
