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, text1, 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 }}
      >
        <svg
          className="w-full h-full absolute top-0 left-0"
          viewBox={`0 0 ${size?.width} ${size?.height}`}
          xmlns="http://www.w3.org/2000/svg"
        >
          <defs>{defsContent}</defs>
          <rect
            x={strokeWidth}
            y={strokeWidth}
            width={size?.width - strokeWidth * 2}
            height={size?.height - strokeWidth * 2}
            rx={4}
            ry={4}
            fill={fillContent}
            stroke={strokeColor}
            strokeDasharray={strokeDasharray}
            strokeWidth={strokeWidth}
          />
          <path
            d={`
              M ${strokeWidth}, 47
              L ${size?.width - strokeWidth}, 47
            `}
            stroke={strokeColor}
            strokeWidth={strokeWidth}
          />
        </svg>

        {/* 类名 */}
        <div className="w-full h-47px relative">
          <CustomInput value={label} styles={text} node={node} />
        </div>
        {/* 方法 */}
        <div
          className="w-full relative"
          style={{
            height: size.height - 47 - 3 * strokeWidth,
            marginTop: strokeWidth,
          }}
        >
          <CustomInput
            value={text1}
            styles={{
              ...text,
              textAlign: "left",
              bold: false,
            }}
            node={node}
            notSetData={true}
            onChange={(val) => {
              node.setData({ text1: val });
            }}
          />
        </div>
      </div>
    </>
  );
};

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

const enumeration: CompoundedComponent = {
  name: "枚举",
  icon: require("./image/enumeration.png"),
  node: {
    shape: "custom-react-enumeration",
    data: {
      label: "枚举名称",
      text1: "枚举值",
      text: {
        bold: true,
      },
    },
  },
};

export default enumeration;
