import { CompoundedComponent } from "@/types";
import { register } from "@antv/x6-react-shape";
import { Graph, Node } from "@antv/x6";
import { useSizeHook, useShapeProps } from "@/hooks";
import { useState } from "react";
import Setting from "./Setting";
import Pool from "./Pool";
import { useLane } from "./hooks/useLane";

const component = ({ node, graph }: { node: Node; graph: Graph }) => {
  const {
    fill,
    stroke,
    opacity,
  } = node.getData();

  const [showSetting, setShowSetting] = useState(false);
  const { size, ref } = useSizeHook();
  const { fillContent, strokeColor, strokeWidth } = useShapeProps(
    fill,
    size,
    stroke
  );

  const { handleChangeLane } = useLane(node, graph);

  graph.on("node:selected", (args) => {
    setShowSetting(
      graph.getSelectedCells().length === 1 && node.id === args.node.id
    );
  });
  graph.on("node:unselected", (args) => {
    if (node.id === args.node.id) setShowSetting(false);
  });

  return (
    <>
      {showSetting && <Setting node={node} onChangeLane={handleChangeLane} />}
      <div
        className="relative text-0 w-full h-full"
        style={{
          opacity: opacity / 100,
          border: `solid ${strokeWidth}px ${strokeColor}`,
        }}
        ref={ref}
      >
        <Pool
          node={node}
          fillContent={fillContent}
          strokeWidth={strokeWidth}
          strokeColor={strokeColor}
        />
      </div>
    </>
  );
};

register({
  shape: "custom-react-verticalPool",
  width: 250,
  height: 540,
  effect: ["data"],
  component: component,
});

const verticalPool: CompoundedComponent = {
  name: "泳池(垂直)",
  icon: require("./image/verticalPool.png"),
  node: {
    shape: "custom-react-verticalPool",
    data: {
      label: "",
      // 泳池名称
      poolName: "",
      // 泳道
      lane: [],
      // 阶段
      stage: [],
      // 泳池名称高度
      headerHeight: 40,
      // 泳道名称高度
      laneHeadHeight: 40,
      // 阶段宽度
      stageWidth: 20,
      // 泳池方向
      direction: "vertical",
      // 文字方向
      textDirection: "horizontal",
      // 忽略拖拽
      ignoreDrag: true,
    },
  },
};

export default verticalPool;
