export default function ContextPadProvider(
  contextPad,
  config,
  injector,
  translate,
  bpmnFactory,
  elementFactory,
  create,
  modeling,
  connect,
) {
  this.create = create;
  this.elementFactory = elementFactory;
  this.translate = translate;
  this.bpmnFactory = bpmnFactory;
  this.modeling = modeling;
  this.connect = connect;
  config = config || {};
  if (config.autoPlace !== false) {
    this.autoPlace = injector.get('autoPlace', false);
  }
  console.log('🚀 ~ file: contextPadProvider.js:23 ~ contextPad:', contextPad);
  contextPad.registerProvider(this);
}

ContextPadProvider.$inject = [
  'contextPad',
  'config',
  'injector',
  'translate',
  'bpmnFactory',
  'elementFactory',
  'create',
  'modeling',
  'connect',
];

ContextPadProvider.prototype.getContextPadEntries = function (element) {
  const { autoPlace, create, bpmnFactory, elementFactory, translate, modeling, connect } = this;
  // 删除功能
  function removeElement(e) {
    modeling.removeElements([element]);
  }

  // 连接功能
  function startConnect(event, element) {
    connect.start(event, element);
  }

  function clickElement(e) {
    console.log(element);
    // window.localStorage.setItem('nodeInfo', JSON.stringify(element))
    // window.localStorage.setItem('nodeVisible', 'true')
  }

  function appendTask(type, name) {
    return function (event, element) {
      console.log('🚀 ~ file: contextPadProvider.js:51 ~ appendTask ~ element:', event);
      console.log('🚀 ~ file: contextPadProvider.js:51 ~ appendTask ~ element:', element);
      console.log(autoPlace);
      if (autoPlace) {
        const shapeType = type;
        const businessObject = bpmnFactory.create(shapeType, {
          id: element.di.id,
          custom: 2,
          name,
        });
        const shape = elementFactory.createShape({ type: shapeType, businessObject });
        autoPlace.append(element, shape);
      } else {
        appendTaskStart(event, element);
      }
    };
  }

  function appendTaskStart(name) {
    return function (event) {
      console.log(event);
      const shapeType = event.type;
      const businessObject = bpmnFactory.create(shapeType, {
        id: element.di.id,
        custom: 2,
        name,
      });
      const shape = elementFactory.createShape({ type: shapeType, businessObject });
      create.start(event, shape, element);
    };
  }

  return {
    'append.user-task': {
      group: 'activity',
      className: 'bpmn-icon-user-task',
      title: translate('添加审批节点'),
      type: 'bpmn:UserTask',
      action: {
        click: appendTask('bpmn:UserTask', '审批节点'),
        dragstart: appendTaskStart('审批节点'),
      },
    },
    'append.receive-task': {
      group: 'activity',
      className: 'bpmn-icon-receive-task',
      title: translate('添加抄送节点'),
      type: 'bpmn:ReceiveTask',
      action: {
        click: appendTask('bpmn:ReceiveTask', '抄送节点'),
        dragstart: appendTaskStart('抄送节点'),
      },
    },
    'append.script-task': {
      group: 'activity',
      className: 'bpmn-icon-script-task',
      title: translate('添加脚本节点'),
      type: 'bpmn:ScriptTask',
      action: {
        click: appendTask('bpmn:ScriptTask', '脚本节点'),
        dragstart: appendTaskStart('脚本节点'),
      },
    },
    'append.service-task': {
      group: 'activity',
      className: 'bpmn-icon-service-task',
      title: translate('添加服务节点'),
      type: 'bpmn:ServiceTask',
      action: {
        click: appendTask('bpmn:ServiceTask', '服务节点'),
        dragstart: appendTaskStart('服务节点'),
      },
    },
    'append.exclusive-gateway': {
      group: 'activity',
      className: 'bpmn-icon-gateway-xor',
      title: translate('添加互斥网关'),
      type: 'bpmn:ExclusiveGateway',
      action: {
        click: appendTask('bpmn:ExclusiveGateway', '互斥网关'),
        dragstart: appendTaskStart('互斥网关'),
      },
    },
    'append.parallel-gateway': {
      group: 'activity',
      className: 'bpmn-icon-gateway-parallel',
      title: translate('添加并行网关'),
      type: 'bpmn:ParallelGateway',
      action: {
        click: appendTask('bpmn:ParallelGateway', '并行网关'),
        dragstart: appendTaskStart('并行网关'),
      },
    },
    connect: {
      group: 'edit',
      className: 'bpmn-icon-connection-multi',
      title: translate('激活连接工具'),
      action: {
        click: startConnect,
        dragstart: startConnect,
      },
    },
    delete: {
      group: 'edit',
      className: 'bpmn-icon-trash',
      title: translate('删除'),
      action: {
        click: removeElement,
      },
    },
  };
};
