package ei.engine.scene;

import java.awt.Rectangle;
import java.util.ArrayList;

import org.lwjgl.opengl.GL11;

import ei.engine.util.MultiPrintStream;

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)){
			MultiPrintStream.out.println("Adding Entity To Node("+getName()+"): "+s.getName());
			entities.add(s);
			return true;
		}
		return false;
	}
	
	/**
	 * Add a Entity to a specific pos
	 * 
	 * @param s The Sprite to add
	 * @param pos the pos to put the entity to
	 * @return False if the Entity alredy exist else true
	 */
	public boolean add(Entity s,int pos){
		if(!entities.contains(s)){
			MultiPrintStream.out.println("Adding Entity To Node("+getName()+"): "+s.getName());
			entities.add(pos,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;
	}
	
	/**
	 * Removes all the entities in this node
	 *
	 */
	public void clear(){
		entities.clear();
	}
	
	/**
	 * Checks if the given entity is present in the node
	 * 
	 * @param e The entity to look for
	 * @return True if the entity exists else false
	 */
	public boolean contains(Entity e){
		return entities.contains(e);
	}
	
	/**
	 * 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){
			MultiPrintStream.out.println("Removing Entity From Node("+getName()+"): "+name);
			entities.remove(i);
			return true;
		}
		return false;
	}
	
	public boolean remove(Entity e){
		if(entities.contains(e)){
			MultiPrintStream.out.println("Removing Entity From Node("+getName()+"): "+e.getName());
			entities.remove(e);
			return true;
		}
		return false;
	}
	
	/**
	 * Draw all the Entities in the node
	 */
	public void render() {
		// store the current model matrix
		GL11.glPushMatrix();

		//Sets the location
		super.setTranslationGL();
		//the rotation
		//Rectangle rect = getBound();
		//super.setRotationGL(rect.width/2,rect.height/2);
		//Sets the scale of the sprite
		super.setScaleGL();
		
		for(int i=0; i<entities.size() ;i++){
			if(entities.get(i).getTexture() != null)
				entities.get(i).getTexture().getColor().setA(getTexture().getColor().getA());
			entities.get(i).render();
		}
		
		// restore the model view matrix to prevent contamination
		GL11.glPopMatrix();
	}
	
	/**
	 * 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;
	}
	
	public Rectangle getBound(){
		Rectangle rect = new Rectangle();
		for(int i=0; i<entities.size() ;i++){
			rect.add(entities.get(i).getBound());
		}
		return rect;
	}
	
	public int size(){
		return entities.size();
	}
	
	public String toString(){
		String msg = "Node[";
		for(int i=0; i<entities.size() ;i++){
			msg += entities.get(i).getName()+",";
		}
		return msg+"]";
	}
}
