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

var FirstUseManager = Class.create ({
	
	getAllAccountTypesForServiceCallback: function(response) {
		Mojo.Log.info("firstuse-manager: getAllAccountTypesForServiceCallback");
		this.calAcctTypes = response.list;
		// Set up the account type icons
		this.calAcctTypes.each(function(acctType){
			// NOTE: Account names to be localized are located in javascripts/accountLocalizations.js
			acctType.name = $L(acctType.name);
			if (acctType.icons)
				acctType.serviceIcon = getCalendarsManager().parseAcctIconString(acctType.icons);
		});
		if (this.pendingShowFirstUseScene) {
			this.pushFirstUseScene();
		}
	},
	
	getCalendarAccountsCallback: function(response) {
		Mojo.Log.info("firstuse-manager: getCalendarAccountsCallback");
		this.calAccts = response.list;
		// Per HI, for EAS accounts show DOMAIN (EMAIL ADDRESS)
		this.calAccts.each(function(acct){
			acct.loginDisplayName = acct.login;
			if (acct.domain == 'eas') {
				acct.accountDisplayName =$L("Microsoft Exchange");
				if (acct.originalLogin) {
					acct.loginDisplayName = acct.originalLogin;
				}
	  	}
			if (acct.accountSpecificIcons)
				acct.serviceIcon = getCalendarsManager().parseAcctIconString(acct.accountSpecificIcons);
		});
		if (this.pendingShowFirstUseScene) {
			this.pushFirstUseScene();
		}
		this.notifyCalendarAcctObservers();
	},
	
	initialize: function(stageController) {
		this.stageController = stageController;
		
		// Set up observers
		this.acctsObservers = new Hash();
		this.acctTypeObservers = new Hash();
		
		// Determine whether we have any active calendar accounts
		this.requests = [];
		this.requests.push(getAccountsService().getAllAccountsForCalendarService(
			this.getCalendarAccountsCallback.bind(this)));
		this.requests.push(getAccountsService().getAllAccountTypesForService (
			this.getAllAccountTypesForServiceCallback.bind(this)));
			
		this.pushFirstUseScene();
	},
	
	setFirstUseToFalseCallback: function(response) {
		//Mojo.Log.info("firstuse-manager: setFirstUseToFalseCallback");
	},
	
	cleanup: function() {
		// Cancel all requests
		this.requests.each(function(request){
			request.cancel();
		});
		
		// Set First Use to false so that the First Use panels aren't shown again
		setFirstUseToFalse();
	},
	
	getCalendarAccts: function() {
		return this.calAccts;
	},
	
	getCalendarAcctTypes: function() {
		return this.calAcctTypes;
	},
	
	getAccountTypeAttribute:function(){
		this.acctTypes= {};
		for(i=0;i<this.calAcctTypes.size();i++)
		{
			var name = this.calAcctTypes[i].name;
			this.acctTypes[name]={
				iconClass:this.calAcctTypes[i].iconClass,
				title:this.calAcctTypes[i].name
			}
			
		}
		return this.acctTypes;
	},
	getAccountListModel:function(){
		var acctList=[];
		for(i=0;i<this.calAccts.size();i++){
			if (this.acctTypes[this.calAccts[i].accountDisplayName]) {
				acctList.push({
					type: this.calAccts[i].accountDisplayName,
					name: this.calAccts[i].loginDisplayName
				});
			}
			else {
				acctList.push({
					iconPath: this.calAccts[i].serviceIcon,
					name: this.calAccts[i].loginDisplayName
				})
			}
		}
		return acctList;
	},
	
	pushFirstUseScene: function() {
		if (this.calAccts == undefined || this.calAcctTypes == undefined) {
			// Initialization hasn't been completed yet
			this.pendingShowFirstUseScene = true;
			return;
		}
		
		this.pendingShowFirstUseScene = false;
		/*if (this.calAccts.size() > 0) {
			this.stageController.pushScene('firstuse-with-accts');
		} else {
			this.stageController.pushScene('firstuse', true );
		}*/
		var acctTypes= this.getAccountTypeAttribute();	
		
		var acctList=this.getAccountListModel();
		
		
		this.stageController.showFirstLaunchScene({
				sceneName: 'FirstLaunchScene',
				iconPath:'icon.png',
				title:$L('Your Calendar accounts'),
				assistant: new FirstUseAssistant('FirstLaunchScene'),
				//dividerFunction: this.divider.bind(this),
				accountTypes: acctTypes
				}, {
					accounts: acctList
				});
	},
	/*divider:function(itemModel) {
		var division = itemModel.type;
		return division;
	},*/
	
	// OBSERVERS
	observeCalendarAccts: function(sceneName, sceneAssistant) {
		this.acctsObservers.set(sceneName, sceneAssistant);
	},
	
	stopObservingCalendarAccts: function(sceneName) {
		this.acctsObservers.unset(sceneName);
	},
	
	notifyCalendarAcctObservers: function() {
		//Mojo.Log.info('calendars-manager: notifyObservers ');
		this.acctsObservers.each(function(elementPair) {
			var sceneController = elementPair.value;
			sceneController.calendarAcctsUpdated();
			});
	}
});
