
import { register } from "@antv/x6-react-shape";
import { Node } from "@antv/x6";
import CustomInput from "../../CustomInput";
import { useSizeHook, useShapeProps } from "../../../hooks";
const component = ({ node }: { node: Node }) => {
  const { label, text, fill, stroke, opacity, name } = node.getData();
  const { size, ref } = useSizeHook();
  const {
    fillContent,
    defsContent,
    strokeColor,
    strokeWidth,
    strokeDasharray,
  } = useShapeProps(fill, size, stroke);

  const { width, height } = size;

  const handleSetName = (val: string) => {
    node.setData({ name: val });
  };

  const handleSetLabel = (val: string) => {
    node.setData({ label: val });
  };

  return (
    <>
      <div
        className="relative text-0 w-full h-full"
        ref={ref}
        style={{ opacity: opacity / 100 }}
      >
        {/* 名称 */}
        <div className="absolute w-full h-25px top-0 left-0 pl-8px">
          <CustomInput value={name} styles={text} node={node} notSetData={true} onChange={handleSetName} />
        </div>

        {/* 文本 */}
        <div className="absolute w-full top-25px left-0" style={{height: height - 25}}>
          <CustomInput value={label} styles={text} node={node} notSetData={true} onChange={handleSetLabel} />
        </div>
        
        <svg
          className="w-full h-full"
          viewBox={`0 0 ${size?.width} ${size?.height}`}
          xmlns="http://www.w3.org/2000/svg"
        >
          <defs>{defsContent}</defs>
          <rect
            x={strokeWidth}
            y={strokeWidth}
            rx={8}
            ry={8}
            width={width - 4}
            height={height - 4}
            fill={fillContent}
            stroke={strokeColor}
            strokeDasharray={strokeDasharray}
            strokeWidth={strokeWidth}
          />
          <path
            d={`
              M ${strokeWidth}, 25
              L ${width * 0.3}, 25
              L ${width * 0.3 + 8}, 17
              L ${width * 0.3 + 8}, ${strokeWidth}
            `}
            fill={fillContent}
            stroke={strokeColor}
            strokeDasharray={strokeDasharray}
            strokeWidth={strokeWidth}
          />
        </svg>
      </div>
    </>
  );
};

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

const combinedFragment = {
  name: "组合片段",
  icon: require("./image/combinedFragment.png"),
  node: {
    shape: "custom-react-combinedFragment",
    data: {
      label: "[Condition]",
      name: "Opt|Alt|Loop",
      text: {
        textAlign: 'left',
      }
    },
  },
};

export default combinedFragment;
