import Vue from 'vue'
import uniqueId from 'lodash'
import {i18n} from "../main";
import StatusFilter from '@/commands/StatusFilter'
import AuthorityFilter from '../commands/AuthorityFilter'
import CreateAndCopySubnetDialog from '@/components/DialogComponents/SubnetDialog/CreateAndCopySubnet.vue'
import DeleteSubnetDialog from '@/components/DialogComponents/SubnetDialog/DeleteSubnetDialog.vue'
import UpdateSubnetDialog from '@/components/DialogComponents/SubnetDialog/UpdateSubnet.vue'
import ImportSubnetDialog from '@/components/DialogComponents/SubnetDialog/ImportSubnetDialog.vue'

Vue.component('CreateAndCopySubnetDialog',CreateAndCopySubnetDialog);
Vue.component('DeleteSubnetDialog',DeleteSubnetDialog);
Vue.component('UpdateSubnetDialog',UpdateSubnetDialog);
Vue.component('ImportSubnetDialog',ImportSubnetDialog);

let CommandTypes = {
  CREATE_COPY_SUBNET_CMD: {
    type: 'CreateAndCopySubnet',
    executeComponent: 'CreateAndCopySubnetDialog',
    registerComponent: CreateAndCopySubnetDialog
  },
  DELETE_SUBNET_CMD: {
    type: 'DeleteSubnet',
    executeComponent: 'DeleteSubnetDialog',
    registerComponent: DeleteSubnetDialog // import对应的对话框组件
  },
  UPDATE_SUBNET_CMD: {
    type: 'UpdateSubnet',
    executeComponent: 'UpdateSubnetDialog',
    registerComponent: UpdateSubnetDialog // import对应的对话框组件
  },
  IMPORT_SUBNET_CMD: {
    type: 'ImportSubnetToExcel',
    executeComponent: 'ImportSubnetDialog',
    registerComponent: ImportSubnetDialog // import对应的对话框组件
  },
};

class SubnetCommands {
  static createAndCopySubnetCommand (title, subnet, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.CREATE_COPY_SUBNET_CMD.type),
      visible: true,
      disabled: false,
      target: subnet,
      title: title,
      executeComponent: CommandTypes.CREATE_COPY_SUBNET_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('SubnetCommands.createSubnet')
    };
    command.execute = function () {
      Vue.set(commandContext, command.id, command);
    };
    command.done = function () {
      Vue.delete(commandContext, command.id)
    };
    return AuthorityFilter.subnetAuthorityFilter(command)
  }

  static deleteSubnetCommand (subnet, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.DELETE_SUBNET_CMD.type),
      visible: true,
      disabled: false,
      target: subnet,
      executeComponent: CommandTypes.DELETE_SUBNET_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('SubnetCommands.deleteSubnet')
    };
    command.execute = function () {
      Vue.set(commandContext, command.id, command);
    };
    command.done = function () {
      Vue.delete(commandContext, command.id)
    };
    return AuthorityFilter.subnetAuthorityFilter(command)
  }

  static updateSubnetCommand (subnet, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.UPDATE_SUBNET_CMD.type),
      visible: true,
      disabled: false,
      target: subnet,
      executeComponent: CommandTypes.UPDATE_SUBNET_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('SubnetCommands.modifySubnet')
    };
    command.execute = function () {
      Vue.set(commandContext, command.id, command);
    };
    command.done = function () {
      Vue.delete(commandContext, command.id)
    };
    return AuthorityFilter.subnetAuthorityFilter(command)
  }

  static importSubnetCommand (commandContext) {
    let command = {
      id: uniqueId(CommandTypes.IMPORT_SUBNET_CMD.type),
      visible: true,
      disabled: false,
      target: null,
      executeComponent: CommandTypes.IMPORT_SUBNET_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.subnetAuthorityFilter(command)
  }

  static generateCommandsForSubnet (subnet, commandContext, deleteFlag={}) {
    let cmdForSunNet = [];
    let deleteList = [];
    deleteList.push({subnetKey: subnet.subnetKey})
    if(SubnetCommands.deleteSubnetCommand(deleteList, commandContext).visible===true){
      cmdForSunNet.push(StatusFilter.deleteStatusFilter(deleteFlag,SubnetCommands.deleteSubnetCommand(deleteList, commandContext)));
    }
    if(SubnetCommands.updateSubnetCommand(subnet, commandContext).visible===true){
      cmdForSunNet.push(SubnetCommands.updateSubnetCommand(subnet, commandContext));
    }
    return cmdForSunNet
  }
}

SubnetCommands.CommandTypes = CommandTypes;

export default SubnetCommands
