import { useWorkflowStore } from '@/store/modules/workflow';
import { FormDesignApi } from '../../form/design/api/FormDesignApi';
import { message } from 'ant-design-vue';
import { computed } from 'vue';
import { PORTS } from '../shape/gengral-config';
import { defaultEdgeData, conditionEdgeData, IllogicalEdgeData, noticeEdgeData, relation1EdgeData, relation2EdgeData } from '../model/edge_type_data';
import {
  subEventDefaultData,
  promoterEventDefaultData,
  messageListeningDefaultData,
  approveEventDefaultData,
  sendEventDefaultData,
  custonActivityDefaultData,
  conditionDefaultData
} from '../data/load-node-data';

export default function () {
  // store
  const workflowStore = useWorkflowStore();

  // 画布
  const graph = computed(() => {
    return workflowStore.graph;
  });

  // 线的类型
  const edgeType = computed(() => {
    return workflowStore.edgeType;
  })

  // 是否可以添加
  const canAdd = nodeItem => {
    if (nodeItem.type === 10) {
      if (graph.value.getNodes().length > 0) {
        let node = graph.value.getNodes().find(({ data }) => {
          return data && JSON.parse(data).type === nodeItem.type;
        });
        if (node) {
          message.warn('只能有一个开始事件');
          return false;
        }
      }
    }

    // 判断拖拽的节点类型是否是结束事件
    if (nodeItem.type === 11) {
      if (graph.value.getNodes().length > 0) {
        let node = graph.value.getNodes().find(({ data }) => {
          return data && JSON.parse(data).type === nodeItem.type;
        });
        if (node) {
          message.warn('只能有一个结束事件');
          return false;
        }
      }
    }
    return true;
  };

  // 添加
  const addNodeItem = async (nodeItem, x, y, type, currentNodeData, nodeData) => {
    let defaultNodeData = {};
    switch (nodeItem.type) {
      case 12:
        defaultNodeData = subEventDefaultData;
        break;
      case 13:
        defaultNodeData = promoterEventDefaultData;
        break;
      case 20:
        defaultNodeData = messageListeningDefaultData;
        break;
      case 30:
        defaultNodeData = approveEventDefaultData;
        break;
      case 31:
        defaultNodeData = sendEventDefaultData;
        break;
      case 40:
        defaultNodeData = custonActivityDefaultData;
        break;
      case 60:
      case 248:
        defaultNodeData = conditionDefaultData;
        break;
      default:
        break;
    }
    nodeItem.data.nodeData = { ...defaultNodeData };

    let id = await FormDesignApi.genId();

    if (id) {
      nodeItem.data.nodeId = id;
      let node = {};
      // 旧ui
      if (nodeItem.type < 99 || nodeItem.type > 600 && nodeItem.type < 610) {
        // 节点
        node = {
          shape: 'custom-vue-shape',
          x: x,
          y: y,
          id,
          width: 254,
          height: 76,
          data: JSON.stringify(nodeItem),
          ports: { ...PORTS }
        };
        // 网关
        if (nodeItem.type === 50 || nodeItem.type === 51) {
          node.shape = 'custom-polygon';
          node.width = 80;
          node.height = 98;
          let attrs = {
            circle: {
              r: 6,
              stroke: '#A7B0BB',
              magnet: true,
              fill: '#fff'
            }
          };
          node.ports = {
            groups: {
              top: {
                position: 'top',
                attrs
              },
              right: {
                position: 'right',
                attrs
              },
              bottom: {
                position: 'bottom',
                attrs
              },
              left: {
                position: 'left',
                attrs
              }
            },
            items: [
              {
                group: 'top'
              },
              {
                group: 'bottom'
              },
              {
                group: 'left'
              },
              {
                group: 'right'
              }
            ]
          };
        } else if ([10, 11].includes(nodeItem.type)) {
          node.shape = 'custom-circle';
          node.width = 92;
          node.height = 36;
        } else if ([60, 61].includes(nodeItem.type)) {
          node.shape = 'custom-circle';
          node.width = nodeItem.type == 60 ? 172 : 123;
          node.height = 36;
        }
      } else {
        // 新ui
        // 节点
        let attrs = {
          circle: {
            r: 6,
            stroke: '#ccc',
            magnet: true,
            fill: '#fff'
          }
        };
        node = {
          shape: 'custom-vue-shape',
          x: x,
          y: y,
          id,
          width: 200,
          height: 36,
          data: JSON.stringify(nodeItem),
          ports: {
            groups: {
              top: {
                position: 'top',
                attrs
              },
              right: {
                position: 'right',
                attrs
              },
              bottom: {
                position: 'bottom',
                attrs
              },
              left: {
                position: 'left',
                attrs
              }
            },
            items: [
              {
                group: 'top'
              },
              {
                group: 'bottom'
              },
              {
                group: 'left'
              },
              {
                group: 'right'
              }
            ]
          }
        };
      }

      graph.value.addNode(node);

      let node111 = graph.value.getNodes().find(({ id }) => {
        return id === node.id;
      });

      let ports = node111.getPorts();

      // 获取当前添加节点的点
      let targetPort = '';

      if (type == 'top') {
        targetPort = ports.find(item => item.group == 'bottom').id;
      } else if (type == 'right') {
        targetPort = ports.find(item => item.group == 'left').id;
      } else if (type == 'bottom') {
        targetPort = ports.find(item => item.group == 'top').id;
      } else if (type == 'left') {
        targetPort = ports.find(item => item.group == 'right').id;
      }

      let edgeData = {};


      if (edgeType.value == 251) {
        // 带条件
        edgeData = conditionEdgeData;
      } else if (edgeType.value == 252) {
        // 无逻辑
        edgeData = IllogicalEdgeData;
      } else if (edgeType.value == 253) {
        // 带消息
        edgeData = noticeEdgeData;
      } else if (edgeType.value == 254) {
        // 关联1
        edgeData = relation1EdgeData;
      } else if (edgeType.value == 255) {
        // 关联2
        edgeData = relation2EdgeData;
      } else {
        // 默认箭头
        edgeData = defaultEdgeData;
      }

      // 线
      graph.value.addEdge({
        source: {
          cell: nodeData.id,
          port: currentNodeData.groupList.find(gItem => gItem.name == type).id
        },
        target: {
          cell: node.id,
          port: targetPort
        },
        ...edgeData
      });

      // 清空加号
      let noNodeData = {
        nodeId: '',
        groupList: []
      };
      workflowStore.setNodeData(noNodeData);
    }
  };

  return {
    graph,
    canAdd,
    edgeType,
    addNodeItem
  };
}
