package
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	import FlDosBox.FlDosBox;
	import FlDosBox.FlDosBoxSpecialChars;
	
	/**
	 * ...
	 * @author Michael Miriti
	 */
	[Frame(factoryClass="Preloader")]
	
	public class Main extends FlDosBox
	{
		public static const STATE_MENU:int = 0;
		public static const STATE_GAME:int = 1;
		public static const STATE_CATSCENE:int = 2;
		
		public static var i:Main;
		public static var keys:Vector.<Boolean> = new Vector.<Boolean>(256);
		
		private var menu:MainMenu;
		private var map:MapManager;
		private var catscene:Catscene;
		
		[Embed(source="../assets/maps/maps.png")]
		private var _testMap:Class;
		private var state:int = STATE_MENU;
		
		public function Main():void
		{
			i = this;
			super(80, 25, 1);
			if (stage)
				init();
			else
				addEventListener(Event.ADDED_TO_STAGE, init);
			focusRect = false;
		}
		
		public function showCutscene(cs:Catscene):void
		{
			catscene = cs;
			setState(STATE_CATSCENE);
		}
		
		private function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			addEventListener(Event.ACTIVATE, onActivate);
			menu = new MainMenu();
			menu.callback = function():void
			{
				var cs:Catscene = new Catscene("EPISODE 7", "One Minute to Escape", 2000);
				cs.callback = function():void
				{
					map = new MapManager();
					map.initEscape();
					state = STATE_GAME;
				}
				showCutscene(cs);
			}
			stage.focus = this;
			Sounds.getI();
		}
		
		private function onActivate(e:Event):void
		{
			stage.focus = this;
		}
		
		private function onMouseDown(e:MouseEvent):void
		{
			stage.focus = this;
		}
		
		private function onKeyUp(e:KeyboardEvent):void
		{
			keys[e.keyCode] = false;
		}
		
		private function onKeyDown(e:KeyboardEvent):void
		{
			keys[e.keyCode] = true;
		}
		
		override protected function update(deltaTime:Number):void
		{
			if (state == STATE_MENU)
				menu.update(deltaTime);
			if (state == STATE_GAME)
				map.update(deltaTime);
			if (state == STATE_CATSCENE)
				catscene.update(deltaTime);
		}
		
		public function get isLeft():Boolean
		{
			return keys[Keyboard.LEFT] || keys[Keyboard.A];
		}
		
		public function get isRight():Boolean
		{
			return keys[Keyboard.RIGHT] || keys[Keyboard.D];
		}
		
		public function get isUp():Boolean
		{
			return keys[Keyboard.UP] || keys[Keyboard.W];
		}
		
		public function get isDown():Boolean
		{
			return keys[Keyboard.DOWN] || keys[Keyboard.S];
		}
		
		public function setState(st:int):void
		{
			state = st;
		}
	
	}

}