import Vue from 'vue'
import uniqueId from 'lodash'
import AuthorityFilter from '../commands/AuthorityFilter'
import CreateUserDialog from "@/components/DialogComponents/UserManagementDialog/CreateUserDialog.vue"
import DeleteUserDialog from "@/components/DialogComponents/UserManagementDialog/DeleteUserDialog.vue"
import UpdateUserDialog from '@/components/DialogComponents/UserManagementDialog/UpdateUserDialog.vue'
import UpdateUserPwdDialog from '@/components/DialogComponents/UserManagementDialog/UpdateUserPwd.vue'
import DeleteDateConfirmDialog from '@/components/DialogComponents/DeleteDataAuthConfirmDialog/DeleteDataAuthConfirmDialog.vue'

Vue.component('CreateUserDialog',CreateUserDialog);
Vue.component('DeleteUserDialog',DeleteUserDialog);
Vue.component('UpdateUserDialog',UpdateUserDialog);
Vue.component('UpdateUserPwdDialog',UpdateUserPwdDialog);
Vue.component('DeleteDateConfirmDialog',DeleteDateConfirmDialog);

let CommandTypes = {
  CREATE_USER_CMD: {
    type: 'CreateUser',
    executeComponent: 'CreateUserDialog',
    registerComponent: CreateUserDialog
  },
  DELETE_USER_CMD: {
    type: 'DeleteUser',
    executeComponent: 'DeleteUserDialog',
    registerComponent: DeleteUserDialog // import对应的对话框组件
  },
  UPDATE_USER_CMD: {
    type: 'UpdateUser',
    executeComponent: 'UpdateUserDialog',
    registerComponent: UpdateUserDialog // import对应的对话框组件
  },
  UPDATE_USER_PWD_CMD: {
    type: 'UpdateUserPwd',
    executeComponent: 'UpdateUserPwdDialog',
    registerComponent: UpdateUserPwdDialog // import对应的对话框组件
  },
  DELETE_DATA_CONFIRM_CMD: {
    type: 'DeleteDateConfirm',
    executeComponent: 'DeleteDateConfirmDialog',
    registerComponent: DeleteDateConfirmDialog // import对应的对话框组件
  },
};

class UserCommands {
  static createUserCommand (commandContext) {
    let command = {
      id: uniqueId(CommandTypes.CREATE_USER_CMD.type),
      visible: true,
      disabled: false,
      executeComponent: CommandTypes.CREATE_USER_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.userAuthorityFilter(command)
  }

  static deleteUserCommand (user, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.DELETE_USER_CMD.type),
      visible: true,
      disabled: false,
      target: user,
      executeComponent: CommandTypes.DELETE_USER_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.userAuthorityFilter(command)
  }

  static updateUserCommand (user, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.UPDATE_USER_CMD.type),
      visible: true,
      disabled: false,
      target: user,
      executeComponent: CommandTypes.UPDATE_USER_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.userAuthorityFilter(command)
  }

  static updateUserPwdCommand (user, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.UPDATE_USER_PWD_CMD.type),
      visible: true,
      disabled: false,
      target: user,
      executeComponent: CommandTypes.UPDATE_USER_PWD_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.loginUserAuthorityFilter(command)
  }

  static confirmUserCommand (commandContext, confirmFlag) {
    let command = {
      id: uniqueId(CommandTypes.DELETE_DATA_CONFIRM_CMD.type),
      visible: true,
      disabled: false,
      target: confirmFlag,
      executeComponent: CommandTypes.DELETE_DATA_CONFIRM_CMD.executeComponent,
      execute: undefined,
      done: undefined,
    };
    command.execute = function () {
      Vue.set(commandContext, command.id, command);
    };
    command.done = function () {
      Vue.delete(commandContext, command.id)
    };
    return command
  }
}

UserCommands.CommandTypes = CommandTypes;

export default UserCommands
