/**
 *        __       __               __ 
 *   ____/ /_ ____/ /______ _ ___  / /_
 *  / __  / / ___/ __/ ___/ / __ `/ __/
 * / /_/ / (__  ) / / /  / / /_/ / / 
 * \__,_/_/____/_/ /_/  /_/\__, /_/ 
 *                           / / 
 *                           \/ 
 * http://distriqt.com
 *
 * This is a test application for the distriqt extension
 * 
 * @author Michael Archbold & Shane Korin
 * 	
 */
package
{
	import com.distriqt.extension.appgroupdefaults.AppGroupDefaults;
	import com.distriqt.extension.appgroupdefaults.events.AppGroupDefaultsEvent;
	import com.distriqt.extension.permissions.Permissions;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.setInterval;
	import flash.utils.setTimeout;
	
	
	/**	
	 * Sample application for using the AppGroupDefaults Native Extension
	 */
	public class TestAppGroupDefaults extends Sprite
	{
		public static const KEY0 : String = "testKeyA";
		public static const KEY1 : String = "testKeyB";
		public static const KEY2 : String = "testKeyC";
		
		
		/**
		 * Class constructor 
		 */	
		public function TestAppGroupDefaults()
		{
			super();
			create();
			init();
		}
		
		
		//
		//	VARIABLES
		//
		
		private var _text		: TextField;
		
		
		//
		//	INITIALISATION
		//	
		
		private function create( ):void
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;

			var tf:TextFormat = new TextFormat();
			tf.size = 24;
			_text = new TextField();
			_text.defaultTextFormat = tf;
			addChild( _text );

			stage.addEventListener( Event.RESIZE, stage_resizeHandler, false, 0, true );
			stage.addEventListener( MouseEvent.CLICK, mouseClickHandler, false, 0, true );
			
		}
		
		
		private function init( ):void
		{
			try
			{
				
				////////////////////////////////////////////////
				// PERMISSIONS are needed for Android 6+ when using the FILE method
				//
				
//				Permissions.service.setPermissions( [
//						"android.permission.READ_EXTERNAL_STORAGE",
//						"android.permission.WRITE_EXTERNAL_STORAGE"
//						]);
//
//				if (!Permissions.service.hasAuthorisation())
//					Permissions.service.requestAuthorisation();

				//
				////////////////////////////////////////////////
				
				
				message( "AppGroupDefaults Supported: " + AppGroupDefaults.isSupported );
				if (AppGroupDefaults.isSupported)
				{
					message( "AppGroupDefaults Version:   " + AppGroupDefaults.service.version );
					
					AppGroupDefaults.service.addEventListener( AppGroupDefaultsEvent.INITIALISED, initialisedHandler );
					AppGroupDefaults.service.setup( "12345678", "group.com.distriqt.test", AppGroupDefaults.TYPE_CONTENTPROVIDER );
					
					setInterval( function():void {
						listKeys();
//						message( "value = " + AppGroupDefaults.service.getValue( KEY0 ) );
					}, 5000 );
				}
			}
			catch (e:Error)
			{
				message( "ERROR::"+e.message );
			}
		}
		
		
		private function initialisedHandler( event:AppGroupDefaultsEvent ):void
		{
			message( "initialisedHandler()" );
			listKeys();
		}
		
		
		private function listKeys():void
		{
			message( "----------" );
			var keys:Array = AppGroupDefaults.service.getKeys();
			for each (var key:String in keys)
			{
				message( "\t" + key + " = " + AppGroupDefaults.service.getValue( key ));
			}
			message( "----------" );
		}
		
		
		//
		//	FUNCTIONALITY
		//
		
		private function message( str:String ):void
		{
			trace( str );
			_text.text = str+"\n"+_text.text;
		}
		
		
		//
		//	EVENT HANDLERS
		//
		
		private function stage_resizeHandler( event:Event ):void
		{
			_text.y = 40;
			_text.width  = stage.stageWidth;
			_text.height = stage.stageHeight - 100;
		}
		
		private var state : int = 0;
		private function mouseClickHandler( event:MouseEvent ):void
		{
			var value:String = "some_value_"+String(Math.round( Math.random()*1000 ));
			
			if (AppGroupDefaults.isSupported)
			{
				switch (state)
				{
					case 0:
						message( "setValue( "+ KEY0 +", " + value + " )" );
						AppGroupDefaults.service.setValue( KEY0, value );
						message( "setValue( "+ KEY1 +", " + value + " )" );
						AppGroupDefaults.service.setValue( KEY1, value );
						message( "setValue( "+ KEY2 +", " + value + " )" );
						AppGroupDefaults.service.setValue( KEY2, value );
						
						setTimeout( listKeys, 50 );
						break;
					
					case 1:
						message( "remove( "+ KEY0 +" )" );
						AppGroupDefaults.service.remove( KEY0 );
						
						setTimeout( listKeys, 50 );
						break;
				
					case 2:
						message( "removeAll()" );
						AppGroupDefaults.service.removeAll();
						
						setTimeout( listKeys, 50 );
						break;
					
					default:
						state = 0;
						return;
				}
				state++;
			}
			
		}
		
		
	}
}

