package com.isartdigital.game.sprites;

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 js.Browser;
import pixi.core.math.Point;
import pixi.extras.MovieClip;

/**
 * ...
 * @author Nathan
 */
class Arrow extends StateGraphic
{
	
	public var currentSpeed:Int = 0;
	private static var speeds:Array<Int> = [4, 8, 10];

	public static var arrowsArray: Array<Arrow> = new Array<Arrow>();
	
	private static var firstArrow:Bool = false;
	
	public function new(pString:String)
	{
		if (!firstArrow) {
			firstArrow = true;
			return;
		}
		super();
		factory = new MovieClipAnimFactory();
		boxType = BoxType.SIMPLE;
		assetName = pString + "Arrow";
		arrowsArray.push(this);
		currentSpeed = speeds[GameManager.levelLayer];
	}
	
	public function init() {
		setModeNormal();
	}
	
	private function setPosition (pGlobal:Point):Void {
		var lLocal:Point = parent.toLocal(pGlobal);
		position.set(lLocal.x, lLocal.y);
	}
	
	override private function setModeNormal():Void {
		setState(DEFAULT_STATE, true);
		cast(anim,MovieClip).anchor.set(0.5, 0.5);
		super.setModeNormal();
	}
	
	override function doActionNormal():Void 
	{
		currentSpeed = speeds[GameManager.levelLayer];
		x -= currentSpeed;
		if (x < DeviceCapabilities.getScreenRect(GameStage.getInstance()).x + DeviceCapabilities.getScreenRect(GameStage.getInstance()).width / 4) alpha = 0.2;
		if (x < DeviceCapabilities.getScreenRect(GameStage.getInstance()).x - width) {
			// GameStage.getInstance().removeChild(this);
			remove();
			arrowsArray.slice(arrowsArray.indexOf(this));
			if (MoveManager.arrayArrow.indexOf(this) != -1) {
				MoveManager.arrayArrow.splice(MoveManager.arrayArrow.indexOf(this),1);
			}
			this.destroy();
			return;
		}
		super.doActionNormal();
	}
	
	public function getAssetName(): String {
		return assetName;
	}
	
	public function remove():Void {
		parent.removeChild(this);
		destroy();
	}
}