import { CompoundedComponent } from "@/types";
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);

  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>
          <polygon
            points={`
              ${size?.width / 2},${strokeWidth} 
              ${size?.width - strokeWidth},${size?.height - strokeWidth} 
              ${strokeWidth},${size?.height - strokeWidth}
            `}
            fill={fillContent}
            stroke={strokeColor}
            strokeDasharray={strokeDasharray}
            strokeWidth={strokeWidth}
          />
        </svg>
      </div>
    </>
  );
};

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

// 左侧连接桩
Graph.registerPortLayout("triangleLeft", (portsPositionArgs, elemBBox) => {
  return portsPositionArgs.map((item) => {
    return {
      ...item,
      position: {
        x: elemBBox.width / 4,
        y: elemBBox.height / 2,
      },
    };
  });
});

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

const Triangle: CompoundedComponent = {
  name: "三角形",
  icon: require("./image/triangle.png"),
  node: {
    shape: "custom-react-triangle",
    data: {
      label: "",
    },
    ports: {
      ...ports,
      groups: {
        ...ports.groups,
        triangleLeft: {
          position: "triangleLeft",
          attrs: {
            circle: {
              r: 4,
              magnet: true,
              stroke: "#5F95FF",
              strokeWidth: 1,
              fill: "#fff",
              style: {
                visibility: "hidden",
              },
            },
          },
        },
        triangleRight: {
          position: "triangleRight",
          attrs: {
            circle: {
              r: 4,
              magnet: true,
              stroke: "#5F95FF",
              strokeWidth: 1,
              fill: "#fff",
              style: {
                visibility: "hidden",
              },
            },
          },
        },
      },
      items: [
        {
          group: "top",
          args: {
            dy: 2,
          },
        },
        {
          group: "triangleRight",
        },
        {
          group: "bottom",
          args: {
            dy: -2,
          },
        },
        {
          group: "triangleLeft",
        },
      ],
    },
  },
};

export default Triangle;
