import {PORT} from "../shape/gengral-config";

export class WorkflowNodeToolbar {
  proxy;
  el;
  graph;
  node;

  constructor(proxy, graph) {
    this.proxy = proxy;
    // this.el = this.proxy.nodeToolbar.$el;
    this.graph = graph;
  }

  hide(visiblePortId) {
    // 隐藏连接桩
    this.hidePorts(visiblePortId);

    this.deleteNode();
  }

  show(node) {
    if (this.graph.isSelected(node)) {
      return;
    }

    this.node = node;

    // 显示连接桩
    this.showPorts(this.graph, this.node, false);

    // this.move();
  }

  showPorts(graph, node, isTargetNode) {

    // 暂时不限制连线的ports
    node.getPorts().forEach(port => this.showPort(node, port.id));

    // let nodeData = JSON.parse(node.data);
    // console.log(nodeData);
    // // 如果节点类型是条件网关、并行网关、事件网关 则显示可以连线的ports
    // let gatewayNodeTypes = [50,51,52];
    // if (gatewayNodeTypes.includes(nodeData.type)) {
    //   node.getPorts().forEach(port => this.showPort(node, port.id));
    //   return;
    // }
    //
    // const portIds = node.getPorts().map(metadata => metadata.id);
    // const allEdges = graph.getEdges();
    //
    // if (allEdges.find(edge => {
    //   const { port: portId } = isTargetNode ? edge.getTarget() : edge.getSource();
    //   return portIds.includes(portId);
    // })) {
    //   // 表示当前节点存在出/入的边
    //   return;
    // }
    //
    // node.getPorts().forEach(port => this.showPort(node, port.id));
  }

  /**
   * 显示连接桩
   * @param node
   * @param portId
   */
  showPort(node, portId) {
    node.portProp(portId, {
      attrs: {
        circle: {
          r: PORT.r,
          // 连接桩在连线交互时可以被连接
          magnet: true,
          fill: '#FFFFFF',
        },
      },
    });
  }

   deleteNode() {
    delete this.node;
  }

  /**
   * 隐藏连接桩
   * @param visiblePortId
   * @private
   */
  hidePorts(visiblePortId) {
    if (!this.node) {
      return;
    }

    const currentNode = this.node;
    currentNode.getPorts().forEach(port => {
      const portId = port.id;
      if (portId === visiblePortId) {
        currentNode.portProp(portId, {
          attrs: {
            circle: {
              r: PORT.r,
              // 连接桩在连线交互时可以被连接
              magnet: true,
              fill: "#A7B0BB",
            },
          },
        });
        return;
      }

      currentNode.portProp(portId, {
        attrs: {
          circle: {
            r: 0,
            // 连接桩在连线交互时不可被连接
            magnet: false,
            fill: '#FFFFFF',
          },
        },
      });
    });
  }
}
