
import { register } from "@antv/x6-react-shape";
import { Graph, Node } from "@antv/x6";
import { ports } from "../../config/data";
import CustomInput from "../CustomInput";
import { useSizeHook, useShapeProps } from "../../hooks";
const component = ({ node }: { node: Node }) => {
  const { label, text, fill, stroke, opacity } = node.getData();
  const { size, ref } = useSizeHook();
  const {
    fillContent,
    defsContent,
    strokeColor,
    strokeWidth,
    strokeDasharray,
  } = useShapeProps(fill, size, stroke);
  // 计算正五边形的顶点坐标
  const getD = (width: number, height: number) => {
    let path = `
      M ${width / 2} ${strokeWidth}
      L ${width * 0.62},${height * 0.38}
      L ${width - strokeWidth},${ height * 0.38}
      L ${width * 0.7},${height * 0.62}
      L ${width * 0.82},${height - strokeWidth}
      L ${width / 2},${ height * 0.76}
      L ${width * 0.18},${height - strokeWidth}
      L ${width * 0.3},${height * 0.62}
      L ${strokeWidth},${height * 0.38}
      L ${width * 0.38},${ height * 0.38}
      L ${width / 2},${strokeWidth} Z
    `;

    return path;
  };

  return (
    <>
      <div
        className="relative text-0 w-full h-full"
        ref={ref}
        style={{ opacity: opacity / 100 }}
      >
        <CustomInput value={label} styles={text} node={node} />
        <svg
          className="w-full h-full"
          viewBox={`0 0 ${size?.width} ${size?.height}`}
          xmlns="http://www.w3.org/2000/svg"
        >
          <defs>{defsContent}</defs>
          <path
            d={getD(size?.width, size?.height)}
            fill={fillContent}
            stroke={strokeColor}
            strokeDasharray={strokeDasharray}
            strokeWidth={strokeWidth}
          />
        </svg>
      </div>
    </>
  );
};

register({
  shape: "custom-react-fiveStar",
  width: 90,
  height: 90,
  effect: ["data"],
  component: component,
});

// 左侧连接桩
Graph.registerPortLayout("fiveStarLeft", (portsPositionArgs, elemBBox) => {
  return portsPositionArgs.map((item) => {
    return {
      ...item,
      position: {
        x: 0,
        y: elemBBox.height * 0.4,
      },
    };
  });
});

// 右侧连接桩
Graph.registerPortLayout("fiveStarRight", (portsPositionArgs, elemBBox) => {
  return portsPositionArgs.map((item) => {
    return {
      ...item,
      position: {
        x: elemBBox.width,
        y: elemBBox.height * 0.4,
      },
    };
  });
});

const FiveStar = {
  name: "五角星",
  icon: require("./image/pentagon.png"),
  node: {
    shape: "custom-react-fiveStar",
    data: {
      label: "",
    },
    ports: {
      ...ports,
      groups: {
        ...ports.groups,
        fiveStarLeft: {
          position: "fiveStarLeft",
          attrs: {
            circle: {
              r: 4,
              magnet: true,
              stroke: "#5F95FF",
              strokeWidth: 1,
              fill: "#fff",
              style: {
                visibility: "hidden",
              },
            },
          },
        },
        fiveStarRight: {
          position: "fiveStarRight",
          attrs: {
            circle: {
              r: 4,
              magnet: true,
              stroke: "#5F95FF",
              strokeWidth: 1,
              fill: "#fff",
              style: {
                visibility: "hidden",
              },
            },
          },
        },
      },
      items: [
        {
          group: "top",
          args: {
            dy: 2,
          },
        },
        {
          group: "fiveStarLeft",
        },
        {
          group: "bottom",
          args: {
            dy: -2,
          },
        },
        {
          group: "fiveStarRight",
        },
      ],
    },
  },
};

export default FiveStar;
