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

NoNetworkAssistant = Class.create({
	initialize: function(params) {
		this.params = params;
		FirstUseUtil.addDashboardSmall($('target'));
	},
	
	activate: function () {
		
	},
		
	setup: function() {
		this.controller.listen(this.controller.get('connectionErrorButton'), Mojo.Event.tap, this.tryAgain.bindAsEventListener(this));
		this.controller.listen(this.controller.get('roamTryAgainButton'), Mojo.Event.tap, this.disableRoamguard.bindAsEventListener(this));
		this.controller.listen(this.controller.get('cancel'), Mojo.Event.tap, this.goBack.bindAsEventListener(this));
		
		// Listen to ## key presses
		this.controller.listen(this.controller.sceneElement, Mojo.Event.keydown, FirstUseUtil.handleKeyPressEvent.bind(this));
		$('target').addEventListener(Mojo.Event.tap, FirstUseUtil.tapHandler.bindAsEventListener(this));
				
		this.showMessage();
	},	
	
	showMessage: function () {
		if (Account.action == "create") {
			this.controller.get('account-creation-roaming-error').style.display = 'block';
		}
		else if (Account.action == "login") {
			this.controller.get('login-roaming-error').style.display = 'block';	
		}	
		else if (Account.action == "restore") {
			Account.action = "login";
			this.controller.get('backup-error').style.display = 'block';	
		}
	},
		
	handleCommand: function(event) {
		if (event.type == Mojo.Event.back) {
			event.preventDefault();
			event.stopPropagation();
		}	
	},
	
	tryAgain: function (event) {
		this.params.popCurrentScene = true;
		RoamUtil.processRoamGuardParams(this.params);
	},
	
	disableRoamguard: function (event) {
		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(response && response.returnValue == true) {
			this.params.popCurrentScene = true;
			RoamUtil.processRoamGuardParams(this.params);
		} else {
			this.showErrorDialog();
		}
	}, 
	
	errorRoamGuardStatus: function (response) {
		Mojo.Log.error("---------------- errorRoamGuardStatus -------------------- %j", response);
		this.showErrorDialog();
	},
	
	showErrorDialog: function() {
		
		var infoMessage = $L("Unable to continue setup because roam guard could not be turned off."); 	
		var that = this;
		var dialog = this.controller.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));
    },
	
	goBack: function(event) {
		Mojo.Controller.stageController.popScene();
	},
		
	deactivate: function() {
		
	}	
});
