/* Copyright 2009 Palm, Inc.  All rights reserved. */

var RoamGuard = Class.create ({
	
	params: undefined,
	
	sceneController: undefined,
	
	/**
	 * Shows up pop-up dialog to turn off roamguard
	 * @param {Object} params
	 * params.sceneController: scene controller
	 * params.method: the method to be invoked. The method should have been bound to the invoking sceneassistant
	 * Sample usage:
	 * if(RoamUtil.enableRoaming() === true) {
			RoamGuardDialog.turnOffRoamguard({
				"sceneController": this.controller,
				"method": 'methodName'
			});
			return;
		}
	 */
	turnOffRoamguard: function(params) { 
		if(!(params.sceneController)) {
			return;
		}
		RoamGuard.params = params;
		this.sceneController = params.sceneController;
				
		var infoMessage = $L("You are currently roaming. In order to continue with setup, you need to enable data roaming. Roaming charges may apply. Would you like to enable data roaming and continue setup?"); 	
		
		var that = this;
		var dialog = this.sceneController.showAlertDialog({
		    onChoose: function(value) {
				if (value === "allow") {
					that.disableRoamguard();
				} else if (value === "ignore") {
					Mojo.Controller.stageController.pushScene("no-network", RoamGuard.params);
				}
			},
			title: $L("Roaming"),
		    message: infoMessage,
		    choices:[
		        {label:$L("Yes, allow roaming"), value:"allow", type: 'affirmative'},
				{label:$L("No, setup later"), value:"ignore", type:'dismiss'}
		    ]
		  });
		dialog.observe(Mojo.Event.keydown, FirstUseUtil.handleKeyPressEvent.bind(this));
    },
	
	showErrorDialog: function() {
		
		if(!this.sceneController) {
			return;
		}
		
		var infoMessage = $L("Unable to continue setup because roam guard could not be turned off."); 	
		var that = this;
		var dialog = this.sceneController.showAlertDialog({
		    onChoose: function(value) {
				if(value === "retry") {
					that.disableRoamguard().bind(that);
				}
			},
			title: $L("Roam Guard"),
		    message: infoMessage,
		    choices:[
		        {label:$L("Retry"), value:"retry", type: 'affirmative'},
				{label:$L("Cancel"), value:"ignore", type:'dismiss'}
		    ]
		  });
		dialog.observe(Mojo.Event.keydown, FirstUseUtil.handleKeyPressEvent.bind(this));
    },
	
	disableRoamguard: function(value) {
		Mojo.Log.info("---------------- disableRoamguard -------------------");	
		this.roamRequest = AppAssistant.accountService.setWanConfig("disable",
								this.readRoamGuardStatus.bind(this), this.errorRoamGuardStatus.bind(this));
	},
	
	readRoamGuardStatus: function(response) {
		Mojo.Log.info("---------------- readRoamGuardStatus -------------------- %j", response);
		if(this.roamRequest) {
			this.roamRequest.cancel();
		}
		
		if(response && response.returnValue == true) {
			RoamUtil.isRoamGaurdEnabled = false;
			RoamUtil.processRoamGuardParams(RoamGuard.params);
			// Continue when connection comes back up
			/*this.statusRequest = AppAssistant.accountService.getConnectionStatus(this.readConnectionStatus.bind(this), 
							this.errorConnectionStatus.bind(this));*/
			
		} else {
			this.showErrorDialog();
		}
	}, 
	
	readConnectionStatus: function (response) {
		if(response && response.isInternetConnectionAvailable == true) {
			if(this.statusRequest) {
				this.statusRequest.cancel();
			}
			RoamUtil.processRoamGuardParams(RoamGuard.params);
		}
	},
	
	errorConnectionStatus: function (response) {
		Mojo.Log.error("-------------------errorConnectionStatus -----------------------%j ", response);
		if(this.statusRequest) {
			this.statusRequest.cancel();
		}
		// Show error screen right here
		RoamUtil.processRoamGuardParams(RoamGuard.params);
	},
	
	errorRoamGuardStatus: function (response) {
		Mojo.Log.error("---------------- errorRoamGuardStatus -------------------- %j", response);

		if(this.roamRequest) {
			this.roamRequest.cancel();
		}
		this.showErrorDialog();
		
	},
	
});

var RoamGuardDialog = new RoamGuard();