package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.ui.Mouse;
	import flash.ui.MouseCursorData;
	import starling.core.Starling;
	import starling.events.Event;
	import starling.utils.AssetManager;
	import tiles.TileNames;
	
	/**
	 * ...
	 * @author RedMaster
	 */
	public class Main extends Sprite 
	{		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(flash.events.Event.ADDED_TO_STAGE, init);
		}
		private function init(e:flash.events.Event = null):void
		{
			removeEventListener(flash.events.Event.ADDED_TO_STAGE, init);
			
			stage.addChild(C.preloader);
			
			Log.showTime = false;
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			C.screen = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
			C.starling = new Starling(Game, stage, C.screen);
			if (C.debugging) C.starling.showStatsAt("right", "top");
			C.starling.addEventListener(starling.events.Event.CONTEXT3D_CREATE, onStarlingInit);
			TileNames.init();
			//TileNames.dumpTable();
		}
		private function onStarlingInit():void
		{
			Log.log(this, "Starting loading assets");
			C.assets = new AssetManager(1, true);
			C.assets.enqueue(File.applicationDirectory.resolvePath("resources"));
			C.assets.loadQueue(onProgress);
			C.assets.verbose = C.debugging;
		}
		private function onProgress(progress:Number):void
		{
			if (progress == 1)
			{
				C.debug = C.debugShape.graphics;
				C.starling.start();
				Log.log(this, "Assets loaded");
				C.assetsLoaded = true;
				C.starling.dispatchEventWith(Events.RESOURCES_LOADED);
			}
		}
	}
}