<template>
  <div class="relation-graph-wrapper">
    <div class="graph-controls">
      <!-- 这项政令标志着中美AI竞争进入一个新阶段，其核心特征是 “精准封锁”与“体系输出”相结合。它短期内无疑会给中国AI产业链带来压力，但长期看，这场竞争更可能是一场围绕技术路线、生态系统和治理规则的持久战。 -->
      <div v-for="item in controlBtns" :key="item.type" :class="['control-btn', { 'control-btn-active': currentLayoutType === item.type }]" @click="handleClickControlBtn(item.type)">
        <img :src="item.icon" alt="" />
      </div>
    </div>
    <div ref="containerRef" class="graph-container"></div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, watch, nextTick } from 'vue'
import G6 from '@antv/g6'
import { Close } from '@element-plus/icons-vue'
import echartsIcon01 from './assets/images/echartsicon01.png'
import echartsIcon02 from './assets/images/echartsicon02.png'
import echartsIcon03 from './assets/images/echartsicon03.png'

const props = defineProps({
  graphData: {
    type: Object,
    default: () => ({ nodes: [], links: [] })
  },
  treeData: {
    type: Object,
    default: () => null
  },
  controlActive: {
    type: Number,
    default: 1
  }
})

const emit = defineEmits(['nodeClick', 'layoutChange'])

const containerRef = ref(null)
const graphInstance = ref(null)
const currentLayoutType = ref(1)

const controlBtns = [
  { type: 1, icon: echartsIcon01, name: '力导向布局' },
  { type: 2, icon: echartsIcon02, name: '树布局' },
  { type: 3, icon: echartsIcon03, name: '环状布局' }
]

const initGraph = (layoutType = 1) => {
  if (!containerRef.value) return

  destroyGraph()

  nextTick(() => {
    const width = containerRef.value.offsetWidth || 800
    const height = containerRef.value.offsetHeight || 600

    switch (layoutType) {
      case 1:
        initNormalGraph(layoutType, width, height)
        break
      case 2:
        initTreeGraph(width, height)
        break
      case 3:
        initCircularGraph(width, height)
        break
    }
  })
}

const initNormalGraph = (layoutType, width, height) => {
  const data = processGraphData(props.graphData)

  if (!data.nodes || data.nodes.length === 0) return

  const layout = {
    type: 'force',
    center: [width / 2, height / 2],
    preventOverlap: true,
    nodeSpacing: 80,
    linkDistance: 250,
    nodeStrength: -800,
    edgeStrength: 0.1,
    collideStrength: 0.8,
    alphaDecay: 0.01,
    alphaMin: 0.001
  }

  graphInstance.value = new G6.Graph({
    container: containerRef.value,
    width,
    height,
    fitView: true,
    fitViewPadding: 100,
    fitCenter: true,
    animate: true,
    animateCfg: {
      duration: 300,
      easing: 'easeLinear'
    },
    minZoom: 0.1,
    maxZoom: 10,
    modes: {
      default: [
        'drag-canvas',
        'zoom-canvas',
        'drag-node',
        {
          type: 'activate-relations',
          trigger: 'mouseenter',
          resetSelected: true
        }
      ]
    },
    layout,
    defaultNode: {
      type: 'image',
      size: 40,
      clipCfg: {
        show: true,
        type: 'circle',
        r: 20
      },
      labelCfg: {
        position: 'bottom',
        offset: 10,
        style: {
          fill: '#333',
          fontSize: 11,
          fontFamily: 'Microsoft YaHei',
          textAlign: 'center',
          background: {
            fill: 'rgba(255, 255, 255, 0.95)',
            padding: [4, 6, 4, 6],
            radius: 4
          }
        }
      }
    },
    defaultEdge: {
      type: 'quadratic',
      style: {
        stroke: '#5B8FF9',
        lineWidth: 3,
        opacity: 0.9,
        endArrow: {
          path: 'M 0,0 L 12,6 L 12,-6 Z',
          fill: '#5B8FF9'
        }
      },
      labelCfg: {
        autoRotate: true,
        style: {
          fill: '#333',
          fontSize: 10,
          fontFamily: 'Microsoft YaHei',
          background: {
            fill: '#fff',
            padding: [2, 4, 2, 4],
            radius: 2
          }
        }
      }
    },
    nodeStateStyles: {
      active: {
        shadowColor: '#1459BB',
        shadowBlur: 15,
        stroke: '#1459BB',
        lineWidth: 3
      },
      inactive: {
        opacity: 0.3
      }
    },
    edgeStateStyles: {
      active: {
        stroke: '#1459BB',
        lineWidth: 4
      },
      inactive: {
        opacity: 0.15
      }
    }
  })

  graphInstance.value.data(data)
  graphInstance.value.render()
  bindGraphEvents()
}

