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

var ConfirmSyncDataAssistant = Class.create({
	
	initialize: function(accountType, loginName, accountId){
		this.accountType = accountType;
		this.accountId = accountId;
		Mojo.Log.info("------------- account type ----------------", accountType);
		this.loginName = loginName;
	},
	
	setup: function() {
		
		this.appMenuModel = { 
			items: [
				Mojo.Menu.editItem,
                {label: $L('Cancel Transport'), command: 'cancelImport'},
                {label: $L('Help'), command: 'help'}
			],
		};
		
		this.controller.setupWidget(Mojo.Menu.appMenu, {omitDefaultItems: true}, this.appMenuModel);
		
		this.messageModel = { email: this.loginName, domain: this.accountType };
		if (this.accountType == 'gmail') {
			this.setupGoogleAccount();
		} 
		else if (this.accountType == 'yahoo') {
			this.setupYahooAccount();
		}  
		else if (this.accountType == 'eas') {
			this.setupEasAccount();
		} 
		else if (this.accountType == 'local') {
			this.setupLocalAccount();
		}
		
		var msgDiv = Mojo.View.render({
			template: 'confirm-sync-data/confirm-text',
			object: this.messageModel
		});
		
		$('syncData').update(msgDiv);
		
		this.controller.listen(this.controller.get('backButton'), Mojo.Event.tap, this.backToAccountTypes.bindAsEventListener(this));
		this.controller.listen(this.controller.get('nextButton'), Mojo.Event.tap, this.importData.bindAsEventListener(this));
		this.controller.listen(this.controller.get('nextYahooButton'), Mojo.Event.tap, this.importData.bindAsEventListener(this));
	},
	
	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);
		}	
	},
	
	setupGoogleAccount: function () {
		this.messageModel.phoneStuff = $L("Tasks and memos/notes will be transferred to your phone only.");
		this.messageModel.syncStuff = $L("You picked the Google account #{email}.").interpolate({
				email: this.messageModel.email
		});
		this.messageModel.moreSyncStuff = $L("Your contacts and calendar events will be transferred to this Google account.");
	},
	
	setupYahooAccount: function () {
		this.messageModel.phoneStuff = $L("Tasks and memos/notes will be transferred to your phone only.");
		this.messageModel.syncStuff = "";
		this.messageModel.moreSyncStuff = $L("To transfer contacts & calendar to Yahoo!, you must use Yahoo!'s upload tool. Go to http://support.palm.com for more info.");
	},
	
	setupEasAccount: function () {
		this.messageModel.phoneStuff = $L("Your memos/notes will be transferred to your phone only.");
		this.messageModel.syncStuff = $L("You picked the Exchange account #{email}.").interpolate({
				email: this.messageModel.email
		});
		this.messageModel.moreSyncStuff = $L("Your contacts, calendar events and tasks will be transferred to this Exchange account.");
	},
	
	setupLocalAccount: function () {
		this.messageModel.phoneStuff = "";
		this.messageModel.syncStuff = $L("You picked the Palm Profile account for #{email}.").interpolate({
				email: this.messageModel.email
		});
		this.messageModel.moreSyncStuff = $L("Your transferred data will be stored on your device and backed up to your Palm profile. You can access this data only on your phone, not on the web or in desktop software.");
	},
	
	showPopup: function () {
		
		if(!this.messageModel.moreSyncStuff) {
			this.messageModel.moreSyncStuff = $L("Your transferred data will be stored on your device and backed up to your Palm profile. You can access this data only on your phone, not on the web or in desktop software.");
		}
		var dialog = this.controller.showAlertDialog({
			title: $L("Data stored on device"), 
			onChoose: function(value){
				if (value == "import") {
					this.startDataImport();
				}
			},
		    message: this.messageModel.moreSyncStuff,
		    choices:[
				{label:$L("Cancel"), type: 'dismiss'},
				{label:$L("OK"), type: 'alert', value:"import"}
		    ]
		 });
	},
	
	backToAccountTypes: function(event) {
		Mojo.Controller.stageController.popScene();
	},
	
	importData: function(event) {
		if(this.accountType == 'local') {
			this.showPopup();
			return;
		}
		this.startDataImport();	
	},
	
	startDataImport: function () {
		this.controller.setupWidget(Mojo.Menu.appMenu, {omitDefaultItems: false});
		
		AppAssistant.dataImportService.importData(
				this.accountType,
				this.accountId,
				this.successImportData.bind(this), 
				this.errorImportData.bind(this), 
				this.controller);
	},
	
	successImportData: function (response) {
		Mojo.Log.info ("----------------- Success in importing data -------------%j", response);
		Mojo.Controller.stageController.popScenesTo();
		Mojo.Controller.stageController.pushScene("done-import", this.accountType);
	},
	
	errorImportData: function (response) {
		Mojo.Log.error ("----------------- Error in importing data -------------%j", response);
		var messageText = $L("There was a problem transferring your data. Please start over by running the desktop application again.")
		var dialog = this.controller.showAlertDialog({
			title: $L("Data transfer error"), 
			onChoose: function(value){
				if(value == "ok") {
					this.controller.window.close();
				} 
			},
		    message: messageText,
		    choices:[
		        {label:$L("OK"), value:"ok", type: 'dismiss'}
		    ]
		 });
	},
	
	addAccount: function (event) {
		Mojo.Log.info("Push add contact scene");
	}
});	