package cn.pluss.platform.util.tree;

import java.util.*;

/**
 * @author Djh
 * 分级节点排序工具类
 */
public class TreeUtils {

    /**
     * 树形结构并排序
     */
    public static <T extends BaseNode<T>> List<T> getSortTree(List<T> nodeList) {
        if (nodeList == null || nodeList.size() == 0) {
            return null;
        }

        nodeList.sort(Comparator.comparingInt(o -> o.sort));
        boolean flag = true;
        List<T> result = new ArrayList<>();

        nodeList.sort(Comparator.comparingInt(o -> o.sort));

        // 首先取得根节点
        List<T> leafList = new ArrayList<>();
        Iterator<T> startIterator = nodeList.iterator();
        while (startIterator.hasNext()) {
            T baseNode = startIterator.next();
            if (baseNode.isRoot()) {
                result.add(baseNode);
                leafList.add(baseNode);
                startIterator.remove();
            }
        }

        while (flag) {
            flag = false;
            List<T> tempLeafList = new ArrayList<>();

            for (T leafNode: leafList) {
                Iterator<T> iterator = nodeList.iterator();
                while(iterator.hasNext()) {
                    T next = iterator.next();
                    if (leafNode.getId().equals(next.getPId())) {
                        iterator.remove();
                        leafNode.children.add(next);
                        tempLeafList.add(next);
                        flag = true;
                    }
                }
            }

            if (flag) {
                leafList = tempLeafList;
            }
        }

        return result;
    }

    /**
     * 树形列表排序
     */
    public static <T extends BaseNode> List<T> getSortList(List<T> srcList) {
        // 先给源数组排序
        if (srcList == null || srcList.size() == 0) {
            return null;
        }
        srcList.sort(Comparator.comparingInt(o -> o.sort));
        List<T> resultList = new LinkedList<>();
        sort(srcList, null, resultList);

        return resultList;
    }

    private static <T extends BaseNode> void sort(List<T> srcList, List<T> rootList, List<T> resultList) {
        if (srcList.size() == 0) {
            return;
        }

        List<T> subRoot = new ArrayList<>();
        if (rootList == null) {
            Iterator<T> iterator = srcList.iterator();
            while (iterator.hasNext()) {
                T t = iterator.next();
                if (t.isRoot()) {
                    resultList.add(t);
                    subRoot.add(t);
                    iterator.remove();
                }
            }
            // 获取到一个下级列表，然后递归
            sort(srcList, subRoot, resultList);

            return;
        }

        for (T root: rootList) {
            Iterator<T> iterator = srcList.iterator();
            List<T> nodeList = new ArrayList<>();
            // 获取根节点在结果组中的位置
            int i = resultList.indexOf(root);
            while (iterator.hasNext()) {
                T t = iterator.next();
                if (t.getPId().equals(root.getId())) {
                    resultList.add(nodeList.size() + i + 1, t);
                    nodeList.add(t);
                    iterator.remove();
                }
            }

            sort(srcList, nodeList, resultList);
        }
    }
}