const initCircularGraph = (width, height) => {
  const data = processGraphData(props.graphData)

  if (!data.nodes || data.nodes.length === 0) return

  const centerX = width / 2
  const centerY = height / 2
  const radius = Math.min(width, height) / 2 - 120

  const otherNodes = data.nodes.filter(n => !n.isCenter)
  const nodeCount = otherNodes.length

  otherNodes.forEach((node, index) => {
    const angle = (2 * Math.PI * index) / nodeCount - Math.PI / 2
    node.x = centerX + radius * Math.cos(angle)
    node.y = centerY + radius * Math.sin(angle)
  })

  const centerNode = data.nodes.find(n => n.isCenter)
  if (centerNode) {
    centerNode.x = centerX
    centerNode.y = centerY
    centerNode.fx = centerX
    centerNode.fy = centerY
  }

  graphInstance.value = new G6.Graph({
    container: containerRef.value,
    width,
    height,
    fitView: false,
    fitCenter: false,
    animate: true,
    animateCfg: {
      duration: 300,
      easing: 'easeLinear'
    },
    minZoom: 0.1,
    maxZoom: 10,
    modes: {
      default: [
        'drag-canvas',
        'zoom-canvas',
        'drag-node',
        {
          type: 'activate-relations',
          trigger: 'mouseenter',
          resetSelected: true
        }
      ]
    },
    defaultNode: {
      type: 'image',
      size: 40,
      clipCfg: {
        show: true,
        type: 'circle',
        r: 20
      },
      labelCfg: {
        position: 'bottom',
        offset: 10,
        style: {
          fill: '#333',
          fontSize: 11,
          fontFamily: 'Microsoft YaHei',
          textAlign: 'center',
          background: {
            fill: 'rgba(255, 255, 255, 0.95)',
            padding: [4, 6, 4, 6],
            radius: 4
          }
        }
      }
    },
    defaultEdge: {
      type: 'quadratic',
      style: {
        stroke: '#5B8FF9',
        lineWidth: 3,
        opacity: 0.9,
        endArrow: {
          path: 'M 0,0 L 12,6 L 12,-6 Z',
          fill: '#5B8FF9'
        }
      },
      labelCfg: {
        autoRotate: true,
        style: {
          fill: '#333',
          fontSize: 10,
          fontFamily: 'Microsoft YaHei',
          background: {
            fill: '#fff',
            padding: [2, 4, 2, 4],
            radius: 2
          }
        }
      }
    },
    nodeStateStyles: {
      active: {
        shadowColor: '#1459BB',
        shadowBlur: 15,
        stroke: '#1459BB',
        lineWidth: 3
      },
      inactive: {
        opacity: 0.3
      }
    },
    edgeStateStyles: {
      active: {
        stroke: '#1459BB',
        lineWidth: 4
      },
      inactive: {
        opacity: 0.15
      }
    }
  })

  graphInstance.value.data(data)
  graphInstance.value.render()
  bindGraphEvents()
}

