package {

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;

	import com.distriqt.extension.nativewebview.NativeWebView;
	import com.distriqt.extension.nativewebview.WebView;
	import com.distriqt.extension.nativewebview.WebViewOptions;
	import com.distriqt.extension.nativewebview.NativeWebViewOptions;
	import com.distriqt.extension.nativewebview.events.NativeWebViewEvent;

	public class TestNativeWebView extends MovieClip {

		private var webView:WebView;
		private var topBar:Sprite;
		private var closeBtn:Sprite;

		private const BAR_HEIGHT:int = 50;

		public function TestNativeWebView() {
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}

		private function onAddedToStage(e:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			init();
		}

		private function init():void {
			trace("[test] init - stage: " + stage.stageWidth + "x" + stage.stageHeight);

			buildTopBar();

			NativeWebView.service.addEventListener(NativeWebViewEvent.INITIALISED, onPlatformReady);
			NativeWebView.service.initialisePlatform(new NativeWebViewOptions());

			stage.addEventListener(Event.RESIZE, onResize);
		}

		private function buildTopBar():void {
			topBar = new Sprite();

			// background
			topBar.graphics.beginFill(0x333333);
			topBar.graphics.drawRect(0, 0, stage.stageWidth, BAR_HEIGHT);
			topBar.graphics.endFill();

			// title label
			var titleFormat:TextFormat = new TextFormat("_sans", 16, 0xFFFFFF);
			var titleField:TextField = new TextField();
			titleField.defaultTextFormat = titleFormat;
			titleField.text = "WebView Test";
			titleField.selectable = false;
			titleField.mouseEnabled = false;
			titleField.x = 12;
			titleField.y = (BAR_HEIGHT - 22) / 2;
			titleField.width = 200;
			titleField.height = 24;
			topBar.addChild(titleField);

			// close button
			closeBtn = new Sprite();
			closeBtn.graphics.beginFill(0xCC3333);
			closeBtn.graphics.drawRoundRect(0, 0, 80, 32, 6, 6);
			closeBtn.graphics.endFill();
			closeBtn.x = stage.stageWidth - 80 - 10;
			closeBtn.y = (BAR_HEIGHT - 32) / 2;
			closeBtn.buttonMode = true;
			closeBtn.useHandCursor = true;

			var btnFormat:TextFormat = new TextFormat("_sans", 14, 0xFFFFFF, true);
			btnFormat.align = TextFormatAlign.CENTER;
			var btnLabel:TextField = new TextField();
			btnLabel.defaultTextFormat = btnFormat;
			btnLabel.text = "Close";
			btnLabel.selectable = false;
			btnLabel.mouseEnabled = false;
			btnLabel.width = 80;
			btnLabel.height = 22;
			btnLabel.y = 6;
			closeBtn.addChild(btnLabel);

			closeBtn.addEventListener(MouseEvent.CLICK, onCloseClick);
			topBar.addChild(closeBtn);

			addChild(topBar);
		}

		private function onPlatformReady(e:NativeWebViewEvent):void {
			trace("[test] platform ready");
			createWebView();
		}

		private function createWebView():void {
			trace("[test] createWebView");

			var viewPort:Rectangle = new Rectangle(
					0,
					BAR_HEIGHT,
					stage.stageWidth,
					stage.stageHeight - BAR_HEIGHT
			);

			var options:WebViewOptions = new WebViewOptions();
			webView = NativeWebView.service.createWebView(viewPort, options, stage);

			webView.addEventListener(NativeWebViewEvent.COMPLETE, onWebViewComplete);
			webView.addEventListener(NativeWebViewEvent.LOCATION_CHANGE, onLocationChange);

			trace("[test] loading google.com");
			webView.loadURL("https://google.com");
		}

		private function onWebViewComplete(e:NativeWebViewEvent):void {
			trace("[test] webview loaded: " + e.data);
		}

		private function onLocationChange(e:NativeWebViewEvent):void {
			trace("[test] location changed: " + e.data);
		}

		private function onCloseClick(e:MouseEvent):void {
			trace("[test] close clicked");
			if (webView.visible) {
				webView.visible = false;
			}
			else {
				webView.visible = true;
			}
		}

		private function onResize(e:Event):void {
			// redraw top bar background
			topBar.graphics.clear();
			topBar.graphics.beginFill(0x333333);
			topBar.graphics.drawRect(0, 0, stage.stageWidth, BAR_HEIGHT);
			topBar.graphics.endFill();

			// reposition close button
			closeBtn.x = stage.stageWidth - 80 - 10;

			// resize webview viewport
			if (webView && webView.visible) {
				webView.viewPort = new Rectangle(
						0,
						BAR_HEIGHT,
						stage.stageWidth,
						stage.stageHeight - BAR_HEIGHT
				);
			}
		}
	}
}



