package com.etherios.gae.connector.tank.datastore;

import javax.jdo.PersistenceManager;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class UserDataUtils {
	/**
	 * Returns the user corresponding to the given key from the Data Store.
	 * 
	 * @param userID The key (username + server) of the user to get from the Data Store.
	 * @return The user.
	 */
	public static UserData getUserFromDataStore(String userID) {
		Key key = KeyFactory.createKey(UserData.class.getSimpleName(), userID);
		UserData userData = null;
		
		try {
			PersistenceManager pm = PMF.get().getPersistenceManager();
			userData = pm.getObjectById(UserData.class, key);
			pm.close();
		} catch (Exception e) {
			return null;
		}
		
		return userData;
	}
	
	/**
	 * Adds the given user to the Data Store.
	 * 
	 * @param userData The user to be added to the data store.
	 */
	public static void addUserToDataStore(UserData userData) {
		if (userData == null)
			return;
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.makePersistent(userData);
		pm.close();
	}
	
	/**
	 * Updates the password of the user in the Data Store.
	 * 
	 * @param userID The key (username + server) of the user to update its password.
	 * @param password The new password for the user in the Data Store.
	 */
	public static void updateUserPassword(String userID, String password) {
		Key key = KeyFactory.createKey(UserData.class.getSimpleName(), userID);
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		UserData userData = pm.getObjectById(UserData.class, key);
		userData.setPassword(password);
		pm.close();
	}
	
	/**
	 * Sets the selected server of the user in the Data Store.
	 * 
	 * @param userID The key (username + server) of the user to update its password.
	 * @param server The name of the server the user belongs to.
	 */
	public static void setServer(String userID, String server) {
		Key key = KeyFactory.createKey(UserData.class.getSimpleName(), userID);
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		UserData userData = pm.getObjectById(UserData.class, key);
		userData.setServer(server);
		pm.close();
	}
	
	/**
	 * Sets the selected Device of the user in the Data Store.
	 * 
	 * @param userID The key (username + server) of the user to update its password.
	 * @param selectedDevice The ID of the selected device for the user.
	 */
	public static void setSelectedDevice(String userID, String selectedDevice) {
		Key key = KeyFactory.createKey(UserData.class.getSimpleName(), userID);
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		UserData userData = pm.getObjectById(UserData.class, key);
		userData.setSelectedDevice(selectedDevice);
		pm.close();
	}
	
	/**
	 * Sets the selected XBee device of the user in the Data Store.
	 * 
	 * @param userID The key (username + server) of the user to update its password.
	 * @param selectedDevice The ID of the selected XBee device for the user.
	 */
	public static void setSelectedXBeeDevice(String userID, String selectedXBeeDevice) {
		Key key = KeyFactory.createKey(UserData.class.getSimpleName(), userID);
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		UserData userData = pm.getObjectById(UserData.class, key);
		userData.setSelectedXBee(selectedXBeeDevice);
		pm.close();
	}
	
	/**
	 * Sets the selected device latitude in the Data Store.
	 * 
	 * @param userID The key (username + server) of the user to update its password.
	 * @param latitude The selected device latitude.
	 */
	public static void setLatitude(String userID, String latitude) {
		Key key = KeyFactory.createKey(UserData.class.getSimpleName(), userID);
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		UserData userData = pm.getObjectById(UserData.class, key);
		userData.setLatitude(latitude);
		pm.close();
	}
	
	/**
	 * Sets the selected device longitude in the Data Store.
	 * 
	 * @param userID The key (username + server) of the user to update its password.
	 * @param longitude The selected device longitude.
	 */
	public static void setLongitude(String userID, String longitude) {
		Key key = KeyFactory.createKey(UserData.class.getSimpleName(), userID);
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		UserData userData = pm.getObjectById(UserData.class, key);
		userData.setLongitude(longitude);
		pm.close();
	}
}
