import Vue from 'vue'
import uniqueId from 'lodash'
import StatusFilter from '@/commands/StatusFilter'
import AuthorityFilter from '../commands/AuthorityFilter'
import CreateAndCopyServerDialog from "@/components/DialogComponents/ServerConfigurationDialog/CreateAndCopyServerDialog.vue"
import DeleteServerDialog from "@/components/DialogComponents/ServerConfigurationDialog/DeleteServerDialog.vue";
import UpdateServerDialog from '@/components/DialogComponents/ServerConfigurationDialog/UpdateServerDialog.vue'
import SDNCDeviceDialog from '@/components/DialogComponents/ServerConfigurationDialog/SDNCDeviceDialog.vue'
import SDNCControllerTopologyDialog from '@/components/DialogComponents/ServerConfigurationDialog/ShowSDNCControllerTopology.vue'

import ServerService from '../domain/services/ServerService'
import HelperUtil from "../utils/HelperUtil";
import {i18n} from "../main";

Vue.component('CreateAndCopyServerDialog',CreateAndCopyServerDialog);
Vue.component('DeleteServerDialog',DeleteServerDialog);
Vue.component('UpdateServerDialog',UpdateServerDialog);
Vue.component('SDNCDeviceDialog',SDNCDeviceDialog);
Vue.component('SDNCControllerTopologyDialog',SDNCControllerTopologyDialog);

let CommandTypes = {
  CREATE_COPY_SERVER_CMD: {
    type: 'CreateAndCopyServer',
    executeComponent: 'CreateAndCopyServerDialog',
    registerComponent: CreateAndCopyServerDialog
  },
  DELETE_SERVER_CMD: {
    type: 'DeleteServer',
    executeComponent: 'DeleteServerDialog',
    registerComponent: DeleteServerDialog // import对应的对话框组件
  },
  UPDATE_SERVER_CMD: {
    type: 'UpdateServer',
    executeComponent: 'UpdateServerDialog',
    registerComponent: UpdateServerDialog // import对应的对话框组件
  },
  SDNC_DEVICE_CMD: {
    type: 'SDNCDevice',
    executeComponent: 'SDNCDeviceDialog',
    registerComponent: SDNCDeviceDialog // import对应的对话框组件
  },
  SHOW_SDNC_DEVICE_CMD: {
    type: 'showSDNCDevice',
    executeComponent: '',
    registerComponent: null // import对应的对话框组件
  },
  SHOW_SDNC_CONTROLLER_TOPOLOGY_CMD: {
    type: 'showSDNCControllerTopology',
    executeComponent: 'SDNCControllerTopologyDialog',
    registerComponent: SDNCControllerTopologyDialog // import对应的对话框组件
  },
};

class ServerCommands {
  static createAndCopyServerCommand (title, server, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.CREATE_COPY_SERVER_CMD.type),
      visible: true,
      disabled: false,
      target: server,
      title: title,
      executeComponent: CommandTypes.CREATE_COPY_SERVER_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.serverAuthorityFilter(command)
  }

  static deleteServerCommand (server, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.DELETE_SERVER_CMD.type),
      visible: true,
      disabled: false,
      target: server,
      executeComponent: CommandTypes.DELETE_SERVER_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('ServerCommands.deleteServer')
    };
    command.execute = function () {
      Vue.set(commandContext, command.id, command);
    };
    command.done = function () {
      Vue.delete(commandContext, command.id)
    };
    return AuthorityFilter.serverAuthorityFilter(command)
  }

  static updateServerCommand (server, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.UPDATE_SERVER_CMD.type),
      visible: true,
      disabled: false,
      target: server,
      executeComponent: CommandTypes.UPDATE_SERVER_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('ServerCommands.updateServer')
    };
    command.execute = function () {
      Vue.set(commandContext, command.id, command);
    };
    command.done = function () {
      Vue.delete(commandContext, command.id)
    };
    return AuthorityFilter.serverAuthorityFilter(command)
  }

  //查看服务器管辖列表
  static SDNCDeviceCommand (server, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.SDNC_DEVICE_CMD.type),
      visible: true,
      disabled: false,
      target: server,
      executeComponent: CommandTypes.SDNC_DEVICE_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('ServerCommands.showDemoList')
    };
    command.execute = function () {
      Vue.set(commandContext, command.id, command);
    };
    command.done = function () {
      Vue.delete(commandContext, command.id)
    };
    return command
  }

  //标注服务器管辖列表
  static showSDNCDeviceCommand (server, commandContext, deviceList) {
    let command = {
      id: uniqueId(CommandTypes.SHOW_SDNC_DEVICE_CMD.type),
      visible: true,
      disabled: false,
      target: server,
      executeComponent: CommandTypes.SHOW_SDNC_DEVICE_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('ServerCommands.showDemoArea')
    };
    command.execute = function () {
      let vue = new Vue({
        i18n,
      });
      Vue.set(commandContext, command.id, command);
      let loadingInstance = vue.Loading.openLoading();
      ServerService.getDevicesUnderServer(command.target.serverKey).then(result => {
        //成功
        vue.Loading.closeLoading(loadingInstance);
        if(result !== null){
          for(let deviceKey in result){
            Vue.set(deviceList, deviceKey, result[deviceKey]);
          }
        }
        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
  }

  //查看SDNC服务器控制关系拓扑图（暂无此功能）
  static ShowSDNCControllerTopologyCommand (server, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.SHOW_SDNC_CONTROLLER_TOPOLOGY_CMD.type),
      visible: true,
      disabled: false,
      target: server,
      executeComponent: CommandTypes.SHOW_SDNC_CONTROLLER_TOPOLOGY_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 command
  }

  //SDNC服务器右键
  static generateCommandsForSDNCService (server, commandContext, deleteFlag, pointList = {}) {
    let cmdForDev = [];
    //删除服务器
    let deleteList = [];
    deleteList.push(server.serverKey);
    let command = {};
    command = ServerCommands.deleteServerCommand(deleteList,commandContext);
    if(command.visible === true){
      cmdForDev.push(StatusFilter.deleteStatusFilter(deleteFlag,command));
    }
    //修改服务器
    command = ServerCommands.updateServerCommand(server,commandContext);
    if(command.visible === true){
      cmdForDev.push(command);
    }
    //查看管辖设备
    cmdForDev.push(ServerCommands.SDNCDeviceCommand(server,commandContext));
    //标注管辖范围
    cmdForDev.push(ServerCommands.showSDNCDeviceCommand(server,commandContext, pointList));
    return cmdForDev
  }

  //其他服务器右键
  static generateCommandsForOtherService (server, commandContext) {
    let cmdForDev = [];
    //删除服务器
    let deleteList = [];
    deleteList.push(server.serverKey);
    cmdForDev.push(ServerCommands.deleteServerCommand(deleteList,commandContext));
    //修改服务器
    cmdForDev.push(ServerCommands.updateServerCommand(server,commandContext));
    return cmdForDev
  }
}

ServerCommands.CommandTypes = CommandTypes;

export default ServerCommands
