package nl.nikhef.sfp.ddmi;

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

public class DDMIGroup extends DDMIElement {

	
	private List<DDMIElement> _children = new ArrayList<DDMIElement>();	

	
	public void add(DDMIElement child) {
		_children.add(child);
	}
	
	
	public List<DDMIElement> getChildren() {
		return _children;
	}
	
	@Override
	public void output(StringBuilder sb, DDMIContext ctx, int level) 
	{
		SFPUtils.pad(sb, level);
		
		if (getLabel() != null) {
			sb.append(String.format("@ Group label=%s\n", getLabel()));
		} else {
			sb.append(String.format("@ Group hash=%08x\n", this.hashCode()));
		}
		
		
		for (DDMIElement child : _children) {
			if (child.isValid(ctx)) {
				child.output(sb, ctx, level + 1);
			}
		}
	}

	protected DDMIElement findElementForId(String id) 
	{
		for (DDMIElement element : getChildren()) 
		{
			if (id.equals(element.getId())) {
				return element;
			}
			if (element instanceof DDMIGroup) {
				DDMIElement el = DDMIGroup.class.cast(element).findElementForId(id);
				if (el != null) return el;
			} 
		}
		return null;
	}

	/**
	 * Searches the element tree for matching elements for the specified filter.
	 * 
	 * @param filter	The filter to apply for the search
	 * @param list		The list to add the matched elements to
	 */
	protected <T extends DDMIElement> void findElements(DDMIFilter<T> filter, List<T> list) 
	{
		for (DDMIElement element : getChildren()) 
		{
			
			if (element instanceof DDMIGroup) 
			{
				DDMIGroup.class.cast(element).findElements(filter, list);
			} 

			if (!filter.matchClass().isAssignableFrom(element.getClass())) continue;
			
			T match = filter.matchClass().cast(element);
			
			if (filter.matches(match)) list.add(match);
		}
	}
	
	
}
