package com.isartdigital.game;
import com.isartdigital.utils.ui.Keyboard;
import js.Browser;
import js.html.KeyboardEvent;

	
/**
 * ...
 * @author Cindy Asselin de Beauville
 */
class Controller 
{
	
	/**
	 * instance unique de la classe Controller
	 */
	private static var instance: Controller;
	
	private var input:Bool = false;
	private var precInput:Bool = false;
	
	public var up (get, null): Bool;
	public var down (get, null): Bool;
	public var left (get, null): Bool;
	public var right (get, null): Bool;
	public var Q (get, null): Bool;
	public var D (get, null): Bool;
	
	private var lastInputLeft:Bool = false;
	private var lastInputRight:Bool = false;
	private var lastInputUp:Bool = false;
	private var lastInputDown:Bool= false;
	
	/**
	 * Retourne l'instance unique de la classe, et la crée si elle n'existait pas au préalable
	 * @return instance unique
	 */
	public static function getInstance (): Controller {
		if (instance == null) instance = new Controller();
		return instance;
	}
	
	/**
	 * constructeur privé pour éviter qu'une instance soit créée directement
	 */
	private function new() 
	{
		Browser.window.addEventListener("keydown", registerKeyDown);
		Browser.window.addEventListener("keyup", registerKeyUp);
	}
	

	/**
	 * détruit l'instance unique et met sa référence interne à null
	 */
	public function destroy (): Void {
		
		Browser.window.removeEventListener("keydown", registerKeyDown);
		Browser.window.removeEventListener("keyup", registerKeyUp);
		instance = null;
	}
	
	
	private var isPressed = false;
	
	private function registerKeyDown(pEvent:KeyboardEvent):Void {
		if (!isPressed) {
			if (pEvent.keyCode == Keyboard.LEFT) left = true;
			if (pEvent.keyCode == Keyboard.RIGHT) right = true;
			if (pEvent.keyCode == Keyboard.DOWN) down = true;
			if (pEvent.keyCode == Keyboard.UP) up = true;
			isPressed = true;
		} else {
			if (pEvent.keyCode == Keyboard.LEFT) left = false;
			if (pEvent.keyCode == Keyboard.RIGHT) right = false;
			if (pEvent.keyCode == Keyboard.DOWN) down = false;
			if (pEvent.keyCode == Keyboard.UP) up = false;
		}
			if (pEvent.keyCode == Keyboard.Q) {
				Q = true;
				D = false;
			}
			else if (pEvent.keyCode == Keyboard.D) {
				D = true;
				Q = false;
			}
	}
	
	private function registerKeyUp(pEvent:KeyboardEvent):Void {
		if (pEvent.keyCode == Keyboard.LEFT) left = false;
		if (pEvent.keyCode == Keyboard.RIGHT) right = false;
		if (pEvent.keyCode == Keyboard.DOWN) down = false;
		if (pEvent.keyCode == Keyboard.UP) up = false;
		if (pEvent.keyCode == Keyboard.Q) Q = false;
		if (pEvent.keyCode == Keyboard.D) D = false;
		isPressed = false;
	}
	
	public function reset():Void {
		left = false;
		right = false;
		down = false;
		up = false;
	}
	
	public function get_up():Bool {
		return up;
	}
	public function get_down():Bool {
		return down;
	}
	public function get_left():Bool {
		return left;
	}
	public function get_right():Bool {
		return right;
	}
	
	public function get_Q():Bool {
		return Q;
	}
	
	public function get_D():Bool {
		return D;
	}

}