package com.isartdigital.game;

import com.isartdigital.game.sprites.Arrow;
import com.isartdigital.game.sprites.Player;
import com.isartdigital.game.sprites.Score;
import com.isartdigital.utils.game.CollisionManager;
import com.isartdigital.utils.game.GameStage;
import com.isartdigital.utils.sounds.SoundManager;
import com.isartdigital.utils.system.DeviceCapabilities;
import com.isartdigital.game.Controller;
import haxe.Timer;
import pixi.core.display.DisplayObject;
import pixi.core.graphics.Graphics;
import pixi.core.math.Point;
import pixi.core.math.shapes.Rectangle;
import com.isartdigital.game.sprites.InputZone;
	
/**
 * ...
 * @author Nathan
 */
class MoveManager 
{
	
	/**
	 * instance unique de la classe MoveManager
	 */
	private static var instance: MoveManager;
	
	public static var arrayArrow: Array<Arrow> = new Array<Arrow>();
	
	private static var arrayDirection: Array<String> = new Array<String>();
	
	private static var createArrow: Bool = true;
	
	//private static var InputZone: Graphics = new Graphics();
	private static var boxInputZone: Graphics = new Graphics();
	
	private static inline var MAXCOMBO: Int = 8;
	public static var combo:Int = 0;
	
	/**
	 * 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 (): MoveManager {
		if (instance == null) instance = new MoveManager();
		
		return instance;
	}
	
	/**
	 * constructeur privé pour éviter qu'une instance soit créée directement
	 */
	private function new() 
	{
		GameStage.getInstance().addChild(InputZone.getInstance());
		InputZone.getInstance().init();
	}
	
	public function init() : Void {
		arrayDirection.push("Left");
		arrayDirection.push("Right");
		arrayDirection.push("Up");
		arrayDirection.push("Down");
	}
	
	public static function doAction () {
		if (arrayArrow.length < 9 && createArrow) {
			
			var lArrow: Arrow = new Arrow(arrayDirection[Math.ceil(Math.random() * arrayDirection.length - 1)]);
			lArrow.start();
			lArrow.init();
			//lArrow.position.set(DeviceCapabilities.getScreenRect(GameStage.getInstance()).x / 2, DeviceCapabilities.getScreenRect(GameStage.getInstance()).x / 2);
			lArrow.x = DeviceCapabilities.getScreenRect(GameStage.getInstance()).width + lArrow.width;
			lArrow.y = lArrow.height/1.5;
			arrayArrow.push(lArrow);

			createArrow = false;
			GameStage.getInstance().addChild(lArrow);
			Timer.delay(function() { createArrow = true; }, 1000);
		}
		
		for (i in 0...arrayArrow.length) {
			if (CollisionManager.hitTestObject(arrayArrow[i].hitBox, InputZone.getInstance().hitBox)) {
				if (Controller.getInstance().get_down() && getArrowName("Down", i)) {
					Player.getInstance().setModeDown();
					valideArrow(i);
					checkFxArrow("Couche12flèche1", "Couche3flèchebas");
				}
				if (Controller.getInstance().get_up() && getArrowName("Up", i)) {
						
					valideArrow(i);
					Player.getInstance().setModeUp();
					
					checkFxArrow("Couche12flèche2", "Couche3flèchehaut");
				}
				if (Controller.getInstance().get_left() && getArrowName("Left", i)) {
					valideArrow(i);
					Player.getInstance().setModeLeft();
					
					checkFxArrow("Couche12flèche3", "Couche3flèchegauche");
				}
				if (Controller.getInstance().get_right() && getArrowName("Right", i)) {
					valideArrow(i);
					Player.getInstance().setModeRight();
					checkFxArrow("Couche12flèche4", "Couche3flèchedroite");
				}
			}
			//Controller.getInstance().reset();
			//Controller.getInstance().reset();
		}
	}
	
	private static function checkFxArrow(pSon_1:String, pSon_2:String):Void {
		if (GameManager.levelLayer != 2) SoundManager.getSound(pSon_1).play();
		else SoundManager.getSound(pSon_2).play();
	}
	
	private static function valideArrow(i:Int):Void {
		arrayArrow[i].alpha = 0;
		Score.getInstance().actualScore += 100;
		combo++;
		
		if (combo == MAXCOMBO) {
			
			if ( GameManager.levelLayer < GameManager.MAXLAYER) GameManager.levelLayer ++;
			
			//Condition de sons
			if (GameManager.getInstance().currentSound == "MainloopCouche1") {
				SoundManager.getSound(GameManager.getInstance().currentSound).fade(1, 0, 1000);
				GameManager.getInstance().currentSound = "MainloopCouche2";
				SoundManager.getSound(GameManager.getInstance().currentSound).fade(0, 1, 1000);
			}
			else if (GameManager.getInstance().currentSound == "MainloopCouche2") {
				SoundManager.getSound(GameManager.getInstance().currentSound).fade(1, 0, 1000);
				GameManager.getInstance().currentSound = "MainloopCouche3";
				SoundManager.getSound(GameManager.getInstance().currentSound).fade(0, 1, 1000);
			}
			
			combo = 0;
			
		}
	}
	
	private static function getArrowName(pName,i):Bool {
		return arrayArrow[i].getAssetName().substring(0, arrayArrow[i].getAssetName().indexOf("Arrow")) == pName;
	}
	
	/**
	 * détruit l'instance unique et met sa référence interne à null
	 */
	public function destroy (): Void {
		instance = null;
	}

}