import { CompoundedComponent } from "@/types";
import { register } from "@antv/x6-react-shape";
import { 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);

  const { width, height } = size;

  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 ${width} ${height}`}
          xmlns="http://www.w3.org/2000/svg"
        >
          <defs>{defsContent}</defs>
          <polygon
            points={`
              ${width*0.5-Math.min(width,height)/8},${strokeWidth} 
              ${width*0.5+Math.min(width,height)/8},${strokeWidth}
              ${width*0.5+Math.min(width,height)/8},${height*0.5-Math.min(width,height)/8}
              ${width-strokeWidth},${height*0.5-Math.min(width,height)/8}
              ${width-strokeWidth},${height*0.5+Math.min(width,height)/8}
              ${width*0.5+Math.min(width,height)/8},${height*0.5+Math.min(width,height)/8}
              ${width*0.5+Math.min(width,height)/8},${height-strokeWidth}
              ${width*0.5-Math.min(width,height)/8},${height-strokeWidth}
              ${width*0.5-Math.min(width,height)/8},${height*0.5+Math.min(width,height)/8}
              ${strokeWidth},${height*0.5+Math.min(width,height)/8}
              ${strokeWidth},${height*0.5-Math.min(width,height)/8}
              ${width*0.5-Math.min(width,height)/8},${height*0.5-Math.min(width,height)/8}
            `}
            fill={fillContent}
            stroke={strokeColor}
            strokeDasharray={strokeDasharray}
            strokeWidth={strokeWidth}
          />
        </svg>
      </div>
    </>
  );
};

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

const cross: CompoundedComponent = {
  name: "十字形",
  icon: require("./image/cross.png"),
  node: {
    shape: "custom-react-cross",
    data: {
      label: "",
      ...defaultData,
    },
    ports,
  },
};

export default cross;
