import { StructureType, TopicType } from "@/enum";
import { MindMapProjectInfo, TopicItem } from "@/types";
import Hierarchy from "@antv/hierarchy";
import { getTreeDiagramHierarchy } from "@/utils/hierarchy/treeDiagram";
import { getOrganizationHierarchy } from "@/utils/hierarchy/organization";
import { getRightFishboneHierarchy } from "@/utils/hierarchy/rightFishbone";
import { getLeftFishboneHierarchy } from "@/utils/hierarchy/leftFishbone";
import { getTimeHierarchy } from "@/utils/hierarchy/time";
import { getTreeHierarchy } from "@/utils/hierarchy/tree";
import { traverseNode } from "../mindmapHander";

// 思维导图结构实现
export const hierarchyMethodMap: Record<
  string,
  (topic: TopicItem, pageSetting: MindMapProjectInfo["pageSetting"]) => any
> = {
  // 右侧图
  [StructureType.right]: <T>(
    topic: TopicItem,
    pageSetting: MindMapProjectInfo["pageSetting"]
  ): T => {
    const result = Hierarchy.mindmap(topic, {
      direction: "H",
      getHeight(d: TopicItem) {
        return d.height;
      },
      getWidth(d: TopicItem) {
        return d.width;
      },
      getHGap(d: TopicItem) {
        if (d.type === TopicType.main) return pageSetting.branchX || 20;
        if (d.type === TopicType.branch) return pageSetting.subTopicX || 20;
        if (d.type === TopicType.sub) return pageSetting.subTopicX || 20;
        return pageSetting.branchX || 40;
      },
      getVGap() {
        return pageSetting.branchY || 20;
      },
      getSide: () => {
        return "right";
      },
    });
    traverseNode(result.children, (topic) => {
      // @ts-ignore
      topic.direction = 'right'
    });
    return result;
  },
  // 左侧图
  [StructureType.left]: <T>(
    topic: TopicItem,
    pageSetting: MindMapProjectInfo["pageSetting"]
  ): T => {
    const result = Hierarchy.mindmap(topic, {
      direction: "H",
      getHeight(d: TopicItem) {
        return d.height;
      },
      getWidth(d: TopicItem) {
        return d.width;
      },
      getHGap(d: TopicItem) {
        if (d.type === TopicType.main) return pageSetting.branchX || 20;
        if (d.type === TopicType.branch) return pageSetting.branchX || 20;
        if (d.type === TopicType.sub) return pageSetting.subTopicX || 20;
        return pageSetting.branchX || 40;
      },
      getVGap() {
        return pageSetting.branchY || 20;
      },
      getSide: () => {
        return "left";
      },
    });

    traverseNode(result.children, (topic) => {
      // @ts-ignore
      topic.direction = 'left'
    });
    return result;
  },
  // 左右分布
  [StructureType.leftRight]: <T>(
    topic: TopicItem,
    pageSetting: MindMapProjectInfo["pageSetting"]
  ): T => {
    if (topic.type !== TopicType.main) {
      return hierarchyMethodMap[StructureType.right](topic, pageSetting);
    } else {
      const rightChildren = (topic.children || []).filter(
        (item, index) => index % 2 === 0
      );
      const leftChildren = (topic.children || []).filter(
        (item, index) => index % 2 === 1
      );
      const left = hierarchyMethodMap[StructureType.left](
        { ...topic, children: leftChildren },
        pageSetting
      );
      const right = hierarchyMethodMap[StructureType.right](
        { ...topic, children: rightChildren },
        pageSetting
      );
      return {
        ...right,
        children: [...right.children, ...left.children],
      };
    }
  },
  // 树状结构
  [StructureType.tree]: <T>(
    topic: TopicItem,
    pageSetting: MindMapProjectInfo["pageSetting"]
  ): T => {
    return getTreeDiagramHierarchy<T>(topic, pageSetting);
  },
  // 组织结构
  [StructureType.organization]: <T>(
    topic: TopicItem,
    pageSetting: MindMapProjectInfo["pageSetting"]
  ): T => {
    return getOrganizationHierarchy<T>(topic, pageSetting);
  },
  // 左侧鱼骨图
  [StructureType.leftFishbone]: <T>(
    topic: TopicItem,
    pageSetting: MindMapProjectInfo["pageSetting"]
  ): T => {
    if (topic.type === TopicType.main) {
      return getLeftFishboneHierarchy<T>(topic);
    } else {
      return hierarchyMethodMap[StructureType.left](topic, pageSetting);
    }
  },
  // 左侧鱼骨图
  [StructureType.rightFishbone]: <T>(
    topic: TopicItem,
    pageSetting: MindMapProjectInfo["pageSetting"]
  ): T => {
    if (topic.type === TopicType.main) {
      return getRightFishboneHierarchy<T>(topic);
    } else {
      return hierarchyMethodMap[StructureType.left](topic, pageSetting);
    }
  },
  // 右侧括号图
  [StructureType.rightBracket]: <T>(
    topic: TopicItem,
    pageSetting: MindMapProjectInfo["pageSetting"]
  ): T => {
    return hierarchyMethodMap[StructureType.right](topic, pageSetting);
  },
  // 左侧括号图
  [StructureType.leftBracket]: <T>(
    topic: TopicItem,
    pageSetting: MindMapProjectInfo["pageSetting"]
  ): T => {
    return hierarchyMethodMap[StructureType.left](topic, pageSetting);
  },
  // 向上时间轴
  [StructureType.upwardTime]: <T>(
    topic: TopicItem,
    pageSetting: MindMapProjectInfo["pageSetting"]
  ): T => {
    if (topic.type !== TopicType.main) {
      return hierarchyMethodMap[StructureType.right](topic, pageSetting);
    }
    return getTimeHierarchy<T>(topic, pageSetting, "up");
  },
  // 向下时间轴
  [StructureType.downwardTime]: <T>(
    topic: TopicItem,
    pageSetting: MindMapProjectInfo["pageSetting"]
  ): T => {
    if (topic.type !== TopicType.main) {
      return hierarchyMethodMap[StructureType.right](topic, pageSetting);
    }
    return getTimeHierarchy<T>(topic, pageSetting, "down");
  },
  // 横向时间轴
  [StructureType.horizontalTime]: <T>(
    topic: TopicItem,
    pageSetting: MindMapProjectInfo["pageSetting"]
  ): T => {
    if (topic.type !== TopicType.main) {
      return hierarchyMethodMap[StructureType.right](topic, pageSetting);
    }
    return getTimeHierarchy<T>(topic, pageSetting, "updown");
  },
  // 树形
  [StructureType.rightTreeShape]: <T>(
    topic: TopicItem,
    pageSetting: MindMapProjectInfo["pageSetting"]
  ): T => {
    return getTreeHierarchy<T>(topic, pageSetting, "right");
  },
  [StructureType.leftTreeShape]: <T>(
    topic: TopicItem,
    pageSetting: MindMapProjectInfo["pageSetting"]
  ): T => {
    return getTreeHierarchy<T>(topic, pageSetting, "left");
  },
  [StructureType.treeShape]: <T>(
    topic: TopicItem,
    pageSetting: MindMapProjectInfo["pageSetting"]
  ): T => {
    return getTreeHierarchy<T>(topic, pageSetting, "leftright");
  },
};
