/*******************************************************************************
 * Licensed Materials - Property of IBM
 * Mobile Browser Simulator
 * � Copyright IBM Corporation 2011. All Rights Reserved.
 *
 * U.S. Government Users Restricted Rights - Use, duplication or 
 * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *******************************************************************************/

/**
 * Widget representing a single mobile browser simulator
 */
define("ibm_mobile/QRCodeDialog",[
	"dojo",
	"dojo/dom-style",
 	"dijit",
 	"dijit/_Widget",
 	"dijit/_TemplatedMixin",
 	"dijit/_WidgetsInTemplateMixin",
 	"dojo/text!ibm_mobile/templates/QRCodeDialog.html",
    "dojo/_base/connect",
 	"ibm_mobile/QRCodeWidget",
 	"dijit/form/ComboBox",
 	"dijit/Tooltip",
 	"dijit/TitlePane",
 	"dojo/i18n"
], function(dojo, domStyle, dijit, _Widget, _TemplatedMixin, _WidgetsInTemplateMixin, QRCodeDialogTemplate, connect) {
	
	var QRCodeDialog = dojo.declare("ibm_mobile.QRCodeDialog",
			[dijit._Widget, dijit._TemplatedMixin, dijit._WidgetsInTemplateMixin ], {
	
		/** Used to debug this widget */
		__DEBUG_ENABLED : false,
		
		// WIDGET PARAMS
		templateString : QRCodeDialogTemplate,
		widgetsInTemplate : true,		
		deviceUrl : null,
		/** @type dijit/Tooltip */
		urlTooltip : null,
		
		// DOJO ATTACH POINTS
		/** DIV */
		_ipCombo : null,
		_qrCodeIPHostName : null,
		_qrCode : null,
		_autoUpdate : false,
				
		/**
		 * <p>Widget lifecycle function.</p>
		 * 
		 * @param params
		 */
		constructor : function(params) {
			this.__log('constructor', arguments);
		}, 
		
		/**
		 * <p>Widget lifecycle function.</p>
		 */
		postMixInProperties : function() {
			
		},
	
		/**
		 * <p>Widget lifecycle function.</p>
		 */
		postCreate : function() {
			this.__log('postCreate', arguments);
			
			connect.connect(this._ipCombo, 'onChange', dojo.hitch(this, function(value) {
				this.updateQRCode();
			}));
			
			connect.connect(this._ipCombo, 'onKeyUp', dojo.hitch(this, function(value) {
				this.updateQRCode();
			}));
			
			connect.connect(dijit.byId('_webpageBox'), 'onChange', dojo.hitch(this, function(value) {
				this.updateQRCode();
			}));
						
			var nls = dojo.i18n.getLocalization("ibm_mobile", "mobile");
			//create a tooltip on the qrcode
			new dijit.Tooltip({
				'connectId' : [this._qrCode.domNode],
				'label' : nls.QRCodeTooltip
			});

			//create a tooltip on the ipCombo
			new dijit.Tooltip({
				'connectId' : [this._ipCombo.domNode],
				'label' : nls.QRCodeIPTooltip
			});
			
			//create a tooltip on the ip label to show full url
			this.urlTooltip = new dijit.Tooltip({
				'connectId' : [this._qrCodeIPHostName.domNode],
				'label' : this.deviceUrl 
			});
			
			dojo.attr(this._qrCodeIPHostName, 'title', nls.QRCodeIPHostName);
		},
		
		/**
		 * <p>Widget lifecycle function.</p>
		 */
		uninitialize : function() {
			this.__log('uninitialize', arguments);
		},
				
				
		/**
		 * <p>Provide new IPs to fill the combo box.</p>
		 */
		setIPList : function(ipArray) {
			var ips = new dojo.data.ItemFileWriteStore({data: {identifier: 'name', items:[]}});
			var ln = ipArray.length;
			for (var i=0; i < ln; i++) {
				  ips.newItem({name: ipArray[i]});
			}
     		this._ipCombo.set('store', ips);
     		this._ipCombo.set('value', ipArray[0]);
     		this.updateQRCode();
		},
		
		/**
		 * <p>Update QRCode</p>
		 */
		updateQRCode : function() {

			if (this._autoUpdate == true) {
				var box = dijit.byId('_webpageBox');
				
				var protocol = "http:";
				
				var ip = this._ipCombo.get('value');
				// remove ip spaces
				ip = ip.replace(/\s+/g, '');
				
				// if we cannot find a port number when parsing the webpage box value, 
				// then we use the same as the MBS URL.
				var port = ":" + window.location.port;				
				
				// method to find the port number in the webpage box value.
				var isolatePortNumber = function(host) {
					var hostPortDelimiterIndex = host.indexOf(":");
					if (hostPortDelimiterIndex == -1)
						return "";
					else
						return host.substring(hostPortDelimiterIndex);
				};
								
				//url = webpageBoxValue = box.get('value');
				url = webpageBoxValue = this.deviceUrl;

				if (url.indexOf("http://") == 0) {
					url = url.substring(7);
					port = isolatePortNumber(url.substring(0, url.indexOf("/")));
					url = url.substring(url.indexOf("/"));
				} else if (url.indexOf("https://") == 0) {
					url = url.substring(8);
					port = isolatePortNumber(url.substring(0, url.indexOf("/")));
					url = url.substring(url.indexOf("/"));
					protocol = "https:";
				} else if (url.indexOf("../") == 0) {
					url = url.substring(2);
					url = url.substring(url.indexOf("/"));
				}

				var previewURL = "Please enter an IP/hostname.";
				// set QRCode URL
				if ((ip != null) && (ip !== "")){ 
					previewURL = protocol + "//" 
								+ ip
								+ port
								+ url;
				    this.urlTooltip.set("label", previewURL);
				}
				this._qrCode.set("text", previewURL);
			}
		},
		
		autoUpdate : function(bool) {
			this._autoUpdate = bool;
		},
		
		/**
		 * <p>Used to log debugging messages</p>
		 * 
		 * @param msg
		 * @param args
		 */
		__log : function(msg, args) {
			if(this.__DEBUG_ENABLED) {
				msg = "QRCodeDialog: " + msg;
				console.log(msg);
				if(args && args.length > 0) {
					console.log(args);
				}
			}
		}
	}); //end dojo.declare
	
	return QRCodeDialog;
});