<template>
  <a-dropdown :trigger="['contextmenu']" @openChange="handleVisibleChange">
    <a-card
      :class="[
        'ks-designer-node',
        `ks-designer-${element?.category ?? 'model'}-node`
      ]"
      hoverable
    >
      <template #title>
        <a-space>
          <div class="port port-in" data-port="in-0" magnet="passive"></div>
          <span class="ks-designer-node-title">{{ element?.name ?? '-' }}</span>
          <div class="port port-out" data-port="out-0" magnet="active"></div>
        </a-space>
      </template>

      <div class="w-full">
        <a-tooltip>
          <template #title>
            {{ element?.description ?? element?.name }}
          </template>
          <p>
            {{ substring(element?.description ?? (element?.name ?? '-'), 40) }}
          </p>
        </a-tooltip>
      </div>
    </a-card>

    <template #overlay>
      <a-menu @click="handleMenuClick">
        <a-menu-item key="delete">
          <template #icon>
            <DeleteOutlined />
          </template>
          删除
        </a-menu-item>
      </a-menu>
    </template>
  </a-dropdown>
</template>

<script lang="ts">
import { defineComponent, onMounted, onUnmounted, ref } from 'vue';
import { elementProps } from './props';
import type { ModelElement } from './element';
import { DeleteOutlined, SettingOutlined } from '@ant-design/icons-vue';
import type { Graph } from '@antv/x6';
import { substring } from '@/utils/strings';

export default defineComponent({
  name: 'ModelElement',
  components: {
    SettingOutlined,
    DeleteOutlined,
  },
  props: elementProps,
  setup(_props) {
    const element = ref<ModelElement | null>(
      _props.node ? (_props.node.getData() as ModelElement) : null,
    );
    const updateKey = ref(0);
    const isMenuVisible = ref(false);

    // 获取画布实例
    const getGraph = (): Graph | null => {
      return _props.graph as Graph || null;
    };

    // 监听节点数据变化
    const handleDataChange = () => {
      if (_props.node) {
        element.value = _props.node.getData() as ModelElement;
      } else {
        element.value = null;
      }
      updateKey.value++;
    };

    const handleVisibleChange = (visible: boolean) => {
      isMenuVisible.value = visible;
    };

    const handleMenuClick = ({ key }: { key: string }) => {
      if (key === 'delete') {
        handleDelete();
      }
    };

    const handleDelete = () => {
      if (!_props.node) return;

      const graph = getGraph();
      if (graph) {
        try {
          // 先删除关联边
          const connectedEdges = graph.getConnectedEdges(_props.node);
          connectedEdges.forEach(edge => graph.removeEdge(edge));
          // 再删除节点
          graph.removeNode(_props.node);
          console.info(`节点 ${_props.node.id} 已删除`);
        } catch (error) {
          console.error('删除节点失败:', error);
        }
      }
      isMenuVisible.value = false;
    };

    onMounted(() => {
      _props.node?.on('change:data', handleDataChange);
    });

    onUnmounted(() => {
      _props.node?.off('change:data', handleDataChange);
    });

    return {
      element,
      substring,
      handleMenuClick,
      handleVisibleChange,
    };
  },
});
</script>

