package  
{
	import engine.Button;
	import engine.Label;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	/**
	 * ...
	 * @author beisik
	 */
	public class HUD extends Sprite 
	{
		private var hpSprite: Sprite;
		
		private var lvlNumber: Label, lvlNumberShadow: Label;
		private var lvlCaption: Label, lvlCaptionShadow: Label;
		public var lvlCaptionTimer: Timer;
		
		private var _menuCaption: Label, _menuAuthor: Label, _menuIGDC: Label;
		private var _menuPlay: Button;
		public var menu: Sprite;
		
		public function HUD() 
		{
			hpSprite = new Sprite();
			hpSprite.y = 20;
			hpSprite.x = 26;
			addChild(hpSprite);
			
			menu = new Sprite();
			
			_menuCaption = new Label(0, 100, "Куику!", 0xFFFFFF, "Nobr1", 64);
			_menuCaption.x = (Game.screenWidth - _menuCaption.width) / 2;
			menu.addChild(_menuCaption);
			
			var shadow: Label = new Label(0, 204, "Играть", 0x999999, "Neucha", 48);
			shadow.x = (Game.screenWidth - shadow.width) / 2 + 4;
			menu.addChild(shadow);
			_menuPlay = new Button(0, 200, "Играть", World.world.Play, 0xFFFFFF, "Neucha", 48);
			_menuPlay.x = (Game.screenWidth - _menuPlay.width) / 2;
			menu.addChild(_menuPlay);
			
			shadow = new Label(32 + 2, Game.screenHeight - 48 - 28 + 2, "сделал бейсик", 0x999999, "Neucha", 28);
			menu.addChild(shadow);
			_menuAuthor = new Label(32, Game.screenHeight - 48 - 28, "сделал бейсик", 0xFFFFFF, "Neucha", 28);
			menu.addChild(_menuAuthor);
			
			shadow = new Label(0, Game.screenHeight - 48 - 28 + 2, "для IGDC.ru", 0x999999, "Neucha", 28);
			shadow.x = Game.screenWidth - shadow.width - 32 + 2;
			menu.addChild(shadow);
			_menuIGDC = new Label(0, Game.screenHeight - 48 - 28, "для IGDC.ru", 0xFFFFFF, "Neucha", 28);
			_menuIGDC.x = Game.screenWidth - _menuIGDC.width - 32;
			menu.addChild(_menuIGDC);
			
			lvlNumber = new Label(0, 0, "", 0xFFFFFF, "Neucha", 16);
			lvlNumberShadow = new Label(0, 0, "", 0x999999, "Neucha", 16);
			lvlCaption = new Label(0, 0, "", 0xFFFFFF, "Neucha", 48);
			lvlCaptionShadow = new Label(0, 0, "", 0x999999, "Neucha", 48);
			lvlCaptionTimer = new Timer(1000, 1);
			lvlCaptionTimer.addEventListener(TimerEvent.TIMER, onLvlTick);
			lvlCaptionTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onLvlComplete);
		}
		
		public function GameOver(fail: Boolean): void
		{
			if (fail)
				_menuCaption.text = "Чувак, это гамовер";
			else
				_menuCaption.text = "Это победа, бро!";
			_menuCaption.x = (Game.screenWidth - _menuCaption.width) / 2;
		}
		
		public function hp(): void
		{
			while (hpSprite.numChildren < World.world.hero.life)
			{
				var healthClip: HealthClip = new HealthClip();
				healthClip.x = hpSprite.numChildren * 30;
				hpSprite.addChild(healthClip);
			}
			while (hpSprite.numChildren > Math.max(0, World.world.hero.life))
				hpSprite.removeChildAt(hpSprite.numChildren - 1);
		}
		
		public function levelCaption(number: uint, caption: String): void
		{
			lvlNumber.text = "уровень " + number;
			lvlCaption.text = caption.toUpperCase();
			lvlCaption.x = (Game.screenWidth - lvlCaption.width) / 2;
			lvlNumber.x = (Game.screenWidth - lvlCaption.width) / 2;
			lvlNumber.y = 100;
			lvlCaption.y = lvlNumber.y + lvlNumber.height;
			lvlCaption.alpha = 0;
			lvlNumber.alpha = 0;
			lvlNumberShadow.text = "уровень " + number;
			lvlCaptionShadow.text = caption.toUpperCase();
			lvlCaptionShadow.x = (Game.screenWidth - lvlCaptionShadow.width) / 2 + 2;
			lvlNumberShadow.x = (Game.screenWidth - lvlCaptionShadow.width) / 2 + 2;
			lvlNumberShadow.y = 100 + 2;
			lvlCaptionShadow.y = lvlNumberShadow.y + lvlNumberShadow.height + 2;
			lvlCaptionShadow.alpha = 0;
			lvlNumberShadow.alpha = 0;
			addChild(lvlNumberShadow);
			addChild(lvlCaptionShadow);
			addChild(lvlNumber);
			addChild(lvlCaption);
			
			lvlCaptionTimer.delay = 50;
			lvlCaptionTimer.repeatCount = 11;
			lvlCaptionTimer.reset();
			lvlCaptionTimer.start();
		}
		
		private function onLvlTick(e: Event): void
		{
			if (lvlCaptionTimer.repeatCount == 11)
			{
				lvlCaption.alpha += 0.1;
				lvlNumber.alpha += 0.1;
				lvlCaptionShadow.alpha += 0.1;
				lvlNumberShadow.alpha += 0.1;
			}
			else
			{
				lvlCaption.alpha -= 0.1;
				lvlNumber.alpha -= 0.1;
				lvlCaptionShadow.alpha -= 0.1;
				lvlNumberShadow.alpha -= 0.1;
			}
		}
		
		private function onLvlComplete(e: Event): void
		{
			if (lvlCaptionTimer.repeatCount == 11)
			{
				lvlCaptionTimer.delay = 3000;
				lvlCaptionTimer.repeatCount = 1;
				lvlCaptionTimer.reset();
				lvlCaptionTimer.start();
			}
			else
				if (lvlCaptionTimer.repeatCount == 1)
				{
					lvlCaptionTimer.delay = 50;
					lvlCaptionTimer.repeatCount = 10;
					lvlCaptionTimer.reset();
					lvlCaptionTimer.start();
				}
				else
				{
					removeChild(lvlCaption);
					removeChild(lvlNumber);
					removeChild(lvlCaptionShadow);
					removeChild(lvlNumberShadow);
				}
		}
		
	}

}