import React, { useState, useEffect, useRef } from 'react';
import { BaseEdge, EdgeLabelRenderer, EdgeProps, getSmoothStepPath, useReactFlow } from '@xyflow/react';
import EdgeAddNodeButton from '@/pages/flowEditor/components/edgeAddNodeButton';
import { getTopicList } from '@/api/event';
import { useSelector } from 'react-redux';
import { Message } from '@arco-design/web-react';

type DataDisplayEdgeData = {
  label?: string;
  value?: any;
  name?: string;
  topic?: string;
  eventId?: string;
  lineType?: string; // 添加lineType属性
};

const DataDisplayEdge: React.FC<EdgeProps> = ({
                                                id,
                                                sourceX,
                                                sourceY,
                                                targetX,
                                                targetY,
                                                sourcePosition,
                                                targetPosition,
                                                style = {},
                                                markerEnd,
                                                selected,
                                                data
                                              }) => {
  const [isOpen, setIsOpen] = useState(false);
  const [selectedValue, setSelectedValue] = useState('');
  const dropdownRef = useRef<HTMLDivElement>(null);
  const [edgePath, labelX, labelY] = getSmoothStepPath({
    sourceX,
    sourceY,
    sourcePosition,
    targetX,
    targetY,
    targetPosition,
    borderRadius: 8 // 设置圆角半径
  });
  const { info, eventTopicList } = useSelector((state: any) => state.ideContainer);

  // 从数据中获取悬停状态
  const hovered = data?.hovered || false;

  // 从数据中获取要显示的信息
  const displayData: DataDisplayEdgeData = data?.displayData || {};

  // 获取lineType
  const lineType = data?.lineType || 'data'; // 默认为data类型

  // 使用useReactFlow钩子获取setEdges方法
  const { setEdges } = useReactFlow();

  // 边点击处理函数
  const handleEdgeAddNode = (e) => {
    // 如果是数据类型边线，不允许添加节点
    if (lineType === 'data') {
      console.warn('数据类型边线上不允许添加节点');
      Message.warning('数据类型连接线上不允许添加节点');
      return;
    }

    // 更新边的数据，触发边上添加节点的流程
    setEdges(eds => eds.map(edge => {
      if (edge.id === id) {
        return {
          ...edge,
          data: {
            ...edge.data,
            addNodeTrigger: true,
            clientX: e.clientX,
            clientY: e.clientY
          }
        };
      }
      return edge;
    }));
  };

  useEffect(() => {
    const handleClickOutside = (event: MouseEvent) => {
      if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
        setIsOpen(false);
      }
    };

    document.addEventListener('mousedown', handleClickOutside);
    return () => {
      document.removeEventListener('mousedown', handleClickOutside);
    };
  }, []);

  const handleSelect = (option) => {
    setSelectedValue(option.label);
    setIsOpen(false);

    // 查找所有具有相同事件ID和连接点的边并同步更新
    setEdges(eds => eds.map((edge: any) => {
      // 检查边是否有displayData且与当前选择的事件匹配
      if (edge.data?.displayData &&
        edge.data.displayData.eventId === displayData.eventId &&
        edge.data.displayData.topic === displayData.topic) {
        return {
          ...edge,
          data: {
            ...edge.data,
            displayData: {
              ...edge.data.displayData,
              name: option.label,
              topic: option.value
            }
          }
        };
      }
      return edge;
    }));
  };

  return (
    <>
      <BaseEdge
        path={edgePath}
        markerEnd={markerEnd}
        style={{
          stroke: selected ? '#1890ff' : '#b1b1b7',
          strokeWidth: selected ? 2 : 1,
          ...style
        }}
      />
      <EdgeLabelRenderer>
        <div
          style={{
            position: 'absolute',
            transform: `translate(-50%, -50%) translate(${labelX}px,${labelY}px)`,
            fontSize: 12,
            pointerEvents: 'all',
            opacity: style?.opacity || 1 // 应用透明度样式到标签容器
          }}
          className="nodrag nopan"
        >
          {/* 数据展示框 */}
          {displayData && Object.keys(displayData).length > 0 && (
            <div
              ref={dropdownRef}
              style={{
                width: 150,
                border: isOpen ? '1px solid #1890ff' : '1px solid #d9d9d9',
                borderRadius: 4,
                backgroundColor: '#fff',
                position: 'relative',
                opacity: style?.opacity || 1 // 应用透明度样式到下拉框
              }}
            >
              <div
                onClick={() => setIsOpen(!isOpen)}
                style={{
                  padding: '4px 11px',
                  cursor: 'pointer',
                  display: 'flex',
                  justifyContent: 'space-between',
                  alignItems: 'center'
                }}
              >
                <span>{selectedValue || displayData.name}</span>
                <span>{isOpen ? '▲' : '▼'}</span>
              </div>

              {isOpen && (
                <div
                  style={{
                    position: 'absolute',
                    top: '105%',
                    left: 0,
                    right: 0,
                    border: '1px solid #d9d9d9',
                    borderTop: 'none',
                    borderRadius: '0 0 4px 4px',
                    backgroundColor: '#fff',
                    zIndex: 1000,
                    maxHeight: 200,
                    overflowY: 'auto',
                    opacity: style?.opacity || 1 // 应用透明度样式到下拉列表
                  }}
                >
                  {eventTopicList.map((option: { value: string; label: string }) => (
                    <div
                      key={option.value}
                      onClick={() => handleSelect(option)}
                      style={{
                        padding: '5px 12px',
                        cursor: 'pointer',
                        borderBottom: '1px solid #f0f0f0'
                      }}
                      onMouseEnter={(e) => e.currentTarget.style.backgroundColor = '#f5f5f5'}
                      onMouseLeave={(e) => e.currentTarget.style.backgroundColor = '#fff'}
                    >
                      {option.label}
                    </div>
                  ))}
                </div>
              )}
            </div>
          )}

          {hovered && Object.keys(displayData).length === 0 && lineType !== 'data' && (
            <EdgeAddNodeButton
              onClick={(e) => handleEdgeAddNode(e)}
              style={{ opacity: style?.opacity || 1 }} // 应用透明度样式到添加节点按钮
            />
          )}
        </div>
      </EdgeLabelRenderer>
    </>
  );
};

export default DataDisplayEdge;