import { CompoundedComponent } from "@/types";
import { register } from "@antv/x6-react-shape";
import { Graph, Node } from "@antv/x6";
import { ports } 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) => {
    let path = `M ${width * 0.4} ${height * 0.1}
      Q ${width * 0.65},${width * -0.1} ${width * 0.75},${height * 0.15}
      Q ${width * 1.1},${height * 0.1} ${width * 0.9},${height * 0.55}
      Q ${width * 1},${height * 0.9} ${width * 0.77},${height * 0.9}
      Q ${width * 0.6},${height * 1.07} ${width * 0.43},${height * 0.87}
      Q ${width * 0.1},${height * 1} ${width * 0.12},${height * 0.7}
      Q ${width * -0.08},${height * 0.5} ${width * 0.08},${height * 0.3}
      Q ${width * 0.1},${height * 0} ${width * 0.4},${height * 0.1}
    `;
    return path + " 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={generatePath(size?.width, size?.height)}
            fill={fillContent}
            stroke={strokeColor}
            strokeDasharray={strokeDasharray}
            strokeWidth={strokeWidth}
          />
        </svg>
      </div>
    </>
  );
};

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

// 左侧连接桩
Graph.registerPortLayout("cloudLeft", (portsPositionArgs, elemBBox) => {
  return portsPositionArgs.map((item) => {
    return {
      ...item,
      position: {
        x: 0,
        y: elemBBox.height / 2,
      },
    };
  });
});

// 右侧连接桩
Graph.registerPortLayout("cloudRight", (portsPositionArgs, elemBBox) => {
  return portsPositionArgs.map((item) => {
    return {
      ...item,
      position: {
        x: elemBBox.width,
        y: elemBBox.height / 2,
      },
    };
  });
});

const Cloud: CompoundedComponent = {
  name: "云",
  icon: require("./image/cloud.png"),
  node: {
    shape: "custom-react-cloud",
    data: {
      label: "",
    },
    ports: {
      ...ports,
      groups: {
        ...ports.groups,
        cloudLeft: {
          position: "cloudLeft",
          attrs: {
            circle: {
              r: 4,
              magnet: true,
              stroke: "#5F95FF",
              strokeWidth: 1,
              fill: "#fff",
              style: {
                visibility: "hidden",
              },
            },
          },
        },
        cloudRight: {
          position: "cloudRight",
          attrs: {
            circle: {
              r: 4,
              magnet: true,
              stroke: "#5F95FF",
              strokeWidth: 1,
              fill: "#fff",
              style: {
                visibility: "hidden",
              },
            },
          },
        },
      },
      items: [
        {
          group: "top",
          args: {
            dy: 2,
          },
        },
        {
          group: "cloudRight",
        },
        {
          group: "bottom",
          args: {
            dy: -2,
          },
        },
        {
          group: "cloudLeft",
        },
      ],
    },
  },
};

export default Cloud;
