package dk.itu.mario.level.grammar;

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

import dk.itu.mario.level.LevelComponent;

public class ParseNode {
	private LevelComponent.TYPE levelCompType;
	private double percentLength;
	private List<ParseNode> children = new ArrayList<ParseNode>();

	public ParseNode(LevelComponent.TYPE levelCompType, double percentLength) {
		this.levelCompType = levelCompType;
		this.percentLength = percentLength;
	}

	public void addChild(ParseNode child) {
		children.add(child);
	}

	public LevelComponent.TYPE getType() {
		return levelCompType;
	}

	public ParseNode getChild(int index) {
		return children.get(index);
	}
	
	public int getNumChildren() {
		return children.size();
	}
	
	public double getPercentLength() {
		return percentLength;
	}

	public void setChildLengthRatios(double... childPercentLengths) {
		double sumLength = 0.0;
		for (double cpl : childPercentLengths) {
			sumLength += cpl;
		}
		if (childPercentLengths.length != children.size()) {
			throw new IllegalArgumentException(
					"Error - number of requested child percent lengths to set does not match the number of children: "
							+ childPercentLengths.length
							+ " != "
							+ children.size());
		}
		if (Math.abs(sumLength - 1.0) > 0.01) {
			throw new IllegalArgumentException(
					"Error - requested child percent lengths do not sum to 100% within an error of 1%.");
		}
		for (int i = 0; i < childPercentLengths.length; i++) {
			children.get(i).setPercentLength(childPercentLengths[i]);
		}
	}

	public void setPercentLength(double percentLength) {
		this.percentLength = percentLength;
	}
}