<style lang="less">
.ks-designer-node {
  background: linear-gradient(150deg, rgba(108, 99, 255) 1%, rgba(108, 99, 255) 100%);
  border-radius: 8px;
  width: 100%;
  height: 100%;
  cursor: pointer;
  position: relative;
  background: #1e2533;
  border: 1px solid #4a7aff;

  &:hover {
    border: 1px solid #4a7aff;
    box-shadow: 0 0 10px rgba(74, 122, 255, 0.3);
  }

  .ant-card-head {
    border: 0;
    height: 38px;
    min-height: 38px;
    border-radius: 0;
    color: #fff;
    font-size: 12px;
    font-weight: normal;
    padding: 0 20px;
    background: linear-gradient(to bottom, #3a4c70, #2d3a56);
    border-top-left-radius: 8px;
    border-top-right-radius: 8px;
    background: linear-gradient(to bottom, rgba(108, 99, 255, 0.15), rgba(108, 99, 255, 0.05));
    //background: linear-gradient(to bottom, rgb(234 234 234 / 20%), rgb(191 191 191 / 58%));
  }

  .ks-designer-node-icon {
    width: 15px;
    height: 15px;
    display: block;
    position: absolute;
    left: 8px;
    top: 13px;
    background: url('@/assets/icons/model-4.svg') center / 100% 100%;
  }

  .ks-designer-node-title {
    font-size: 13px;
    color: #fff;
  }

  .ant-card-body {
    color: #f5f5f5;
    height: calc(100% - 38px);
    border-radius: 0;
    font-size: 12px;
    padding: 8px 15px;
    border-top: 1px solid rgba(108, 99, 255, 0.5);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    box-shadow: 0 0 10px rgba(74, 122, 255, 0.3);
  }

  // model/task 节点：浅紫渐变
  //&.ks-designer-model-node,
  //&.ks-designer-task-node {
  //  background: linear-gradient(150deg, rgba(92,84,247,0.9) 1%, rgba(115,108,250,0.7) 55%);
  //
  //  .ant-card-body {
  //    border-top: 1px solid rgba(92,84,247,0.5);
  //  }
  //
  //  .ks-designer-node-icon {
  //    background: url('@/assets/icons/m-02.png') center / 100% 100%;
  //  }
  //}
  //
  //// input 节点：深紫渐变
  //&.ks-designer-input-node {
  //  background: linear-gradient(150deg, rgba(82,73,245,0.9) 1%, rgba(105,98,249,0.7) 55%);
  //
  //  .ant-card-body {
  //    border-top: 1px solid rgba(82,73,245,0.5);
  //  }
  //
  //  .ks-designer-node-icon {
  //    background: url('@/assets/icons/icon-model-input.png') center / 100% 100%;
  //  }
  //}
  //
  //// action 节点：亮紫渐变（对应之前的action-node）
  //&.ks-designer-action-node {
  //  background: linear-gradient(150deg, rgba(108,99,255,0.9) 1%, rgba(140,133,255,0.7) 55%);
  //
  //  .ant-card-body {
  //    border-top: 1px solid rgba(108,99,255,0.5);
  //  }
  //
  //  .ks-designer-node-icon {
  //    background: url('@/assets/icons/bg-fk-point.png') center / 100% 100%;
  //  }
  //}
  //
  //// precondition/component 节点：蓝紫渐变
  //&.ks-designer-precondition-node,
  //&.ks-designer-component-node {
  //  background: linear-gradient(150deg, rgba(72,64,243,0.9) 1%, rgba(95,88,248,0.7) 55%);
  //
  //  .ant-card-body {
  //    border-top: 1px solid rgba(72,64,243,0.5);
  //  }
  //}
  //
  //// select/control 节点：浅蓝紫渐变
  //&.ks-designer-select-node,
  //&.ks-designer-control-node {
  //  background: linear-gradient(150deg, rgba(90,82,246,0.9) 1%, rgba(118,111,251,0.7) 55%);
  //
  //  .ant-card-body {
  //    border-top: 1px solid rgba(90,82,246,0.5);
  //  }
  //
  //  .ks-designer-node-icon {
  //    background: url('@/assets/icons/bg-model-builder-card-title.png') center / 100% 100%;
  //  }
  //}

  // 连接桩容器样式
  .ks-designer-node-content {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 4px;
  }

  .ks-designer-node-row {
    width: 100%;
    display: flex;
    align-items: center;
    position: relative;
    min-height: 24px;
  }

  .port {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    cursor: crosshair;
    flex-shrink: 0;
    box-shadow: 0 0 0 2px rgb(108, 99, 255, 0.8);
    z-index: 10;
    magnet: true;
  }

  // 左侧入桩样式
  .port-in {
    background-color: #6C63FF;
    margin-right: 8px;
    //border: 1px solid #093866;
    magnet: passive;
    box-shadow: none;
    width: 15px;
    height: 15px;
    display: block;
    background: url('@/assets/icons/point.svg') center / 100% 100%;

    position: absolute;
    left: 5px;
    top: 12px;
  }

  .port-out {
    margin-left: 8px;
    margin-right: 5px;
    magnet: active;
    box-shadow: none;
    width: 15px;
    height: 15px;
    display: block;
    background: url('@/assets/icons/point.svg') center / 100% 100%;

    position: absolute;
    right: 5px;
    top: 12px;
  }

  // 节点文本样式
  .ks-designer-node-name {
    flex: 1;
    line-height: 24px;
    overflow: hidden;
    text-overflow: ellipsis;
    //white-space: nowrap;
  }
}
</style>