/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 06/11/2017 : SCA : V2.2.1 
 *  Redmine #5403 - DMPT3 
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller.tools.carrington;

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


public class MTreeNode implements Comparable<MTreeNode> {

    private String name;        // name of the branch
    private String desc;        
    private MTreeNode parent;
    private List<MTreeNode> children; 

    public MTreeNode() {
        setChildren(new ArrayList<MTreeNode>());
    }

    public MTreeNode(String n) {
        this();
    	this.setName(n);
    }

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public MTreeNode getParent() {
		return parent;
	}

	public void setParent(MTreeNode parent) {
		this.parent = parent;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public List<MTreeNode> getChildren() {
		return children;
	}

	public void setChildren(List<MTreeNode> children) {
		this.children = children;
	}

	@Override
	public int compareTo(MTreeNode node) {
		return name.compareTo((node.getName()));
	}

	public boolean contains(String name) {
		for (MTreeNode child : children) {
			if (child.getName().equals(name)) {
				return true;
			}
		}
		return false;
	}
	
	public MTreeNode getChild(String name) {
		for (MTreeNode child : children) {
			if (child.getName().equals(name)) {
				return child;
			}
		}
		return null;
	}

	/**
     * Get a child by its path.
     * @return found node or null if not found
     */
    public MTreeNode getNode(String name, int level) {
        if (hasChild() && getLevel() < level) {
            for (final MTreeNode subNode : getChildren()) {
                MTreeNode findNode = subNode.getNode(name, level);
                if (findNode != null) {
                    return findNode;
                }
            }
        } else {
            if (name.equals(getName())) {
            	return this;
            }
        }
        return null;
    }

	/**
     * Get a child by its name and parent
     * @return found node or null if not found
     */
    public MTreeNode getNode(String parent, String name, int level) {
        if (hasChild() && getLevel() < level) {
            for (final MTreeNode subNode : getChildren()) {
                MTreeNode findNode = subNode.getNode(parent, name, level);
                if (findNode != null) {
                    return findNode;
                }
            }
        } else {
            if (parent.equals(getParent().getName()) && name.equals(getName())) {
            	return this;
            }
        }
        return null;
    }

	public boolean hasChild() {
		return !children.isEmpty();
	}
	
	public int getLevel() {
		if (parent != null) {
			return parent.getLevel() + 1;
		}
		else {
			return 1;
		}
	}
}

