import { CompoundedComponent } from "@/types";
import { register } from "@antv/x6-react-shape";
import { Node } from "@antv/x6";
import CustomInput from "@/components/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"
        style={{ opacity: opacity / 100 }}
        ref={ref}
      >
        <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>
          <ellipse
            cx={size?.width / 2}
            cy={size?.height / 2}
            rx={size?.width ? size.width / 2 - strokeWidth : 0}
            ry={size?.height  ? size.height /  2 - strokeWidth : 0}
            fill={fillContent}
            stroke={strokeColor}
            strokeDasharray={strokeDasharray}
            strokeWidth={strokeWidth}
          />
        </svg>
      </div>
    </>
  );
};

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

const ovalContainer: CompoundedComponent = {
  name: "容器",
  icon: require("./image/ovalContainer.png"),
  node: {
    shape: "custom-react-ovalContainer",
    data: {
      label: "",
    },
  },
};

export default ovalContainer;
