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 generatePath = (width: number, height: number, strokeWidth: number) => {
    let path = "";
    // 开始位置
    path += `M${width * 0.117},${height * 0.81995}`;
    path += `A${(width - 2 * strokeWidth) / 2} ${(height - 2 * strokeWidth) / 2} 0 1 1 ${width / 6} ${height * 0.872}`;
    path += `L${strokeWidth} ${height - strokeWidth}`;
    path += `L${width * 0.117},${height * 0.81995} Z`;

    return path;
  };

  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>
          <path
            d={generatePath(size?.width, size?.height, strokeWidth)}
            fill={fillContent}
            stroke={strokeColor}
            strokeDasharray={strokeDasharray}
            strokeWidth={strokeWidth}
          />
        </svg>
      </div>
    </>
  );
};

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

const Comment: CompoundedComponent = {
  name: "评论",
  icon: require("./image/comment.png"),
  node: {
    shape: "custom-react-comment",
    data: {
      label: "",
      ...defaultData,
    },
    ports,
  },
};

export default Comment;
