package {
	import com.bit101.components.VBox;
	import com.netgrind.core.Scheduler;
	import com.netgrind.core.Utils;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author netgrind
	 */
	public class MissionTimer extends Sprite {
		
		private var active:Vector.<Mission> = new Vector.<Mission>();
		private var texts:Vector.<TextField> = new Vector.<TextField>();
		private var box:VBox;
		
		public function MissionTimer() {
			super();
			box = new VBox(this);
			Scheduler.invokeEvery(update);
		}
		
		private function update():void {
			for (var i:int = 0; i < active.length; i++) {
				texts[i].text = ""+format(active[i].time);
			}
		}
		
		public function format(i:Number):Number {
			return Math.floor(i*100) / 100;
		}
		
		public function addTimer(m:Mission):void {
			if (active.indexOf(m) != -1) return;
			active.push(m);
			var t:TextField = new TextField();
			t.defaultTextFormat = new TextFormat(null, 16, m.char.color);
			texts.push(t);
			box.addChild(t);
		}
		
		public function removeTimer(m:Mission):void {
			var i:int = active.indexOf(m);
			if (i == -1) return;
			active.splice(i, 1);
			Utils.removeChild(texts[i]);
			texts.splice(i, 1);
		}
		
	}
	
}