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

var DataRestoreUtil = Class.create ({
	
	initialize: function () {
		
	},
	
	getDeviceIdWithData: function (response) {
		if(!response.allDevices) {
			return;
		}
		var devices = response.allDevices;
		var deviceId = "";
		var that = this;
		//TODO What to do if there is more than one device
		if(Object.isArray(devices) == true) {
			devices.each(function (item) {
				Mojo.Log.info("item in array======="+item);
				if(item.length > 0) {
					var tmp = item;
					if(tmp) {
						var tokens = tmp.split("-");
						if(tokens) {
							var devId = tokens[0];
							Mojo.Log.info("Device id == "+devId);
							var dataSize =  tokens[1];
							Mojo.Log.info("data size == "+dataSize);
							var versionFromServer = tokens[2];				 
							if(dataSize > 0) {
								Mojo.Log.info("----------------- versionFromServer: ---------------- ", versionFromServer);
								//TODO Get db version again if it is -1
								if((dbVersion != -1) && (versionFromServer > dbVersion)) {
									// Show error and user can't continue
									deviceId = -1;
									return deviceId;
								}
								deviceId = devId;
							}
						}	
					}
				}
			});
		} else {
			//deviceId = devices;
			var tmp = devices;
			Mojo.Log.info("Devices that have backed up data: ------ ", tmp);
			if(tmp) {
				var tokens = tmp.split("-");
				if(tokens) {
					var devId = tokens[0];
					var dataSize =  tokens[1];
					Mojo.Log.info("data size == "+dataSize);
					var versionFromServer = tokens[2];				 
					if(dataSize > 0) {
						Mojo.Log.info("----------------- versionFromServer: ---------------- ", versionFromServer);
						//TODO Get db version again if it is -1
						if((dbVersion != -1) && (versionFromServer > dbVersion)) {
							// Show error and user can't continue
							deviceId = -1;
							return deviceId;
						}
						deviceId = devId;
					} else {
						Mojo.Log.info("no data available to restore");
					}
				}	
			}
		}
		return deviceId;
	},
	
	showVersionError: function () {
		Account.action = 'version-error';
		AppAssistant.updateService = new UpdateService();
		this.conRequest = AppAssistant.accountService.getConnectionStatus(this.readConStatus.bind(this), this.errorConStatus.bind(this));
	},
	
	initUpdateService: function () {
		if(!AppAssistant.updateService) {
			AppAssistant.updateService = new UpdateService();
		}
	},
	
	getNetworkStatus: function (response) {
		var networkStatus = response.wan.network;
		if(!networkStatus || networkStatus === 'unknown' || networkStatus === 'unusable' || networkStatus === 'disconnected') {
			//Show error
			return "error";
		}
		if(networkStatus === 'gprs' || networkStatus === '1x') {
			return "2G";
		} else {
			return "3G";
		}
	},
	
	readConStatus: function (response) {
		Mojo.Log.info("------- Con status Response ---------%j", response);
		if(this.conRequest) {
			this.conRequest.cancel();
		}
		
		var networkStatus = this.getNetworkStatus(response);
		if(networkStatus === "error") {
			//Show error
			return;
		}
		if(networkStatus === "2G") {
			Mojo.Controller.stageController.swapScene('update-intro', '2G');
		} else {
			Mojo.Controller.stageController.swapScene('update-intro', '3G');
		}
	},
	
	errorConStatus: function (response) {
		Mojo.Log.error("------- Con status Error ---------%j", response);
	},
	
	
});	

var RestoreUtil = new DataRestoreUtil();