//
//
//
///**
// *        __       __               __
// *   ____/ /_ ____/ /______ _ ___  / /_
// *  / __  / / ___/ __/ ___/ / __ `/ __/
// * / /_/ / (__  ) / / /  / / /_/ / /
// * \__,_/_/____/_/ /_/  /_/\__, /_/
// *                           / /
// *                           \/
// * https://distriqt.com
// *
// * This is a test application for the distriqt extension
// *
// * @author Michael Archbold & Shane Korin
// *
// */
//package
//{
//	import com.distriqt.test.nativewebview.Main;
//
//	import feathers.utils.ScreenDensityScaleFactorManager;
//
//	import flash.display.Sprite;
//	import flash.display.StageAlign;
//	import flash.display.StageScaleMode;
//	import flash.display3D.Context3DProfile;
//	import flash.display3D.Context3DRenderMode;
//	import flash.events.Event;
//	import flash.geom.Rectangle;
//
//	import starling.core.Starling;
//
//
//	/**
//	 * Sample application for using the NativeWebView Native Extension
//	 */
//	public class TestNativeWebView extends Sprite
//	{
//		////////////////////////////////////////////////////////
//		//	CONSTANTS
//		//
//
//
//		////////////////////////////////////////////////////////
//		//	VARIABLES
//		//
//
//		private var _starling:Starling;
//		private var _scaler:ScreenDensityScaleFactorManager;
//
//
//		////////////////////////////////////////////////////////
//		//	FUNCTIONALITY
//		//
//
//
//		/**
//		 *  Constructor
//		 */
//		public function TestNativeWebView()
//		{
//			super();
//			if(this.stage)
//			{
//				this.stage.align	 = StageAlign.TOP_LEFT;
//				this.stage.scaleMode = StageScaleMode.NO_SCALE;
//			}
//			this.mouseEnabled = this.mouseChildren = false;
//			this.loaderInfo.addEventListener(Event.COMPLETE, loaderInfo_completeHandler);
//		}
//
//
//
//		////////////////////////////////////////////////////////
//		//	INTERNALS
//		//
//
//
//
//
//		////////////////////////////////////////////////////////
//		//	EVENT HANDLERS
//		//
//
//		private function loaderInfo_completeHandler(event:Event):void
//		{
//			Starling.multitouchEnabled = true;
//			this._starling = new Starling( Main, this.stage, null, null, Context3DRenderMode.AUTO, Context3DProfile.BASELINE );
//			this._starling.enableErrorChecking = false;
//			this._starling.skipUnchangedFrames = false;
//			this._starling.supportHighResolutions = true;
//			this._starling.start();
//
//			this._scaler = new ScreenDensityScaleFactorManager(this._starling);
//
//			this.stage.addEventListener(Event.DEACTIVATE, stage_deactivateHandler, false, 0, true);
//		}
//
//
//		private function stage_deactivateHandler(event:Event):void
//		{
//			this._starling.stop(true);
//			this.stage.addEventListener(Event.ACTIVATE, stage_activateHandler, false, 0, true);
//		}
//
//
//		private function stage_activateHandler(event:Event):void
//		{
//			this.stage.removeEventListener(Event.ACTIVATE, stage_activateHandler);
//			this._starling.start();
//		}
//
//
//
//	}
//
//}
//
