package com.judong.chuanyiserver.util;

import org.openscada.opc.lib.da.browser.Branch;
import org.openscada.opc.lib.da.browser.Leaf;
import org.openscada.opc.lib.da.browser.TreeBrowser;

import java.util.*;

/**
 * @author zhoupeng
 * @date 2022/7/28
 */
public class MapTree {
    String key;
    String value;
    List<MapTree> children;

    public MapTree() {
        key = null;
        value = null;
        children = new ArrayList<>();
        children.clear();
    }

    public MapTree(String key, String value) {
        this.key = key;
        this.value = value;
        children = new ArrayList<>();
        children.clear();
    }

    /**
     * 添加子树
     */
    public void addTree(MapTree mapTree) {
        children.add(mapTree);
    }

    /**
     * 根据key获取子树
     */
    public MapTree getChild(String key) {
        if (this.isEmpty()) {
            return null;
        } else {
            for (MapTree child : this.children) {
                if (child.key.equals(key)) {
                    return child;
                }
            }
            return null;
        }
    }

    /**
     * 置空树
     */
    public void clearTree() {
        key = null;
        value = null;
        children.clear();
    }

    /**
     * 获得树根
     */
    public MapTree root() {
        return this;
    }

    /**
     * 获取根节点的数据,放入一个map中
     */
    public Map<String, String> getRoot() {
        Map<String, String> m = new HashMap<>(1);
        m.put(key, value);
        return m;
    }

    /**
     * 判空
     */
    public boolean isEmpty() {
        return (this.children.isEmpty() && this.key == null && this.value == null);
    }

    /**
     * 判断是否为叶子节点
     */
    public boolean isLeaf() {
        return this.children.isEmpty();
    }

    /**
     * 返回当前子树（亦或是叶子）的孩子节点个数
     */
    public int size() {
        if (this.isEmpty()) {
            return 0;
        } else if (this.isLeaf()) {
            return 1;
        } else {
            return this.children.size();
        }
    }

    public void browserTree(TreeBrowser treeBrowser, Branch branch, MapTree tree) {

        try {
            treeBrowser.fillBranches(branch);
        } catch (Exception ignored) {
        }
        try {
            treeBrowser.fillLeaves(branch);
        } catch (Exception ignored) {
        }

        Iterator iter = branch.getLeaves().iterator();
        while (iter.hasNext()) {
            Leaf leaf = (Leaf) iter.next();
            //key是leaf.getName()，value是leaf.getItemId()
            tree.addTree(new MapTree(leaf.getName(), leaf.getItemId()));
            //this.itemList.put(leaf.getName(),leaf.getItemId())
        }

        iter = branch.getBranches().iterator();
        while (iter.hasNext()) {
            Branch subBranch = (Branch) iter.next();
            //key和value都是subBranch.getName()，即群组名
            tree.addTree(new MapTree(subBranch.getName(), subBranch.getName()));
            this.browserTree(treeBrowser, subBranch, tree.getChild(subBranch.getName()));
        }
    }
}
