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

var Util = Class.create ({
	
	_pimFilters:
	{
		totalContacts: { eas:null, gmail: null },
		totalEvents: { eas: null, gmail: null },
		totalMemos: { },
		totalTasks: { eas: null }
	},
	
	getLocalAccount: function () {
		this.previousCount = -1;
		this.localRequest = AppAssistant.dataImportService.getLocalAccount(
				this.successLocalAccount.bind(this), 
				this.errorLocalAccount.bind(this));
	},
	
	successLocalAccount: function (response) {
		DataImportUtil.userName = "";
		
		if(response.userName) {
			this.userName = response.userName;
		}
		this.getTransferredPIMTypes();
	},
	
	errorLocalAccount: function (response) {
		Mojo.Log.error("------- Could not get local account name ----------%j", response);
		DataImportUtil.userName = "";
		this.getTransferredPIMTypes();
	},
	
	getAllAccountsOfUser: function() {
		this.acctRequest = AppAssistant.dataImportService.getAllAccountsOfUser(
				this.successAccounts.bind(this), 
				this.errorAccounts.bind(this));
	},
	
	successAccounts: function(response) {
		Mojo.Log.info("Success in getting accounts: %j", response);
		if (response.list) {
			Mojo.Log.info("---------------- this.previousCount != response.count ------------------"+ this.previousCount +", " +response.count);
			if (this.previousCount != response.count) {
				var accounts = this.filterAccounts(response);
				this.previousCount = response.count;
				Mojo.Log.info("-------------- New value of previousCount ------------------" + this.previousCount);
				Mojo.Controller.stageController.popScenesTo();
				Mojo.Controller.stageController.pushScene('sync-data', response.list, response.list.length, DataImportUtil.userName);
			}
		}
		else {
			Mojo.Log.info("---------------- response has no list ------------------");
			Mojo.Controller.stageController.popScenesTo();
			Mojo.Controller.stageController.pushScene('sync-data', [], 0, this.userName);
		}	
	},
	
	errorAccounts: function(response) {
		Mojo.Log.error("Error in getting accounts: "+$H(response).inspect());
		Mojo.Controller.stageController.popScenesTo();
		Mojo.Controller.stageController.pushScene('sync-data', [], 0, DataImportUtil.userName);
	},
	
	filterAccounts: function (response) {
		var results = this.getPossiblePIMAccounts();
		// Remove any accounts from the account list if we do not have any pim content for that
		// account type.
		var accounts = response.list;
		for (var i = 0; i < accounts.length;) {
			var type = accounts[i].domain;
			if (results[type] !== null) {
				accounts.splice(i, 1);
			}
			else {
				i++;
			}
		}
	},
	
	getPossiblePIMAccounts: function() {
		// Create a union of all possible account types
		var results = {};
		for (var pim in this.pimTypes) {
			if (this.pimTypes[pim] > 0) {
				Object.extend(results, this._pimFilters[pim]);
			}
		}
		return results;
	},
	
	cancelImport: function (sceneController) {
		this.controller = sceneController;
		var infoMessage = $L("Are you sure you want to cancel the transfer and delete the transferred data?");
		var dialog = sceneController.showAlertDialog({
				title: $L("Cancel Transfer"),
			    onChoose: function(value){
					if(value == "cancel") {
						AppAssistant.dataImportService.deleteData(DataImportUtil.successDeleteData,
																  DataImportUtil.errorDeleteData);
					} 
				}, 
			    message: infoMessage,
			    choices:[
			        {label:$L("Delete Transferred Data"), value:"cancel", type: 'negative'},
					{label:$L("Don't Delete Data"), type:'affirmative'}
			    ]
			 });
	},
	
	successDeleteData: function (response) {
		console.log("-------- success delete data called ------------");
		DataImportUtil.closeApp();
	},
	
	errorDeleteData: function (response) {
		Mojo.Log.error("------------ Error in deleting migration file ------------%j", response);
		DataImportUtil.closeApp();
	},
	
	closeApp: function() {
		if (this.controller) {
			this.controller.window.close();
		} else {
			Mojo.Controller.stageController.topScene().assistant.controller.window.close();
		}
	},
	
	getDataTransferReport: function() {
		this.reportRequest = AppAssistant.dataImportService.getDataTransferReport(
				this.successReport.bind(this), 
				this.errorReport.bind(this));
	},
	
	successReport: function (response) {
		Mojo.Controller.stageController.pushScene('status', response);
	},
	
	errorReport: function (response) {
		Mojo.Log.error("---------- Could not get data transfer status -----------%j", response);
	},
	
	getTransferredPIMTypes: function () {
		this.pimTypesRequest = AppAssistant.dataImportService.getTransferredPIMTypes(
				this.successPIMTypes.bind(this), 
				this.errorPIMTypes.bind(this));
	},
	
	successPIMTypes: function (response) {
		if (response.pimTypes) {
			this.pimTypes = response.pimTypes;
		}	
		this.getAllAccountsOfUser();
	}, 
	
	errorPIMTypes: function (response) {
		Mojo.Log.error("----------- Could not get pim types in the mig file -----%j", response);
		this.getAllAccountsOfUser();
	}
		
});

var DataImportUtil = new Util();
