import Vue from 'vue'
import uniqueId from 'lodash'
import {i18n} from '../main'
import AuthorityFilter from '../commands/AuthorityFilter'
import CreateAndCopyIndexTemplateDialog from '@/components/DialogComponents/IndexTemplateDialog/CreateAndCopyIndexTemplateDialog.vue'
import CopyIndexTemplateDialog from '@/components/DialogComponents/IndexTemplateDialog/CopyIndexTemplateDialog.vue'
import DeleteIndexTemplateDialog from '@/components/DialogComponents/IndexTemplateDialog/DeleteIndexTemplateDialog.vue'
import UpdateIndexTemplateDialog from '@/components/DialogComponents/IndexTemplateDialog/UpdateIndexTemplateDialog.vue'

Vue.component('CreateAndCopyIndexTemplateDialog', CreateAndCopyIndexTemplateDialog)
Vue.component('CopyIndexTemplateDialog', CopyIndexTemplateDialog)
Vue.component('DeleteIndexTemplateDialog', DeleteIndexTemplateDialog)
Vue.component('UpdateIndexTemplateDialog', UpdateIndexTemplateDialog)

let CommandTypes = {
  CREATE_COPY_INDEX_TEMPLATE_CMD: {
    type: 'CreateAndCopyIndexTemplateDialog',
    executeComponent: 'CreateAndCopyIndexTemplateDialog',
    registerComponent: CreateAndCopyIndexTemplateDialog
  },
  COPY_INDEX_TEMPLATE_CMD: {
    type: 'CopyIndexTemplateDialog',
    executeComponent: 'CopyIndexTemplateDialog',
    registerComponent: CopyIndexTemplateDialog
  },
  DELETE_INDEX_TEMPLATE_CMD: {
    type: 'DeleteIndexTemplateDialog',
    executeComponent: 'DeleteIndexTemplateDialog',
    registerComponent: DeleteIndexTemplateDialog // import对应的对话框组件
  },
  UPDATE_INDEX_TEMPLATE_CMD: {
    type: 'UpdateIndexTemplateDialog',
    executeComponent: 'UpdateIndexTemplateDialog',
    registerComponent: UpdateIndexTemplateDialog
  }
}

class IndexTemplateCommands {
  static createAndCopyCommand (title, indexTemplate, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.CREATE_COPY_INDEX_TEMPLATE_CMD.type),
      visible: true,
      disabled: false,
      target: indexTemplate,
      title: title,
      executeComponent: CommandTypes.CREATE_COPY_INDEX_TEMPLATE_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('IndexTemplateCommands.createIndexTemplate')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return AuthorityFilter.indexTemplateAuthorityFilter(command)
  }

  static copyCommand (indexTemplate, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.COPY_INDEX_TEMPLATE_CMD.type),
      visible: true,
      disabled: false,
      target: indexTemplate,
      executeComponent: CommandTypes.COPY_INDEX_TEMPLATE_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('IndexTemplateCommands.createIndexTemplate')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return AuthorityFilter.indexTemplateAuthorityFilter(command)
  }

  static deleteIndexTemplateCommand (indexTemplate, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.DELETE_INDEX_TEMPLATE_CMD.type),
      visible: true,
      disabled: false,
      target: indexTemplate,
      executeComponent: CommandTypes.DELETE_INDEX_TEMPLATE_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('IndexTemplateCommands.deleteIndexTemplate')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return AuthorityFilter.indexTemplateAuthorityFilter(command)
  }

  static updateIndexTemplateCommand (indexTemplate, commandContext) {
    let command = {
      id: uniqueId(CommandTypes.UPDATE_INDEX_TEMPLATE_CMD.type),
      visible: true,
      disabled: false,
      target: indexTemplate,
      executeComponent: CommandTypes.UPDATE_INDEX_TEMPLATE_CMD.executeComponent,
      execute: undefined,
      done: undefined,
      name: i18n.tc('IndexTemplateCommands.updateIndexTemplate')
    }
    command.execute = function () {
      Vue.set(commandContext, command.id, command)
    }
    command.done = function () {
      Vue.delete(commandContext, command.id)
    }
    return AuthorityFilter.indexTemplateAuthorityFilter(command)
  }
}

IndexTemplateCommands.CommandTypes = CommandTypes

export default IndexTemplateCommands
