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

var SyncDataAssistant = Class.create({
	
	initialize: function(accountsList, count, userName) {
		this.accountsList = accountsList;
		this.count = count;
		this.userName = userName;
		
		this.accountsAttributes ={
			itemTemplate : "sync-pim/existingAccount"
		}
		this.accountsModel = {
			items : []
		}
	},
	
	setup: function() {
		
		this.appMenuModel = { 
			items: [
				Mojo.Menu.editItem,
                {label: $L('Cancel Transfer'), command: 'cancelImport'},
                {label: $L('Help'), command: 'help'}
			],
		};
		
		this.controller.setupWidget(Mojo.Menu.appMenu, {omitDefaultItems: true}, this.appMenuModel);
		
		this.controller.setupWidget("ExistingAccountTypes", this.accountsAttributes, this.accountsModel);
		this.controller.get('addAccount').observe(Mojo.Event.tap, this.addAccount.bindAsEventListener(this));
		
		Mojo.Log.info("Is count greater than 0? "+this.count);
		if(this.count > 0) {
			this.accountsModel.items = this.accountsList;
			//this.controller.modelChanged(this.accountsModel);
		}
		this.accountsModel.items.push({"accountDisplayName": $L("Palm Profile"), "domain": "local", "login": this.userName});
		
		this.confirmMethod = this.confirmSyncData.bind(this);
		this.controller.get("ExistingAccountTypes").observe(Mojo.Event.listTap, this.confirmMethod);
	},
	
	ready: function() {
		// If there are any possible PIM accounts, we return immediately
		for (var key in DataImportUtil.getPossiblePIMAccounts()) {
			return;
		}
		// Otherwise hide the create account button
		this.controller.get('addAccount').hide();
	},
	
	confirmSyncData: function(e) {
		Mojo.Log.info("------------- confirm sync data ---------------------");
		Mojo.Controller.stageController.pushScene('confirm-sync-data', e.item.domain, e.item.login, e.item.accountId);
	},
	
	handleCommand: function(event) {	
		if (event.type == Mojo.Event.command && event.command == 'help') {
			Mojo.Log.info("------------------ Launch Help -------------------------");
			AppAssistant.dataImportService.launchHelp();
		}
		else if (event.type == Mojo.Event.command && event.command == 'cancelImport') {
			DataImportUtil.cancelImport(this.controller);
		}	
	},
	
	addAccount: function (event) {
		Mojo.log("Push add contact scene");
		var scenePushArguments = {
			appId:"com.palm.app.contacts",
			name:"choosetransport",
		}
		Mojo.Controller.stageController.pushScene(scenePushArguments, null, null, null, null, true);
	},
	
	importData: function(event) {
		Mojo.Controller.stageController.pushScene("done-import", 'local');
	}
});	