package com.fdkankan.platform.agent.util;

import com.fdkankan.platform.agent.entity.AgentFramework;

import java.util.ArrayList;
import java.util.List;

public class AgentFrameworkTreeUtil {

    private List<AgentFrameworkTree> resultNodes = new ArrayList<AgentFrameworkTree>();//树形结构排序之后list内容

    private List<AgentFrameworkTree> nodes = new ArrayList<AgentFrameworkTree>();
    //传入list参数

    public AgentFrameworkTreeUtil(List<AgentFramework> nodesList) {//通过构造函数初始化
        for (AgentFramework n : nodesList) {
            AgentFrameworkTree treeGrid = new AgentFrameworkTree();
            treeGrid.setId(n.getId());
            treeGrid.setName(n.getAgentName());
//            treeGrid.setIcon(n.getIcon());
            if (n.getParentId() != null) {
                treeGrid.setParentId(n.getParentId());
            }
            nodes.add(treeGrid);
        }
    }

    public AgentFrameworkTreeUtil() {
    }

    /**
     * 构建树形结构list
     *
     * @return 返回树形结构List列表
     */
    public List<AgentFrameworkTree> buildTree() {
        for (AgentFrameworkTree node : nodes) {
            Long id = node.getParentId();
            if (id == null) {//通过循环一级节点 就可以通过递归获取二级以下节点
                resultNodes.add(node);//添加一级节点
//                node.setLevel(1);
                build(node, node.getLevel());//递归获取二级、三级、。。。节点
            }
        }
        return resultNodes;
    }

    /**
     * 递归循环子节点
     *
     * @param node 当前节点
     */
    private void build(AgentFrameworkTree node, int level) {
        List<AgentFrameworkTree> children = getChildren(node);
        if (!children.isEmpty()) {//如果存在子节点
            node.setChildren(children);
//        	level++;
            for (AgentFrameworkTree child : children) {//将子节点遍历加入返回值中
//        		child.setLevel(level);
                build(child, child.getLevel());
            }
        }
    }

    /**
     * @param node
     * @return 返回
     */
    private List<AgentFrameworkTree> getChildren(AgentFrameworkTree node) {
        List<AgentFrameworkTree> children = new ArrayList<AgentFrameworkTree>();
        Long id = node.getId();
        for (AgentFrameworkTree child : nodes) {
            if (child.getParentId() != null && id.longValue() == child.getParentId().longValue()) {//如果id等于父id
                children.add(child);//将该节点加入循环列表中
            }
        }
        return children;
    }

    public List<AgentFrameworkTree> buildTree(List<AgentFramework> all, List<AgentFramework> in) {
        for (AgentFramework n : all) {
            AgentFrameworkTree treeGrid = new AgentFrameworkTree();
            treeGrid.setId(n.getId());
            treeGrid.setName(n.getAgentName());
            for (AgentFramework nin : in) {
                if (nin.getId().equals(n.getId())) {
                    treeGrid.setChecked(true);
                }
            }
            if (n.getParentId() != null) {
                treeGrid.setParentId(n.getParentId());
            }
            nodes.add(treeGrid);
        }
        return buildTree();
    }
}
