import Vue from 'vue'
import uniqueId from 'lodash'
import AuthorityFilter from '../commands/AuthorityFilter'
import CreateUserGroupDialog from '@/components/DialogComponents/UserGroupDialog/CreateUserGroupDialog.vue'
import DeleteUserGroupDialog from '@/components/DialogComponents/UserGroupDialog/DeleteUserGroupDialog.vue'
import UpdateUserGroupDialog from '@/components/DialogComponents/UserGroupDialog/UpdateUserGroupDialog.vue'

Vue.component('CreateUserGroupDialog', CreateUserGroupDialog)
Vue.component('DeleteUserGroupDialog', DeleteUserGroupDialog)
Vue.component('UpdateUserGroupDialog', UpdateUserGroupDialog)

let CommandTypes = {
  CREATE_USER_GROUP_CMD: {
    type: 'CreateUserGroup',
    executeComponent: 'CreateUserGroupDialog',
    registerComponent: CreateUserGroupDialog
  },
  DELETE_USER_GROUP_CMD: {
    type: 'DeleteGroupUser',
    executeComponent: 'DeleteUserGroupDialog',
    registerComponent: DeleteUserGroupDialog // import对应的对话框组件
  },
  UPDATE_USER_GROUP_CMD: {
    type: 'UpdateUserGroup',
    executeComponent: 'UpdateUserGroupDialog',
    registerComponent: UpdateUserGroupDialog // import对应的对话框组件
  }
}

class UserGroupCommands {
  static createUserGroupCommand (commandContext) {
    let command = {
      id: uniqueId(CommandTypes.CREATE_USER_GROUP_CMD.type),
      visible: true,
      disabled: false,
      executeComponent: CommandTypes.CREATE_USER_GROUP_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.usergroupAuthorityAuthorityFilter(command)
  }

  static deleteUserGroupCommand (userGroup, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.DELETE_USER_GROUP_CMD.type),
      visible: true,
      disabled: false,
      target: userGroup,
      executeComponent: CommandTypes.DELETE_USER_GROUP_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.usergroupAuthorityAuthorityFilter(command)
  }

  static updateUserGroupCommand (userGroup, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.UPDATE_USER_GROUP_CMD.type),
      visible: true,
      disabled: false,
      target: userGroup,
      executeComponent: CommandTypes.UPDATE_USER_GROUP_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.usergroupAuthorityAuthorityFilter(command)
  }
}

UserGroupCommands.CommandTypes = CommandTypes

export default UserGroupCommands