const initTreeGraph = (width, height) => {
  const treeDataSource = convertGraphToTree(props.graphData)

  if (!treeDataSource) return

  graphInstance.value = new G6.TreeGraph({
    container: containerRef.value,
    width,
    height,
    fitView: true,
    fitViewPadding: 80,
    animate: true,
    animateCfg: {
      duration: 300,
      easing: 'easeLinear'
    },
    minZoom: 0.1,
    maxZoom: 10,
    modes: {
      default: [
        'drag-canvas',
        'zoom-canvas',
        'drag-node',
        {
          type: 'collapse-expand',
          onChange: function onChange(item, collapsed) {
            const data = item.getModel()
            data.collapsed = collapsed
            return true
          }
        }
      ]
    },
    layout: {
      type: 'compactBox',
      direction: 'LR',
      getId: function getId(d) {
        return d.id
      },
      getHeight: function getHeight() {
        return 16
      },
      getWidth: function getWidth() {
        return 16
      },
      getVGap: function getVGap() {
        return 30
      },
      getHGap: function getHGap() {
        return 120
      }
    },
    defaultNode: {
      type: 'image',
      size: 40,
      clipCfg: {
        show: true,
        type: 'circle',
        r: 20
      },
      labelCfg: {
        position: 'right',
        offset: 10,
        style: {
          fill: '#333',
          fontSize: 11,
          fontFamily: 'Microsoft YaHei',
          background: {
            fill: 'rgba(255, 255, 255, 0.95)',
            padding: [4, 6, 4, 6],
            radius: 4
          }
        }
      }
    },
    defaultEdge: {
      type: 'cubic-horizontal',
      style: {
        stroke: '#5B8FF9',
        lineWidth: 3
      }
    },
    nodeStateStyles: {
      active: {
        shadowColor: '#1459BB',
        shadowBlur: 15,
        stroke: '#1459BB',
        lineWidth: 3
      }
    }
  })

  graphInstance.value.data(treeDataSource)
  graphInstance.value.render()
  graphInstance.value.fitView()
  bindGraphEvents()
}

const convertGraphToTree = (graphData) => {
  if (!graphData || !graphData.nodes || graphData.nodes.length === 0) {
    return null
  }

  const nodes = graphData.nodes
  const links = graphData.links || graphData.edges || []
  const centerNode = nodes[0]
  const centerId = String(centerNode.id || '0')

  const childIdSet = new Set()
  const childrenNodes = []

  links.forEach((link) => {
    const source = String(link.source)
    const target = String(link.target)

    if (source === centerId && !childIdSet.has(target)) {
      const node = nodes.find(n => String(n.id) === target)
      if (node) {
        childIdSet.add(target)
        childrenNodes.push({
          id: target,
          label: node.name || '',
          img: node.image || echartsIcon03,
          size: node.symbolSize || 40,
          name: node.name,
          image: node.image,
          isSanctioned: node.isSanctioned
        })
      }
    } else if (target === centerId && !childIdSet.has(source)) {
      const node = nodes.find(n => String(n.id) === source)
      if (node) {
        childIdSet.add(source)
        childrenNodes.push({
          id: source,
          label: node.name || '',
          img: node.image || echartsIcon03,
          size: node.symbolSize || 40,
          name: node.name,
          image: node.image,
          isSanctioned: node.isSanctioned
        })
      }
    }
  })

  if (childrenNodes.length === 0) {
    nodes.slice(1).forEach((node) => {
      const nodeId = String(node.id)
      if (!childIdSet.has(nodeId)) {
        childIdSet.add(nodeId)
        childrenNodes.push({
          id: nodeId,
          label: node.name || '',
          img: node.image || echartsIcon03,
          size: node.symbolSize || 40,
          name: node.name,
          image: node.image,
          isSanctioned: node.isSanctioned
        })
      }
    })
  }

  return {
    id: centerId,
    label: centerNode.name || '',
    img: centerNode.image || echartsIcon03,
    size: centerNode.symbolSize || 60,
    name: centerNode.name,
    image: centerNode.image,
    isSanctioned: centerNode.isSanctioned,
    children: childrenNodes
  }
}

