import { CompoundedComponent } from "@/types";
import { register } from "@antv/x6-react-shape";
import { Graph, Node } from "@antv/x6";
import { ports, defaultData } 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="40,2 78,69 2,69"
            points={`
              ${strokeWidth},${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-rightTriangle",
  width: 80,
  height: 70,
  effect: ["data"],
  component: component,
});

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

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

export default RightTriangle;
