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

var EmailAppDepot = Class.create({
	initialize: function() {
		this.depot = new Mojo.Depot({ name: "email_accounts", version: 1 },
		                            this._depotReady.bind(this));

		// Since number of accounts and details about them are used a lot, keep them in memory
		// instead of each scene having to request them.
		this.accounts = {};
		this.subscriptionFailureCount = 0;
		this.acctSubscription = EmailAccount.subscribeAccounts(this._updateAccountsInfo.bind(this), this._accountsErrorHandler.bind(this));
	},

	isFirstUseComplete: function() {
		return this.firstUseComplete;
	},

	setFirstUseComplete: function() {
		this.firstUseComplete = true;
		this.depot.simpleAdd('firstUseComplete', true);
	},

	isShowFavorites: function(accountId, callback) {
		Mojo.Log.info("isShowFavorites id="+accountId)
		this.depot.simpleGet(accountId, callback);
	},

	setShowFavorites: function(accountId, obj) {
		Mojo.Log.info("setShowFavorites id="+accountId+", "+$H(obj).inspect())
		this.depot.simpleAdd(accountId, obj);
	},
	
	getDefaultSignature: function(callback) {
		if (this.defaultSignature) {
			callback(this.defaultSignature);
		}
		else {
			this.depot.simpleGet('defaultSignature', callback);
		}
	},
	
	setDefaultSignature: function(signature) {
		this.defaultSignature = signature;
		this.depot.simpleAdd('defaultSignature', this.defaultSignature);
	},

	/*
	 * Load up settings that the email app will need often or want to a quick response.
	 */
	// internal use only
	_depotReady: function() {
		this.depot.simpleGet('firstUseComplete', function(firstUseComplete) {
					Mojo.Log.info("firstUseComplete = "+firstUseComplete);
					this.firstUseComplete = firstUseComplete;		
				}.bind(this));
		this.depot.simpleGet('defaultSignature', function(signature) {
					this.defaultSignature = signature;		
				}.bind(this));
	},

	// internal use only
	_updateAccountsInfo: function(accounts) {
		if (this.accounts.list && accounts.list && this.numAccounts !== accounts.list.length) {
			// look for accounts that have been removed
			var removedAccounts = [];
			this.accounts.list.each(function(existingAcct){
				var stillExists = false;
				accounts.list.each(function(newAcct) {
					if (existingAcct.originalLogin === newAcct.originalLogin) {
						stillExists = true;
					}
				});
				
				if (stillExists === false) {
					removedAccounts.push(existingAccnt);
				}
			});
			if (removedAccounts.length > 0) {
				// delete the security policy that are associated with the deleted
				// accounts. 
				SecuritypolicyAssistant.removeSecurityPolicies(removedAccounts);
			}
		}
			
		this.accounts = accounts;
		this.numAccounts = accounts.list.length;
	},

	// internal use only
	_accountsErrorHandler: function() {
		if (this.retryTimeout) {
			clearTimeout(this.retryTimeout);
		}

		// Try relaunching the mail service 3 times before giving up
		this.subscriptionFailureCount++;
		if (this.subscriptionFailureCount < 3) {
			this.acctSubscription.cancel();
			this.acctSubscription = EmailAccount.subscribeAccounts(this._updateAccountsInfo.bind(this), this._accountsErrorHandler.bind(this));
		} else {
			var delayedRetryFunction = function() {
				this.retryTimeout = undefined;
				this.subscriptionFailureCount = 0;
				this._accountsErrorHandler();
			}.bind(this);
			this.retryTimeout = delayedRetryFunction.delay(180); // retry in 3 minutes
		}
	}

});

EmailAppDepot.depot = null;
EmailAppDepot.getDepot = function() {
	if (!EmailAppDepot.depot) {
		EmailAppDepot.depot = new EmailAppDepot();
	}
	return EmailAppDepot.depot;
};


