import { TopicItem, MindMapProjectInfo, HierarchyResult } from "@/types"
import { TopicType } from "@/enum"
import { e } from "unocss";

// branch subtopic
const B2SH = 20;
const S2SH = 20;
const B2SV = 20;
const S2SV = 20;

function firstWalk(node: TopicItem): HierarchyResult {
  const root: HierarchyResult = {
    x: 0,
    y: 0,
    totalHeight: 0,
    totalWidth: 0,
    width: node.width,
    height: node.height,
    id: node.id,
    data: node,
    children: [],
  };

  root.children = node.collapsed ? [] : node.children?.map((child) => firstWalk(child)) || [];

  return root;
}

// 设置宽高
function secondWalk(
  node: HierarchyResult,
  pageSetting: MindMapProjectInfo["pageSetting"]
) {
  let totalHeight = node.height;
  let totalWidth = node.width;
  if (node.children?.length) {
    node.children.forEach((child: HierarchyResult, index) => {
      const childSize = secondWalk(child, pageSetting);
      
      if(node.data.type === TopicType.branch) {
        // 分支主题高度 = 分支主题高度 + 子主题或自身高的数据 + 间隔
        totalHeight += childSize.totalHeight + B2SV;
      } else {
        // 子主题高度 = 子子主题高度之和
        totalHeight += childSize.totalHeight + S2SV;
      }

      // 分支主题宽度 = 分支主题的一半 + 子主题到分支主题中心的距离 + 子主题的宽度
      totalWidth = Math.max(totalWidth, (node.width / 2) + B2SH + childSize.totalWidth);
    });
  }
  node.totalHeight = totalHeight;
  node.totalWidth =  totalWidth;

  return node;
}

// 分支主题位置
function thirdWalk(root: HierarchyResult, type: 'left' | 'right' | 'leftright') {
  let startX = root.x + root.width / 2;
  let startY = root.y + root.height + B2SV;
  if(type === 'leftright') {
    let leftStartY = startY + root.children[0]?.height || 0 + S2SV;
    let rightStartY = startY;
    root.children.forEach((child, index) => {
      child.x = index % 2 === 1 ? startX - child.width - B2SH : startX + B2SH;
      const previous = root.children[index - 1];
      if(index % 2 === 0) {
        if(previous && rightStartY <= previous.y + previous.height) {
          rightStartY = previous.y + previous.height + S2SV;
        }
        child.y = rightStartY;
        rightStartY += child.totalHeight + S2SV;
      } else {
        if(previous && leftStartY <= previous.y + previous.height) {
          leftStartY = previous.y + previous.height + S2SV;
        }
        child.y = leftStartY;
        leftStartY += child.totalHeight + S2SV;
      }
      if(child.children?.length) {
        thirdWalk(child, index % 2 === 1 ? 'left' : 'right');
      }
    });
  } else {
    root.children.forEach((child) => {
      child.x = type === 'left' ? startX - child.width - B2SH : startX + B2SH;
      child.y = startY;
      startY += child.totalHeight + S2SV;
      if(child.children?.length) {
        thirdWalk(child, type);
      }
    });
  }
}

export const getTreeHierarchy = <T>(
  topic: TopicItem,
  pageSetting: MindMapProjectInfo["pageSetting"],
  type: 'left' | 'right' | 'leftright' = 'leftright',
): T => {
  // 处理结构
  const root = firstWalk(topic);
  // 处理宽高
  secondWalk(root, pageSetting);
  // 根据左右分别处理
  const leftRoot = {
    ...root,
    children: root.children.filter((child, index) => index % 2 === 1),
  }
  const rightRoot = {
    ...root,
    children: root.children.filter((child, index) => index % 2 === 0),
  }
  if(type === 'leftright') {
    root.children = [...(leftRoot.children || []), ...(rightRoot.children || [])];
    thirdWalk(root, 'leftright');
  } else if(type === 'left') {
    thirdWalk(root, 'left');
  } else if(type === 'right') {
    thirdWalk(root, 'right');
  }

  console.log(root)
  return root as T;
}