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

var StatusAssistant = Class.create({
	spinner: 'activity-spinner',
	spinning: false,
	spinnerAttrs: {
		spinnerSize: Mojo.Widget.spinnerLarge
	},
	
	initialize: function(){
		FirstUseUtil.addDashboardSmall($('target'));
		Mojo.Log.info("Account.action === %s", Account.action);
		$('acctDoneButton').observe(Mojo.Event.tap, this.doneFirstUse.bindAsEventListener(this));
		$('loginDoneButton').observe(Mojo.Event.tap, this.notifyProvisioning.bindAsEventListener(this));
		$('acctBackToIntroButton').observe(Mojo.Event.tap, this.goBackToIntro.bindAsEventListener(this));
		$('loginBackToIntroButton').observe(Mojo.Event.tap, this.goBackToIntro.bindAsEventListener(this));
		$('acctConnectBackToIntroButton').observe(Mojo.Event.tap, this.goBackToIntro.bindAsEventListener(this));
		$('loginConnectBackToIntroButton').observe(Mojo.Event.tap, this.goBackToIntro.bindAsEventListener(this));
	},
		
	setup: function(){
		
		$('target').addEventListener(Mojo.Event.tap, FirstUseUtil.tapHandler.bindAsEventListener(this));
		this.controller.setupWidget(this.spinner, this.spinnerAttrs, {});		
		if(Account.action == "create") {
			$('account-status').show();
			saveCurrentState("status");
			if(displayRequest) {
				displayRequest.cancel();
			}
		} else if (Account.action == "login") {
			$('login-status').show();
			saveCurrentState("status");
			if(displayRequest) {
				displayRequest.cancel();
			}
			
			this.postRequest = AppAssistant.accountService.postLoginSettings(
								this.readPostLoginSettings.bind(this), 
								this.errorPostLoginSettings.bind(this), 
								this.controller);
								
		} else if (Account.action == "account-creation-error") {
			$('account-creation-error').show();
			saveCurrentState("intro");
		} else if (Account.action == "login-error") {
			$('login-error').show();
			saveCurrentState("intro");
		} else if (Account.action == "version-error") {
			$('version-error').show();
			$("pinned-button-bottom-fade-div").hide();
			$("mojo-scene-status").removeClassName('has-pinned-button');
			saveCurrentState("intro");
		} else if (Account.action == "login-connection-error") {
			$('login-connection-error').show();
			saveCurrentState("intro");
		} else if (Account.action == "account-creation-connection-error") {
			$('account-creation-connection-error').show();
			saveCurrentState("intro");
		}	
				
		// Listen to ## key presses
		this.controller.listen(this.controller.sceneElement, Mojo.Event.keydown, FirstUseUtil.handleKeyPressEvent.bind(this));
	},
	
	readPostLoginSettings: function (response) {
		if(response.returnValue && response.returnValue == true) {
			if(this.postRequest) {
				this.postRequest.cancel();
			}
		}
	},
	
	errorPostLoginSettings: function (response) {
		Mojo.Log.error("----------------- Could not do post login settings ------------------%o", $H(response));
	},
	
	handleCommand: function(event) {
		if (event.type == Mojo.Event.back) {
			event.preventDefault();
			event.stopPropagation();
		}	
	},	
	
	doneFirstUse: function(event){
		//Set locale in the end
		/*AppAssistant.accountService.launchTutorial(
							Account.language, Account.country); */	
		//this.setLocale();
		$('acctDoneButton').enabled = false;
		this.controller.get('acctDoneButton').toggleClassName('disabled');
		if (FirstUseUtil.isSimulator()) {
			// It is either emulator or simulator
			this.controller.window.close();
			return;
		}
		Mojo.Controller.stageController.pushScene("location-intro");
	},
	
	goBackToIntro: function(event) {
		if(Account.isInvalidToken == true) {
			Mojo.Controller.stageController.pushScene('invalid-token');
			return;
		}
		Mojo.Controller.stageController.pushScene("intro");
	},
	
	notifyProvisioning: function() {
		this.showScrim();
		this.audioRequest = AppAssistant.accountService.resetDefaultVolume(this.controller);
		if ("cdma" == Account.platformType.toLowerCase()) {
			this.notifyRequest = AppAssistant.accountService.notifyProvisioning(this.readNotificationStatus.bind(this), this.controller);
		} else {
			this.saveDoneState();
		}						
	},
	
	readNotificationStatus: function (response) {
		if(response.errorCode) {
			Mojo.Log.error("--------- Could not notify provisioning to process NIDC ---------", Object.toJSON(response));
		} else {
			Mojo.Log.info("--------- Notified provisioning to process NIDC ---------", Object.toJSON(response));
		}
		this.saveDoneState();
	},
	
	saveDoneState: function () {
		this.tokenState = Account.isInvalidToken;
				
		$('loginDoneButton').enabled = false;
		this.controller.get('loginDoneButton').toggleClassName('disabled');
		FirstUseUtil.saveDoneState();
		
	},
	
	hideScrim: function() {
	    this.spinning = false;
		$(this.spinner).mojo.stop();
		this.controller.get('scrim').hide();
               
    },
	
	showScrim: function() {
		this.controller.get('scrim').show();
		this.spinning = true;
		$(this.spinner).mojo.start();
	},
	
	deactivate: function () {
		this.hideScrim();
	}
});	
