/**
 * @file   		TestNativeMaps.as
 * @created		16/07/2016
 */
package
{
	import com.distriqt.extension.nativemaps.NativeMaps;
	import com.distriqt.extension.nativemaps.events.NativeMapEvent;
	import com.distriqt.extension.nativemaps.objects.MapType;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class TestNativeMaps extends Sprite
	{
		////////////////////////////////////////////////////////
		//	CONSTANTS
		//


		////////////////////////////////////////////////////////
		//	VARIABLES
		//
		protected var _created :Boolean = false;
		protected var _step:int = 0;

		
		////////////////////////////////////////////////////////
		//	FUNCTIONALITY
		//

		/**
		 *  Constructor
		 */
		public function TestNativeMaps()
		{
			super();
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			if (stage)
			{
				onAddedToStage(null);
			}
			else
			{
				stage.addEventListener( Event.ADDED_TO_STAGE, onAddedToStage, false, 0, true );
			}
		}
		
		
		public function nextStep():void
		{
			if (!NativeMaps.isSupported) 
			{
				trace("NativeMaps is not supported on this device");
				return;
			}

			switch (_step)
			{
				case 0: 
					// First initialisation of extensions
					try
					{
						trace( "supported: " + NativeMaps.isSupported );
						trace( "version:   " + NativeMaps.service.version );
							
						NativeMaps.service.addEventListener(NativeMapEvent.MAP_CREATED, map_createdHandler, false, 0, true);
					}
					catch (e:Error)
					{
						trace( e );
					}
					break;

				case 1:
					// Create a map
					trace("Create Map")
					var viewport: Rectangle = new Rectangle( 0, 0, stage.stageWidth, stage.stageHeight );
					NativeMaps.service.createMap( viewport, MapType.MAP_TYPE_HYBRID );
					break;

				
				default:
					trace("dispose()")
					NativeMaps.service.dispose();
					_step = 0; 
					return;
			}

			_step++;
		}
		
		
		
		////////////////////////////////////////////////////////
		//	EVENT HANDLERS
		//
		
		private function onAddedToStage(event:Event):void
		{
			stage.addEventListener(MouseEvent.CLICK, mouseClickHandler, false, 0, true);
			trace("Ready - tap the screen to step through functionality");
		}

		private function mouseClickHandler( event:MouseEvent ):void
		{
			nextStep();
		}

		private function map_createdHandler( event:NativeMapEvent ):void
		{
			trace("Map created!");
		}
		
	}
}

