package ei.engine.input;

import java.util.ArrayList;

/**
 * This class handels all the input in the engine
 * 
 * @author Ziver
 */

public class InputHandler {
	// The instance of this handler
	private static InputHandler instance;
	// The array of inputs
	private ArrayList<Input> input;
	
	public InputHandler(){
		input = new ArrayList<Input>();
	}
	
	/**
	 * updates all the inputs
	 *
	 */
	public void update(){
		for(int i=0; i<input.size() ;i++){
			if(input.get(i).isEnabled()){
				input.get(i).update();
			}
		}
	}
	
	/**
	 * renders all the inputs
	 *
	 */
	public void render(){
		for(int i=0; i<input.size() ;i++){
			if(input.get(i).isEnabled()){
				input.get(i).render();
			}
		}
	}
	
	/**
	 * Add a input
	 * 
	 * @param i the input
	 * @return true if successful else false
	 */
	public boolean addInput(Input i){
		if(!input.contains(i)){
			input.add(i);
			return true;
		}
		return false;
	}
	
	/**
	 * Removes a input from the handler
	 * 
	 * @param i The input to remove
	 * @return true if successful else false
	 */
	public boolean removeInput(Input i){
		if(input.contains(i)){
			input.remove(i);
			return true;
		}
		return false;
	}
	
	/**
	 * Remove input from handler by
	 * index in handler
	 * 
	 * @param i The index of the input
	 * @return true if successful else false
	 */
	public boolean removeInput(int i){
		if(i >= 0 && i < input.size()){
			input.remove(i);
			return true;
		}
		return false;
	}
	
	/**
	 * Remove a input by name
	 * 
	 * @param name The name of the input to remove
	 * @return true if successful else false
	 */
	public boolean removeInput(String name){
		for(int i=0; i<input.size() ;i++){
			if(input.get(i).getName().equals(name)){
				return removeInput(i);
			}
		}
		return false;
	}
	
	/**
	 * Returns an insance of this class
	 * 
	 * @return an insance of this class
	 */
	public static InputHandler getInstance(){
		if(instance == null){
			instance = new InputHandler();
		}
		return instance;
	}
}
