package display 
{
	import flash.geom.Point;
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.text.TextField;
	import starling.utils.HAlign;
	import display.gui.GUIButton;
	
	/**
	 * ...
	 * @author RedMaster
	 */
	public class DeathOverlay extends Sprite 
	{
		private var _bg:Quad = new Quad(C.screen.width, C.screen.height, 0x000000);
		private var _header:TextField = new TextField(400, 120, "Игра окончена", C.FONT_NAME, 24, 0xFFFFDD);
		private var _minedLabel:TextField = new TextField(150, 30, "Добыто:", C.FONT_NAME, 24, 0xFFFFDD);
		private var _minedDisplay:MinedDisplay = new MinedDisplay();
		private var _retry:GUIButton = new GUIButton("Последнее сохранение");
		private var _menu:GUIButton = new GUIButton("В меню");
		
		private var _controlsPos:Point = new Point(350, 100);
		
		public static const MENU:String = "mainMenu";
		public static const RETRY:String = "retry";
		
		public function DeathOverlay() 
		{
			_bg.alpha = 0.8;
			
			_header.x = _controlsPos.x - 50;
			_header.y = _controlsPos.y;
			
			_minedLabel.x = _controlsPos.x - 50;
			_minedLabel.y = _controlsPos.y + _header.height + 5;
			_minedLabel.hAlign = HAlign.RIGHT;
			
			_minedDisplay.x = _minedLabel.x + _minedLabel.width;
			_minedDisplay.y = _minedLabel.y;
			
			_retry.x = _controlsPos.x;
			_retry.y = _minedLabel.y + _minedLabel.height + 5;
			
			_menu.x = _retry.x;
			_menu.y = _retry.y + _retry.height + 5;
			
			_retry.addEventListener(Event.TRIGGERED, onRetryTrigger);
			_menu.addEventListener(Event.TRIGGERED, onMenuTrigger);
			
			addChild(_bg);
			addChild(_header);
			addChild(_minedLabel);
			addChild(_minedDisplay);
			addChild(_retry);
			addChild(_menu);
		}
		
		public function show(balrog:Boolean = false):void
		{
			if (!balrog)
			{
				visible = true;
				alpha = 0;
				C.starling.juggler.tween(this, 0.2, {alpha:1} );
				return;
			}
			_header.text = "Вы выпустили древнее зло,\nи оно уничтожило все вокруг.\nИгра окончена";
			visible = true;
			alpha = 0;
			C.starling.juggler.tween(this, 3, {alpha:1} );
		}
		
		public function set retryEnabled(value:Boolean):void
		{
			if (value) _retry.enabled = true;
			else _retry.enabled = false;
		}
		
		public function set mined(value:uint):void
		{
			_minedDisplay.mined = value;
		}
		public function set total(value:uint):void
		{
			_minedDisplay.total = value;
		}
		private function onRetryTrigger(e:Event):void
		{
			dispatchEventWith(RETRY);
		}
		private function onMenuTrigger(e:Event):void
		{
			dispatchEventWith(MENU);
		}
	}
}