import { TopicItem, MindMapProjectInfo, HierarchyResult } from "../../types"
import { TopicType } from "../../enum"

// 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.data.type === TopicType.branch ? node.height : 0;
  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 + (index !== node.children.length - 1 ? S2SV : 0);
      }
      
      if(node.data.type === TopicType.branch) {
        // 分支主题宽度 = 分支主题的一半 + 子主题到分支主题中心的距离 + 子主题的宽度
        totalWidth = Math.max(totalWidth, (node.width / 2) + B2SH + childSize.totalWidth);
      } else {
        totalWidth = Math.max(totalWidth, node.width + childSize.totalWidth + S2SH);
      }
    });
  }
  node.totalHeight = Math.max(node.height, totalHeight);
  node.totalWidth =  totalWidth;

  return node;
}

// 分支主题位置
function thirdWalk(root: HierarchyResult) {
  let startX = root.width + 20;
  const centerY = root.height / 2;
  root.children.forEach((child) => {
    child.x = startX;
    child.y = root.y + centerY - child.height / 2;
    startX += child.totalWidth + 30;
  });
}

// 中间处理
function fourthWalk(branch: HierarchyResult, index: number, type: 'up' | 'down' | 'updown') {
  let startX = branch.x + branch.width / 2 + B2SH;
  let startY = branch.y;

 for(let i = 0; i < branch.children.length; i++) {
  if(type === 'down' || (type === 'updown' && index % 2 === 1)) {
    startY += 20;
    fivethWalk(branch.children, startX, startY, 'down');
  } else {
    startY -= 10;
    fivethWalk(branch.children, startX, startY, 'up');
  }
 }
}

// 设置一级子主题位置
function fivethWalk(nodes: HierarchyResult[], x: number, y: number, type: 'up' | 'down') {
  let startY = y;
  for(let i = 0; i < nodes.length; i++) {
    // 根据朝向 设置渲染方向
    const node = nodes[type === 'down' ? i : nodes.length - i - 1];
    node.x = x;
    node.y = startY + (type === 'up' ? -(node.totalHeight/2) - node.height/2 : node.totalHeight / 2 + node.height/2);
    startY += type === 'up' ? -node.totalHeight - S2SV : node.totalHeight + S2SV;
    if(node.children?.length) {
      sixthWalk(node);
    }
  }
}

// 其他子级位置
function sixthWalk(node: HierarchyResult) {
  let startY = node.y + node.height / 2 - node.totalHeight / 2;
  const x = node.x + node.width + S2SH;
  node.children.forEach((child) => {
    child.x = x;
    child.y = startY;
    startY += child.totalHeight + S2SV;
    if(child.children?.length) {
      sixthWalk(child);
    }
  });
}
export const getTimeHierarchy = <T>(
  topic: TopicItem,
  pageSetting: MindMapProjectInfo["pageSetting"],
  type: 'up' | 'down' | 'updown' = 'updown',
): T => {
  // 处理结构
  const root = firstWalk(topic);
  // 处理宽高
  secondWalk(root, pageSetting);
  // 处理分支主题位置
  thirdWalk(root);
  // 处理子主题位置
  root.children.forEach((child, index) => {
    fourthWalk(child, index, type);
  });
  console.log(root)
  return root as T;
}