const processGraphData = (rawData) => {
  if (!rawData || !rawData.nodes || rawData.nodes.length === 0) {
    return { nodes: [], edges: [] }
  }

  const nodeMap = new Map()
  const nodes = []

  rawData.nodes.forEach((node, index) => {
    const nodeId = String(node.id || index)

    if (nodeMap.has(nodeId)) {
      return
    }
    nodeMap.set(nodeId, true)

    const isCenter = index === 0
    const size = node.symbolSize || (isCenter ? 60 : 40)

    nodes.push({
      id: nodeId,
      label: node.name || '',
      img: node.image || echartsIcon03,
      size,
      isCenter,
      clipCfg: {
        show: true,
        type: 'circle',
        r: size / 2
      },
      style: {
        cursor: 'pointer'
      },
      labelCfg: {
        position: 'bottom',
        offset: 12,
        style: {
          fill: isCenter ? '#1459BB' : '#333',
          fontSize: isCenter ? 13 : 11,
          fontWeight: isCenter ? 'bold' : 'normal',
          fontFamily: 'Microsoft YaHei',
          textAlign: 'center'
        }
      },
      ...node,
      id: nodeId
    })
  })

  const edgeMap = new Map()
  const edges = []

  const rawEdges = rawData.links || rawData.edges || []
  rawEdges.forEach((edge, index) => {
    const source = String(edge.source)
    const target = String(edge.target)
    const edgeKey = `${source}-${target}`

    if (edgeMap.has(edgeKey)) {
      return
    }
    if (!nodeMap.has(source) || !nodeMap.has(target)) {
      return
    }
    edgeMap.set(edgeKey, true)

    edges.push({
      id: `edge-${index}`,
      source,
      target,
      label: edge.name || ''
    })
  })

  return { nodes, edges }
}

const bindGraphEvents = () => {
  if (!graphInstance.value) return

  graphInstance.value.on('node:click', (evt) => {
    const node = evt.item
    const model = node.getModel()
    emit('nodeClick', model)
  })

  graphInstance.value.on('canvas:click', () => {
    
  })
}

const handleClickControlBtn = (btn) => {
  currentLayoutType.value = btn
  emit('layoutChange', btn)
  initGraph(btn)
}

const destroyGraph = () => {
  if (graphInstance.value) {
    graphInstance.value.destroy()
    graphInstance.value = null
  }
}

const handleResize = () => {
  if (graphInstance.value && containerRef.value) {
    const width = containerRef.value.offsetWidth
    const height = containerRef.value.offsetHeight
    graphInstance.value.changeSize(width, height)
    graphInstance.value.fitView()
  }
}

watch(
  () => props.graphData,
  () => {
    initGraph(currentLayoutType.value)
  }
)

watch(
  () => props.treeData,
  () => {
    if (currentLayoutType.value === 2) {
      initGraph(2)
    }
  }
)

watch(
  () => props.controlActive,
  (newVal) => {
    if (newVal !== currentLayoutType.value) {
      handleClickControlBtn(newVal)
    }
  }
)

onMounted(() => {
  initGraph(1)
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
  destroyGraph()
})

defineExpose({
  refresh: () => initGraph(currentLayoutType.value),
  changeLayout: (type) => handleClickControlBtn(type),
  getGraph: () => graphInstance.value
})
</script>

<style lang="scss" scoped>
.relation-graph-wrapper {
  position: relative;
  width: 100%;
  height: 100%;
}

.graph-container {
  width: 100%;
  height: 100%;
}

.graph-controls {
  position: absolute;
  top: 16px;
  right: 16px;
  display: flex;
  gap: 8px;
  z-index: 10;

  .control-btn {
    width: 32px;
    height: 32px;
    border-radius: 4px;
    border: 1px solid rgba(234, 236, 238, 1);
    background: rgba(255, 255, 255, 1);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;

    img {
      width: 16px;
      height: 16px;
    }

    &:hover {
      border-color: rgba(5, 95, 194, 0.5);
    }
  }

  .control-btn-active {
    border-color: rgba(5, 95, 194, 1);
    background: rgba(231, 243, 255, 1);
  }
}
</style>