package ei.engine.scene;

import java.util.ArrayList;

public class Node extends Sprite {
	/** the sprites of this node */
	protected ArrayList<Entity> entities;
	
	public Node(String name) {
		super(name);
		entities = new ArrayList<Entity>();
	}
	
	/**
	 * Add a Entity
	 * @param s the Sprite to add
	 * @return false if the Entity alredy exist else true
	 */
	public boolean add(Entity s){
		if(!entities.contains(s)){
			entities.add(s);
			return true;
		}
		return false;
	}
	
	/**
	 * get a Entity
	 * @param name the name of Entity to get
	 * @return null if the Entity wasnt found
	 */
	public Entity get(String name){
		int i = getId(name);
		if(i >= 0){
			return entities.get(i);
		}
		return null;
	}
	
	/**
	 * remove a Entity
	 * @param name the name of Entity to remove
	 * @return false if the Entity alredy exist else true
	 */
	public boolean remove(String name){
		int i = getId(name);
		if(i >= 0){
			entities.remove(i);
			return true;
		}
		return false;
	}
	
	/**
	 * Draw all the Entities in the node
	 */
	public void render() {
		for(int i=0; i<entities.size() ;i++){
			entities.get(i).render();
		}
	}
	
	/**
	 * update all the Entities in the node
	 */
	public void update() {
		for(int i=0; i<entities.size() ;i++){
			entities.get(i).update();
		}
	}
	
	/**
	 * Searches for the given name of a Entity
	 * @param name The name of the Entity
	 * @return The index of the Entity
	 */
	private int getId(String name){
		for(int i=0; i<entities.size() ;i++){
			if(entities.get(i).getName().equals(name)){
				return i;
			}
		}
		return -1;
	}
}
