package display.states 
{
	import display.BaseState;
	import display.gui.GUIBackground;
	import display.gui.GUIButton;
	import display.gui.GUISwitchButton;
	import flash.geom.Point;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import map.MapOptions;
	import starling.events.Event;
	import starling.text.TextField;
	import starling.utils.HAlign;
	import flash.utils.setTimeout;
	
	/**
	 * ...
	 * @author RedMaster
	 */
	public class NewWorldState extends BaseState 
	{
		private var _sizeInput:flash.text.TextField;
		private var _sizeLabel:starling.text.TextField;
		private var _seedInput:flash.text.TextField;
		private var _seedLabel:starling.text.TextField;
		private var _nameLabel:TextField;
		private var _nameInput:flash.text.TextField;
		private var _header:TextField;
		private var _hardnessLabel:TextField;
		
		private var _bg:GUIBackground;
		
		private var _back:GUIButton;
		private var _makeSeed:GUIButton;
		private var _play:GUIButton;
		private var _hardness:GUISwitchButton;
		
		private var _controlsPos:Point;
		
		override public function init():void 
		{
			_bg = new GUIBackground();
			
			_controlsPos = new Point(250, 200);
			
			_header = new TextField(300, 30, "Генерация мира", C.FONT_NAME, 20, 0xFFFFDD);
			_header.x = _controlsPos.x + 100;
			_header.y = _controlsPos.y;
			
			_nameLabel = new TextField(100, 32, "Имя:", C.FONT_NAME, 20, 0xFFFFDD);
			_nameLabel.x = _controlsPos.x;
			_nameLabel.y = _header.y + _header.height + 5;
			_nameLabel.hAlign = HAlign.RIGHT;
			
			_sizeLabel = new TextField(100, 32, "Размер:", C.FONT_NAME, 20, 0xFFFFDD);
			_sizeLabel.x = _controlsPos.x;
			_sizeLabel.y = _nameLabel.y + _nameLabel.height + 5;
			_sizeLabel.hAlign = HAlign.RIGHT;
			
			_seedLabel = new TextField(100, 32, "Семя:", C.FONT_NAME, 20, 0xFFFFDD);
			_seedLabel.x = _controlsPos.x;
			_seedLabel.y = _sizeLabel.y + _sizeLabel.height + 5;
			_seedLabel.hAlign = HAlign.RIGHT;
			
			_makeSeed = new GUIButton("Сгенерировать семя");
			_makeSeed.x = _seedLabel.x + _seedLabel.width;
			_makeSeed.y = _seedLabel.y + _seedLabel.height + 5;
			
			_hardnessLabel = new TextField(150, 32, "Сложность:", C.FONT_NAME, 20, 0xFFFFDD);
			_hardnessLabel.x = _controlsPos.x - 50;
			_hardnessLabel.y = _makeSeed.y + _makeSeed.height + 5;
			_hardnessLabel.hAlign = HAlign.RIGHT;
			
			_hardness = new GUISwitchButton(MapOptions.MEDIUM, MapOptions.HARD, MapOptions.EASY);
			_hardness.x = _hardnessLabel.x + _hardnessLabel.width;
			_hardness.y = _hardnessLabel.y;
			
			_play = new GUIButton("Играть");
			_play.x = _makeSeed.x;
			_play.y = _hardnessLabel.y + _hardnessLabel.height + 30;
			
			_back = new GUIButton("В меню");
			_back.x = 5;
			_back.y = C.screen.height - _back.height - 5;
			
			_nameInput = new flash.text.TextField();
			_nameInput.defaultTextFormat = new TextFormat(C.FONT_NAME, 24, 0xFFFFDD);
			_nameInput.x = _nameLabel.x + _nameLabel.width;
			_nameInput.y = _nameLabel.y;
			_nameInput.width = 300;
			_nameInput.height = 30;
			_nameInput.border = true;
			_nameInput.borderColor = 0xFFFFDD;
			_nameInput.type = TextFieldType.INPUT;
			_nameInput.text = "Новый мир";
			
			_sizeInput = new flash.text.TextField();
			_sizeInput.defaultTextFormat = new TextFormat(C.FONT_NAME, 24, 0xFFFFDD);
			_sizeInput.x = _sizeLabel.x + _sizeLabel.width;
			_sizeInput.y = _sizeLabel.y;
			_sizeInput.width = 300;
			_sizeInput.height = 30;
			_sizeInput.border = true;
			_sizeInput.borderColor = 0xFFFFDD;
			_sizeInput.type = TextFieldType.INPUT;
			_sizeInput.text = "512";
			
			_seedInput = new flash.text.TextField();
			_seedInput.defaultTextFormat = new TextFormat(C.FONT_NAME, 24, 0xFFFFDD);
			_seedInput.x = _seedLabel.x + _seedLabel.width
			_seedInput.y = _seedLabel.y;
			_seedInput.width = 300;
			_seedInput.height = 30;
			_seedInput.border = true;
			_seedInput.borderColor = 0xFFFFDD;
			_seedInput.type = TextFieldType.INPUT;
			_seedInput.text = Math.round(Math.random() * uint.MAX_VALUE).toString();
			
			_makeSeed.addEventListener(Event.TRIGGERED, onMakeSeedTrigger);
			_back.addEventListener(Event.TRIGGERED, onBackTrigger);
			_play.addEventListener(Event.TRIGGERED, onPlayTrigger);
			
			C.starling.nativeOverlay.addChild(_nameInput);
			C.starling.nativeOverlay.addChild(_sizeInput);
			C.starling.nativeOverlay.addChild(_seedInput);
			
			addChild(_bg);
			addChild(_header);
			addChild(_nameLabel);
			addChild(_sizeLabel);
			addChild(_seedLabel);
			addChild(_makeSeed);
			addChild(_hardnessLabel);
			addChild(_hardness);
			addChild(_play);
			addChild(_back);
		}
		
		private function onMakeSeedTrigger(e:Event):void
		{
			_seedInput.text = Math.round(Math.random() * uint.MAX_VALUE).toString();
		}
		private function startGenerating():void
		{
			var opts:MapOptions = new MapOptions();
			opts.seed = parseInt(_seedInput.text);
			if (opts.seed == 0 || isNaN(opts.seed)) opts.seed = Math.round(Math.random() * uint.MAX_VALUE);
			opts.size = new Point(parseInt(_sizeInput.text), parseInt(_sizeInput.text));
			if (!opts.size || opts.size == new Point()) opts.size = new Point(512, 512);
			opts.name = _nameInput.text;
			if (opts.name.length <= 0) opts.name = "Новый мир";
			opts.createNew  = true;
			opts.hardness = _hardness.state;
			Game.Playing.setOptions(opts);
			C.game.showState(Game.Playing);
		}
		private function onPlayTrigger(e:Event):void
		{
			C.starling.nativeStage.addChild(C.preloader);
			setTimeout(startGenerating, 100);
		}
		private function onBackTrigger(e:Event):void
		{
			C.game.showState(Game.MainMenu);
		}
		
		override public function destroy():void 
		{
			removeChildren(0, -1, true);
			_makeSeed.removeEventListeners();
			_play.removeEventListeners();
			_back.removeEventListeners();
			C.starling.nativeOverlay.removeChild(_sizeInput);
			C.starling.nativeOverlay.removeChild(_seedInput);
			C.starling.nativeOverlay.removeChild(_nameInput);
		}
	}
}