package info.bukova.isspst.ui.signeddocs;

import info.bukova.isspst.data.SignedDocumentItem;
import org.zkoss.zul.DefaultTreeNode;
import org.zkoss.zul.TreeNode;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class SignedDocsTreeNode extends DefaultTreeNode<SignedDocumentItem> {
    public SignedDocsTreeNode(SignedDocumentItem data) {
        super(data);
    }

    public SignedDocsTreeNode(SignedDocumentItem data, Collection<? extends TreeNode<SignedDocumentItem>> children) {
        super(data, children);
    }

    public SignedDocsTreeNode getParentNode() {
        return (SignedDocsTreeNode) this.getParent();
    }

    public int getDeep() {
        int iDeep = 0;
        SignedDocsTreeNode node = this.getParentNode();

        while (node != null) {
            node = node.getParentNode();
            iDeep++;
        }

        return iDeep;
    }

    public List<SignedDocsTreeNode> getLeafs() {
        List<SignedDocsTreeNode> list = new LinkedList<SignedDocsTreeNode>();

        if (!this.isLeaf()) {
            List<TreeNode<SignedDocumentItem>> children = this.getChildren();

            for (TreeNode<SignedDocumentItem> item : children) {
                SignedDocsTreeNode node = (SignedDocsTreeNode) item;

                if (node != null) {
                    if (node.isLeaf()) {
                        list.add(node);
                    } else {
                        list.addAll(node.getLeafs());
                    }
                }
            }
        }

        return list;
    }

    public int getLeafsCount() {
        int count = 0;

        if (!this.isLeaf()) {
            List<TreeNode<SignedDocumentItem>> children = this.getChildren();

            for (TreeNode<SignedDocumentItem> item : children) {
                SignedDocsTreeNode node = (SignedDocsTreeNode) item;

                if (node != null) {
                    if (node.isLeaf()) {
                        count += 1;
                    } else {
                        count += node.getLeafsCount();
                    }
                }
            }
        }

        return count;
    }
}
