package {
	import com.greensock.TweenLite;
	import com.netgrind.core.Scheduler;
	import com.netgrind.core.Utils;
	import com.netgrind.core.UtilsArt;
	import flash.display.Sprite;
	
	/**
	 * ...
	 * @author netgrind
	 */
	public class Mission {
		
		public var started:Boolean = false;
		public var minSpeed:Number = 30;
		public var points:Vector.<MissionWaypoint>;
		public var target:int = 0;
		public var car:Car;
		public var level:Level;
		public var char:Character;
		public var goalTimes:Array = [];
		public var goalPoints:Array = [];
		public var time:Number = 0;
		public var callDialog:Vector.<TextChunk>;
		public var talking:Boolean = false;
		public var rangeSprite:Sprite;
		public var isRace:Boolean = false;
		public var callback:Function;
		
		public function Mission(points:Vector.<MissionWaypoint>, characterIndex:int, callDialog:Vector.<TextChunk>) {
			super();
			this.points = points;
			rangeSprite = UtilsArt.makeCircle();
			rangeSprite.alpha = .3;
			char = Character.all[characterIndex];
			this.callDialog = callDialog;
		}
		
		public function update():void {
			if (talking) return;
			time+= Scheduler.deltaMS / 1000;
			if (Utils.getDistance(points[target].waypoint, car) < points[target].distance) {
				if(points[target].requireStop){
					if (car.speed < minSpeed) {
						displayDialog();						
					}
				}else {
					displayDialog();
				}
			}
		}
		
		public function activate(car:Car, level:Level):void {
			this.car = car;
			this.level = level;
			if (callDialog != null) {
				target = -1;
				displayDialog(callDialog);
			}else {
				char.addToWaypoint(points[0].waypoint); 
			}
			Scheduler.invokeEvery(update);
		}
		
		private function displayDialog(dialog:Vector.<TextChunk> = null):void {
			if (target == 0) started = true;
			if (dialog == null) dialog = points[target].dialog;
			talking = true;
			level.displayDialog(dialog,nextWaypoint, char);
		}		
		
		public function nextWaypoint():void {
			if (target >= 0) {
				Utils.removeChild(rangeSprite, false);
				level.hudTarget.removePoint(points[target].waypoint.position);
				if (points[target].spawnMission != null && points[target].spawnMission.length != 0) {
					level.startMission(points[target].spawnMission);
				}
				if (points[target].enterCar) {
					char.moveToMirror(level.mirror);
				}else if (points[target].exitCar) {
					char.removeFromMirror(level.mirror);
				}
			}else {
				level.timer.addTimer(this);
			}
			target++;
			if (target >= points.length) {
				finishMission();
			}else {
				drawRangeSprite();
				
				points[target].waypoint.addChild(rangeSprite);
				level.hudTarget.addPoint(points[target].waypoint.position);
			}
			talking = false;
		}
		
		private function drawRangeSprite():void {
			rangeSprite.graphics.clear();
			var color:uint = char.color;
			if (isRace && target == points.length - 1) color = 0x00ff00;
			rangeSprite.graphics.beginFill(color);
			rangeSprite.graphics.drawCircle(0, 0, points[target].distance);
			rangeSprite.graphics.endFill();
		}
		
		public function beginMission():void {
			started = true;
		}
		
		public function finishMission():void {
			level.timer.removeTimer(this);
			level.endMission(this);
			if (callback!=null) callback();
			kill();
		}
		
		public function kill():void {
			Scheduler.cancel(update);
			started = false;
		}
		
		/*public static function createRandom(car:Car, level:Level):Mission {
			var start:Waypoint = Waypoint.getUnused();
			if (start == null) return null;
			start.isUsed = true;
			var end:Waypoint = Waypoint.getUnused();
			if (end == null) return null;
			end.isUsed = true;
			start.waitTime = 1000;
			start.requireStop = true;
			end.requireStop =  true;
			end.waitTime = 0;
			return new Mission(new <Waypoint>[start,end], car, level, new Character());
		}*/
		
	}
	
}