package entities
{
	import org.flixel.*;
	import util.DynamicText;

	public class HUD extends FlxGroup
	{
		public var lvlText:DynamicText;
		private var TimeTxt:FlxText;
		private var gameTimer:FlxTimer;	
		
		public function HUD() 
		{
			super();
				
			gameTimer  = new FlxTimer();
			

			TimeTxt = new FlxText(FlxG.width -50, 10, 50);
			TimeTxt.size = 18;
			TimeTxt.scrollFactor.x = 0;
			TimeTxt.scrollFactor.y = 0;
			TimeTxt.shadow = 0xffffffff;
			TimeTxt.color = 0xff333333;
			add(TimeTxt);
			
		}
				
		public function startTimer(time:int):void
		{
			gameTimer.start(time,1,onTimer);
		}
		
		override public function update():void
		{
			super.update();
			
			if(gameTimer.timeLeft > 10){
				TimeTxt.text = Math.floor(gameTimer.timeLeft).toString();
				}
			else{
				TimeTxt.text = Math.floor(gameTimer.timeLeft).toString();
				TimeTxt.color = 0xffff0000;
				TimeTxt.size = 20;
			}
			
			
			if (FlxG.keys.P){ 
				gameTimer.paused = !gameTimer.paused;
			}
		}

		private function onTimer(Timer:FlxTimer):void
		{
			
			FlxG.shake(0.01, 0.2, onShake);
			TimeTxt.text = "00:00"
		}
		
		protected function onShake():void
		{
			FlxG.fade(0xff000000, 2, onFade);
		}
		
		protected function onFade():void
		{		
			FlxG.resetState();
		}
		
		public function showLevel():void
		{
			lvlText = new DynamicText(FlxG.width / 3, FlxG.height / 4, 300, "Nivel " + Registry.currentLevel.toString()).fadeAndDie();
			Registry.currentLevel++;
		}
		
	}

}