import { getExtensionValue, editExtension } from './utils'
export default class CustomContextPad {
  constructor(bpmnFactory, config, contextPad, create, elementFactory, injector, translate) {
    this.bpmnFactory = bpmnFactory
    this.create = create
    this.elementFactory = elementFactory
    this.translate = translate
    this.config = config
    if (config.autoPlace !== false) {
      this.autoPlace = injector.get('autoPlace', false)
    }

    contextPad.registerProvider(this)
  }

  getContextPadEntries(element) {
    const {
      autoPlace,
      bpmnFactory,
      elementFactory,
      config
    } = this

    function appendUserTask() {
      return function(event, element) {
        if (autoPlace) {
          const extensionElements = bpmnFactory.create('bpmn:ExtensionElements')
          const Properties = bpmnFactory.create('camunda:Properties')
          const Property = bpmnFactory.create('camunda:Property')
          Property.name = 'relationInfo'
          Property.value = JSON.stringify({
            taskExtInfo: {
              custom: 'userTask'
            }
          })
          Properties.values = [Property]
          extensionElements.values = [Properties]
          const businessObject = bpmnFactory.create('bpmn:UserTask', {
            extensionElements: extensionElements
          })

          const shape = elementFactory.createShape({
            type: 'bpmn:UserTask',
            businessObject: businessObject
          })

          autoPlace.append(element, shape)
        }
      }
    }
    function changeStyle() {
      return function(event, element) {
        const custom = getExtensionValue(element.businessObject, 'custom')
        editExtension(config.modules[0].bpmnjs[1], element, 'custom', custom === 'userTask' ? 'conditionalUserTask' : 'userTask')
      }
    }
    const pad = {}
    // const taskNotify = {
    //   group: 'model',
    //   className: 'icon-custom bpmn-icon-notify',
    //   title: '插入消息任务',
    //   action: {
    //     click: appendTask('msgTask'),
    //     dragstart: appendTaskStart()
    //   }
    // }
    const userTask = {
      group: 'model',
      className: 'icon-custom bpmn-icon-custom-user-task',
      title: '插入',
      action: {
        click: appendUserTask(),
        dragstart: appendUserTask()
      }
    }
    const changeUserTaskStyle = {
      group: 'model',
      className: 'icon-custom bpmn-icon-custom-change-style',
      title: '改变用户任务的样式',
      action: {
        click: changeStyle(),
        dragstart: changeStyle()
      }
    }
    if (element.type !== 'bpmn:EndEvent' && element.type !== 'bpmn:TextAnnotation' && element.type !== 'label' && element.type !== 'bpmn:SequenceFlow') {
      // pad['append.task-notify'] = taskNotify
      pad['append.task-user'] = userTask
      pad['changeUserTaskStyle'] = changeUserTaskStyle
    }
    return pad
  }
}

CustomContextPad.$inject = [
  'bpmnFactory',
  'config',
  'contextPad',
  'create',
  'elementFactory',
  'injector',
  'translate'
]