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

var PrefsFirstLaunchAssistant = Class.create({
	initialize: function(params){
        this.appController = Mojo.Controller.getAppController();
        this.Messaging = this.appController.Messaging;
		this.updateModelBound = this.updateModel.bind(this);
	},

	setup: function() {
		this.Messaging.IMAccounts.observe(this.updateModelBound);
	},

	cleanup: function() {
		this.Messaging.IMAccounts.stopObserving(this.updateModelBound);
	},

	updateModel: function(data) {
		var accountsArray = [{
			name: $L("SMS Account"),
			type: 'sms',
			divider: $L("Text messaging")
		}];
		
		data.list.each(function(account) {
			var a = {
				name: account.username,
				type: account.domain
			};
			accountsArray.push(a);
		});

		if (this.model) {
			this.model.accounts = accountsArray;
			this.controller.modelChanged(this.model);
		}
		
		return accountsArray;
	},

	// Add Account button tapped
	addAccount: function(event) {
		this.controller.stageController.pushScene('prefsAccountTypeList', {popScenesTo:"firstLaunchScene"});
	},

	// Done button tapped
	done: function() {
    	// Notify the service that first launch is complete
        MessagingMojoService.setFirstLaunchToFalse(this.controller,function() {});
		this.Messaging.messagingPrefs.firstUseMode = false;
		
		// If there is more than 1 IM account, make sure that the buddy list is shown after first launch
		// This is needed when an IM account gets created from another app such as email.  If the account
		// is created in our app, then we already do this the moment that it is created.
		var forceListView = this.Messaging.Views.HISTORY;
		if (this.Messaging.IMAccounts.data.list && this.Messaging.IMAccounts.data.list.length > 0) {
			this.Messaging.setListViewVisible(this.Messaging.Views.BUDDY);
			forceListView = this.appController.Messaging.Views.BUDDY;
		}
		
        this.controller.stageController.swapScene({
            name: 'listview',
			transition:Mojo.Transition.crossFade,
            id: CONSTANTS.SCENE_ID_LISTVIEW // we specify a sceen ID because we need to popScenesTo() later
          },{forceListView: forceListView});
	}
});
