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

FirstUseAssistant = Class.create({
	initialize: function(params) {
		if (params) {
			this.deferredComposeParams = [params];			
		}
		Mojo.Log.info("\nEMAIL FIRST USE %j\n", params);
	},
	
	setup: function() {
		this.accountsErrorCount = 0;
		this.emailAddress = "";
		this.serviceRequest = EmailAccount.getFirstUseEmailAddress(this.firstUseEmailAddressCallback.bind(this));
	},
	
	cleanup: function() {
		this.serviceRequest.cancel();
		this.closeScene();		
	},
	
	activate: function() {
		// Clear the notification filters so user will get notified when any email comes in
		var notificationAssistant = Mojo.Controller.getAppController().assistant.notificationAssistant;
		notificationAssistant.setFilter();
	},

	closeScene: function() {
		var appAssistant;
		function doOneLaunch (oneLaunchParams) {
			var launchHandler = new LaunchHandler(appAssistant, oneLaunchParams);
			launchHandler.handleLaunch();
		}

		if (this.model.accounts.length > 0) {
			if (this.deferredComposeParams) {
				appAssistant = Mojo.Controller.appController.assistant;
				this.deferredComposeParams.each(doOneLaunch);
			}
		} else {
			Mojo.Controller.getAppController().closeStage("email");
		}
	},
	
	handleCompose: function(composeParams) {
		if (this.deferredComposeParams === undefined) {
			this.deferredComposeParams = [];
		}
		this.deferredComposeParams.push(composeParams);
	},

	firstUseEmailAddressCallback: function(resp) {
		// Use the email address entered during first use to prepolulate the email address field
		// when setting up the first email account
		if (resp && resp.accountAlias)
			this.emailAddress = resp.accountAlias;
		
		// Get the list of email accounts now
		this.serviceRequest = EmailAccount.subscribeAccounts(this.accountsCallback.bind(this), this.error.bind(this));
	},
	
	accountsCallback : function(resp) {
		if (resp.list.length === 0) {
			if (this.controller.stageController.topScene().sceneName !== 'wizard') {
				this.controller.stageController.pushScene('wizard', {type: "email", emailAddress:this.emailAddress});
			}
		}
		this.model.accounts = this.formulateToModelAccounts(resp.list);
		this.controller.modelChanged(this.model);
	},
	
	formulateToModelAccounts: function(list) {
		var accounts = [];
		Mojo.Log.info("\n account list=%j\n", list);
		list.each(function(acct) {
			var modelAcct = {
				name: acct.originalLogin,
				iconClass: "firstuse account-icon #{accountDomain} icon left".interpolate(acct)
			};
			accounts.push(modelAcct);
		});
		
		return accounts;
	},
	
	addAccount: function() {
		this.controller.stageController.pushScene('wizard', {type: "email"});
	},
	
	done: function() {
		EmailAppDepot.depot.setFirstUseComplete();
		this.controller.stageController.popScenesTo();
		this.controller.stageController.pushScene("accounts");
	},
	
	error: function(err) {
		// Try relaunching the mail service 3 times before giving up
		this.accountsErrorCount++;
		if (this.accountsErrorCount < 3) {
			Mojo.Log.warn("FirstUse accountsErrorCount=" + this.accountsErrorCount + " detail=" + $H(err).inspect());
			this.serviceRequest.cancel();
			this.serviceRequest = EmailAccount.subscribeAccounts(this.accountsCallback.bind(this), this.error.bind(this));
		} else {
			Mojo.Log.error("This Is Bad: FirstUse cannot maintain accountList subscription " + $H(err).inspect());
		}
	}
});