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 getD = () => {
    const { width: w, height: h } = size;

    return `
      M ${strokeWidth},${Math.min(h/8,w/12)}
      Q ${w*1/4},${3*Math.min(h/8,w/12) + strokeWidth} ${w/2 + strokeWidth - strokeWidth},${Math.min(h/8,w/12) + strokeWidth}
      t ${w/2 - strokeWidth},0
      L ${w-strokeWidth},${h-Math.min(h/8,w/12)}
      Q ${w*3/4},${h-3*Math.min(h/8,w/12) + strokeWidth} ${w/2 + strokeWidth},${h-Math.min(h/8,w/12)}
      t ${-w/2},0
      L ${strokeWidth},${Math.min(h/8,w/12)}
      Z
    `
  }

  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>
          <path
            d={getD()}
            fill={fillContent}
            stroke={strokeColor}
            strokeDasharray={strokeDasharray}
            strokeWidth={strokeWidth}
          />
        </svg>
      </div>
    </>
  );
};

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

const paperTape: CompoundedComponent = {
  name: "条带",
  icon: require("./image/paperTape.png"),
  node: {
    shape: "custom-react-paperTape",
    data: {
      label: "",
      ...defaultData,
    },
    ports,
  },
};

export default paperTape;
