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

var CalendarPrefsManager = Class.create ({
	
	initialize: function() {
		//Mojo.Log.info("calendar-prefs-manager initialize");
		this.observers = new Hash();
		this.getCalendarPrefs();
	},
	
	getCalendarPrefs: function(){
		// Subscribe to the getCalendarPrefs service
		this.request = new Mojo.Service.Request(CalendarMojoService.identifier, {
			method: 'getCalendarPrefs',
			parameters: {subscribe: true},
			onSuccess: this.getCalendarPrefsCallback.bind(this),
			onFailure: this.getCalendarPrefsFailed			
		});
	},
	
	cleanup: function() {
		// Unsubscribe from the getCalendars service
		this.request.cancel();
	},
	
	getCalendarPrefsFailed: function(response){
		console.error("PrefsManager: getCalendarsPrefs service call failed: "+Object.toJSON(response));
	},
	
	getCalendarPrefsCallback: function(response){
		//Mojo.Log.info('!!!!!!! calendar-prefs-manager: getCalendarPrefsCallback %j',response);
 		this.prefs = response;
		
		// Update the observers to the prefs
		this.notifyObservers();
	},
	
	getPrefs: function() {
		return this.prefs;
	},
	
	// *** DEFAULT CALENDAR ***
	setCalendarDefaultCalIdCallback: function(response) {
		//Mojo.Log.info("calendar-prefs-manager: setCalendarDefaultCalIdCallback");
	},
	
	validateDefaultCalPref: function() {
		//Mojo.Log.info("calendar-prefs-manager: validateDefaultCalPref");
		var updatePref = false;
		if (this.prefs.defaultCalId != 0) {
			if (getCalendarsManager().getCalName("id"+this.prefs.defaultCalId) == undefined) {
				// The calendar the user chose to be the default is no longer there
				//Mojo.Log.info("validateDefaultCalPref: defaultCalId no longer there");
				this.prefs.defaultCalId = 0;
				updatePref = true;
			}
		}
		
		if (this.prefs.autoDefaultCalId != 0) {
			if (getCalendarsManager().getCalName("id"+this.prefs.autoDefaultCalId) == undefined) {
				//Mojo.Log.info("calendar-prefs-manager: validateDefaultCalPref: autoDefaultCalId is no longer available");
				// The calendar the app chose to be the default is no longer there
				this.prefs.autoDefaultCalId = 0;
				updatePref = true;
			}
		}
		
		if (updatePref) {
			// Save the new preferences if anything was updated.
			getCalendarService().setCalendarPrefs(this.prefs, this.setCalendarDefaultCalIdCallback.bind(this));
		}
	},
	
	getDefaultCalendar: function() {
		//Mojo.Log.info("calendar-prefs-manager: getDefaultCalendar %s,auto:%s", this.prefs.defaultCalId, this.prefs.autoDefaultCalId);
		if (this.prefs.defaultCalId == 0) {
			// The user hasn't manually set the default calendar, so let's use
			// some smarts to figure this one out
			if (this.prefs.autoDefaultCalId == 0) {
	  		// If there is a calendar account other than Local, let's use that one
				var nonLocalCalId = getCalendarsManager().getNonLocalCalendarId(true /*excludeReadOnly*/);
				if (nonLocalCalId != 0) {
					//Mojo.Log.info("Found a non-local calendar");
					// Save the setting so the same calendar is returned as default next time
					this.prefs.autoDefaultCalId = nonLocalCalId;
					// Save the prefs so the autoDefaultCalId is persisted
					getCalendarService().setCalendarPrefs(this.prefs, this.setCalendarDefaultCalIdCallback.bind(this));
				} else {
					//Mojo.Log.info("Could not find a non-local calendar");
					// If there is no non-local account we return the local calendar, but
					// DO NOT save it in the prefs
					return getCalendarsManager().getLocalCalendarId();
				}
				
				
			}
			return this.prefs.autoDefaultCalId;
		} else {
			return this.prefs.defaultCalId;
		}
	},
	
	// OBSERVERS
	
	observeCalendarPrefs: function(sceneName, sceneController) {
//		Mojo.Log.info('calendar-prefs-manager: observeCalendarPrefs ');
		this.observers.set(sceneName, sceneController);
	},
	
	stopObservingCalendarPrefs: function(sceneName) {
//		Mojo.Log.info('calendar-prefs-manager: stopObservingCalendarPrefs ');
		this.observers.unset(sceneName);
	},
	
	notifyObservers: function() {
//		Mojo.Log.info('calendar-prefs-manager: notifyObservers ');
		this.observers.each(function(elementPair) {
			var sceneName = elementPair.key;
			var sceneController = elementPair.value;
//			Mojo.Log.info('calendar-prefs-manager: updating scene %s',sceneName);
			if (sceneController != null) 
				sceneController.calendarPrefsUpdated();
		});
	}
	
});
