package ei.engine.ui;

import java.util.ArrayList;

import ei.engine.scene.Node;

/**
 * Handles all the ui assign to it
 * @author Ziver
 *
 */
public class UiHandler {
	private ArrayList<UiComponent> ui;
	private Node uiNode;
	
	/**
	 * Creates a ui handler
	 * 
	 * @param name The name of the ui handler
	 */
	public UiHandler(String name){
		ui = new ArrayList<UiComponent>();
		uiNode = new Node(name);
	}
	
	/**
	 * Add An ui component to the handler
	 * 
	 * @param u The component to add
	 * @return True if successfully added else false
	 */
	public boolean addUi(UiComponent u){
		if(!ui.contains(u)){
			ui.add(u);
			uiNode.add(u.getNode());
			return true;
		}
		return false;
	}
	
	/**
	 * Removes a component from the handler
	 * 
	 * @param u The component to remove
	 * @return True if successfully removed else false
	 */
	public boolean removeUi(UiComponent u){
		if(ui.contains(u)){
			ui.remove(u);
			uiNode.remove(u.getNode());
			return true;
		}
		return false;
	}
	
	/**
	 * Removes a component from the handler by its name
	 * 
	 * @param name The name of the component to remove
	 * @return True if successfully removed else false
	 */
	public boolean addUi(String name){
		for(int i=0; i<ui.size() ;i++){
			if(ui.get(i).getName().equals(name)){
				uiNode.remove(ui.get(i).getNode());
				ui.remove(i);
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Get the UiNode
	 * 
	 * @return The UiNode
	 */
	public Node getNode(){
		return uiNode;
	}
	
	/**
	 * Should be cald from a mouselistener
	 * 
	 * @param x The x pos of the cursor
	 * @param y The y pos of the cursor
	 */
	public void mousePos(int x, int y){
		for(int i=0; i<ui.size() ;i++){
			ui.get(i).mousePos(x, y);
		}
	}
	
	/**
	 * Should be cald from a mouselistener when mouse is down
	 * 
	 * @param x The x pos of the cursor
	 * @param y The y pos of the cursor
	 */
	public void mouseDown(int x, int y, int event){
		for(int i=0; i<ui.size() ;i++){
			ui.get(i).mouseDown(x, y, event);
		}
	}
	
	public void keyDown(int event){
		for(int i=0; i<ui.size() ;i++){
			ui.get(i).keyDown(event);
		}
	}
}
