package com.isartdigital.game.sprites;
import com.isartdigital.game.LevelManager;
import com.isartdigital.utils.game.BoxType;
import com.isartdigital.utils.game.factory.MovieClipAnimFactory;
import com.isartdigital.utils.game.GameStage;
import com.isartdigital.utils.game.StateGraphic;
import com.isartdigital.utils.system.DeviceCapabilities;
import pixi.core.math.shapes.Rectangle;
import pixi.extras.MovieClip;

	
/**
 * ...
 * @author Nathan
 */
class Player extends StateGraphic 
{
	
	/**
	 * instance unique de la classe Player
	 */
	private static var instance: Player;
	
	private static var IDLE_STATE : String = "idle";
	private static var LEFT_STATE : String = "left";
	private static var RIGHT_STATE : String = "right";
	private static var UP_STATE : String = "up";
	private static var DOWN_STATE : String = "down";
	
	private static var speed : Int = 10;
	
	public var leftState:Bool;
	public var rightState:Bool;
	
	/**
	 * 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 (): Player {
		if (instance == null) instance = new Player();
		return instance;
	}
	
	/**
	 * constructeur privé pour éviter qu'une instance soit créée directement
	 */
	private function new() 
	{
		super();
		factory = new MovieClipAnimFactory();
		boxType = BoxType.SIMPLE;
		//assetName = "Player";
		// ATTENTION: assetName et boxType se définissent après le super
	}
	
	public function init():Void {
		start();
		x = DeviceCapabilities.getScreenRect(GameStage.getInstance().getGameContainer()).x + DeviceCapabilities.getScreenRect(GameStage.getInstance().getGameContainer()).width / 2;
		y = DeviceCapabilities.getScreenRect(GameStage.getInstance().getGameContainer()).y + DeviceCapabilities.getScreenRect(GameStage.getInstance().getGameContainer()).height * 5/8 ;
		cast(anim,MovieClip).anchor.set(0.5, 0.5);
	}
	
	private function doActionMove() {
		var test:Int = 0;
		var lScreen:Rectangle = DeviceCapabilities.getScreenRect(GameStage.getInstance().getGameContainer());
		if (Controller.getInstance().Q || Controller.getInstance().D) {
			if (Controller.getInstance().Q && lScreen.x + lScreen.width * 7 / 16 < x) x -= speed;
			if (Controller.getInstance().D && lScreen.x + lScreen.width * 9 / 16 > x) x += speed;
		} else {
			if (x + speed >= lScreen.x + lScreen.width / 2 &&
				x - speed <= lScreen.x + lScreen.width / 2) {
					return;
			}
			if (lScreen.x + lScreen.width / 2 < x) x -= speed;
			else x += speed;
		}
		
		if ( x < lScreen.x + lScreen.width * 7.5/16) {
			leftState 	= true;
			rightState 	= false;
		}
		else if (x > lScreen.x + lScreen.width * 8.5 / 16) {
			leftState 	= false;
			rightState 	= true;
		}
		else {
			leftState 	= false;
			rightState 	= false;
		}
	}
	
	override function doActionNormal():Void 
	{
		untyped anim.animationSpeed = 0.18;
		doActionMove();
		super.doActionNormal();
	}
	
	override function setModeNormal():Void 
	{
		super.setModeNormal();
		setState(IDLE_STATE, true);
	}
	
	public function setModeUp():Void {
		setState(UP_STATE);
		doAction = doActionUp;
	}
	
	public function setModeDown():Void {
		setState(DOWN_STATE);
		doAction = doActionDown;
	}
	
	public function setModeLeft():Void {
		setState(LEFT_STATE);
		doAction = doActionLeft;
	}
	
	public function setModeRight():Void {
		setState(RIGHT_STATE);
		doAction = doActionRight;
	}
	
	private function doActionUp():Void {
		doActionMove();
		untyped anim.onComplete = function() { setModeNormal(); }
	}
	
	private function doActionDown():Void {
		doActionMove();
		untyped anim.onComplete = function() { setModeNormal(); }
	}
	
	private function doActionLeft():Void {
		doActionMove();
		untyped anim.onComplete = function() { setModeNormal(); }
	}
	
	private function doActionRight():Void {
		doActionMove();
		untyped anim.onComplete = function() { setModeNormal(); }
	}
	
	
	
	//private function doActionQ():Void {
		////anim Q
		//pickObject(0);
	//}
	//
	//private function doActionD():Void {
		//// anim D
		//pickObject(1);
	//}
	
	private function pickObject(index:Int) :Void {
		if (isAnimEnd) setModeNormal();
		// if (LevelManager.getInstance().toBePicked(LevelManager.getInstance().currentObjects[index])) Score ++;
	}
	
	/**
	 * détruit l'instance unique et met sa référence interne à null
	 */
	override public function destroy (): Void {
		instance = null;
	}

}