import { StructureType, TopicType } from "@/enum";
import { MindMapProjectInfo, TopicItem } from "@/types";
import Hierarchy from "@antv/hierarchy";
import { getTreeStructure } from "@/utils/hierarchy/tree";
import { getOrganization } from "@/utils/hierarchy/organization";
import { getFishbone } from "@/utils/hierarchy/fishbone";

// 思维导图结构实现
export const hierarchyMethodMap: Record<
  string,
  (topic: TopicItem, pageSetting: MindMapProjectInfo["pageSetting"]) => any
> = {
  // 右侧图
  [StructureType.right]: <T>(
    topic: TopicItem,
    pageSetting: MindMapProjectInfo["pageSetting"]
  ): T => {
    return 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";
      },
    });
  },
  // 左侧图
  [StructureType.left]: <T>(
    topic: TopicItem,
    pageSetting: MindMapProjectInfo["pageSetting"]
  ): T => {
    return 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 "left";
      },
    });
  },
  // 左右分布
  [StructureType.leftRight]: <T>(
    topic: TopicItem,
    pageSetting: MindMapProjectInfo["pageSetting"]
  ): T => {
    if (topic.type !== TopicType.main) {
      return hierarchyMethodMap[StructureType.left](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 getTreeStructure<T>(topic, pageSetting);
  },
  // 组织结构
  [StructureType.organization]: <T>(topic: TopicItem, pageSetting: MindMapProjectInfo['pageSetting']): T => {
    return getOrganization<T>(topic, pageSetting);
  },
  // 左侧鱼骨图
  [StructureType.leftFishbone]: <T>(topic: TopicItem, pageSetting: MindMapProjectInfo['pageSetting']): T => {
    if(topic.type === TopicType.main) {
      return getFishbone<T>(topic, 'left')
    } else {
      return hierarchyMethodMap[StructureType.left](topic, pageSetting);
    }
  },
  // 左侧鱼骨图
  [StructureType.rightFishbone]: <T>(topic: TopicItem, pageSetting: MindMapProjectInfo['pageSetting']): T => {
    if(topic.type === TopicType.main) {
      return getFishbone<T>(topic, 'right')
    } 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);
  },
};
