package org.swellrt.api;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.Cookies;

/**
 * Shared logic to manage session data in the browser side.
 * 
 * See {@link SessionManagerImpl} and {@link HttpWindowSessionFilter} classes for
 * server side related logic. 
 * 
 * @author pablojan@gmail.com (Pablo Ojanguren)
 *
 */
public class BrowserSession {
	
	/** A property in localStorage to count each new tab/window */
	private final static String PROP_BROWSER_WINDOW_COUNTER = "wc";
	
	/** A window's property storing the current window id */ 
	private final static String PROP_SESSION_WINDOW_ID = "wid";
	
	/** A HTTP header to send window id */
	public final static String HTTP_HEADER_WINDOW_ID = "X-window-id";
	
	/** Name of session cookie */
	private final static String SESSION_COOKIE_NAME = "WSESSIONID";
	
	/** URL parameter name for session id */
	private final static String SESSION_PATH_PARAM = "sid";
	
	/** URL parameter for window id */
	private final static String WINDOWID_PATH_PARAM = "wid";
	
	
	public static native void init() /*-{
	
		var PROP_BROWSER_WINDOW_COUNTER = @org.swellrt.api.BrowserSession::PROP_BROWSER_WINDOW_COUNTER;
		var PROP_SESSION_WINDOW_ID = @org.swellrt.api.BrowserSession::PROP_SESSION_WINDOW_ID;
		
	
	    try {
	
	      if (!$wnd.localStorage) return;
	
	      // Generate a browser window/tab id
	      if (!$wnd[PROP_SESSION_WINDOW_ID]) {
	
	        if (!$wnd.localStorage.getItem(PROP_BROWSER_WINDOW_COUNTER)) {
	          $wnd.localStorage.setItem(PROP_BROWSER_WINDOW_COUNTER, 0);
	        }
	
	        var windowCount = $wnd.localStorage.getItem(PROP_BROWSER_WINDOW_COUNTER);
	        $wnd[PROP_SESSION_WINDOW_ID] = windowCount++;
	        $wnd.localStorage.setItem(PROP_BROWSER_WINDOW_COUNTER, windowCount);
	      }
	
	    } catch (e) {
	      console.log("Error generating session window id: "+e);
	    }
		
	
	}-*/;
	
	
	public static native String getToken() /*-{
	
		var PROP_BROWSER_WINDOW_COUNTER = @org.swellrt.api.BrowserSession::PROP_BROWSER_WINDOW_COUNTER;
		var PROP_SESSION_WINDOW_ID = @org.swellrt.api.BrowserSession::PROP_SESSION_WINDOW_ID;
		
		var token = $wnd.__session['sessionid'];
	    	    
	    try {
	      if ($wnd[PROP_SESSION_WINDOW_ID] != null) {
	        token+=":"+$wnd[PROP_SESSION_WINDOW_ID];
	       }
	     } catch(e) {
			console.log("Error getting session token: "+e);
	     }
	    return token;
	
	}-*/;
	
	
	public static native String getSessionId() /*-{
		return $wnd.__session['sessionid'];
	}-*/;
	
	
	public static native String getWindowId() /*-{
		var PROP_SESSION_WINDOW_ID = @org.swellrt.api.BrowserSession::PROP_SESSION_WINDOW_ID;
		return $wnd[PROP_SESSION_WINDOW_ID];
	}-*/;
	
	
	/**
	 * Define session data in a window object.
	 * 
	 * This is the old way from Wave to share session data
	 * in the client code.
	 *
	 * @param localDomain
	 * @param userAddress
	 * @param sessionId
	 */
	public static native JavaScriptObject setUserData(String localDomain, String userAddress, String seed,
			String sessionId) /*-{
		$wnd.__session = new Object();
		$wnd.__session['domain'] = localDomain;
		$wnd.__session['address'] = userAddress;
		$wnd.__session['id'] = seed; // 'id' is used in Session.java/ClientIdGenerator to get the seed
		$wnd.__session['sessionid'] = sessionId; //
		return $wnd.__session;
		
	}-*/;


	protected static native void clearSessionObject() /*-{
		$wnd.__session = null;
	}-*/;
	
	public static void clearUserData() {		
		Cookies.removeCookie(SESSION_COOKIE_NAME);
		clearSessionObject();
	}
	
	
	public static String getSessionURLparameter() {
	    if (Cookies.getCookie(SESSION_COOKIE_NAME) == null) {
	      return ";" + SESSION_PATH_PARAM + "=" + getSessionId();
	    }
	    return "";
	}
	
	public static String getWindowURLparameter() {
		return (getWindowId() != null ? "&"+WINDOWID_PATH_PARAM+"=" + getWindowId() : "");
	}

	/**
	 * Add an extra path token for session id (;sid=) at the end of the URL path
	 * if session cookie is not available. This is specific for the Jetty
	 * server.
	 *
	 * @param url
	 *            The url where to add the session id
	 * @return
	 */
	public static String addSessionToUrl(String url) {
		if (Cookies.getCookie(SESSION_COOKIE_NAME) == null &&  getSessionId() != null) {
			url += ";" + SESSION_PATH_PARAM + "=" +  getSessionId();
		}
		return url;
	}

}

@@ -2186,16 +2186,18 @@
 onid'%5D;%0A
+%09%09
 %09    %09  
@@ -2199,16 +2199,16 @@
   %09    %0A
-
 %09    try
@@ -2387,24 +2387,40 @@
 oken: %22+e);%0A
+%09%09%09return null;%0A
 %09     %7D%0A%09   
@@ -2496,16 +2496,80 @@
 () /*-%7B%0A
+%09%09if ($wnd.__session && $wnd.__session%5B'sessionid'%5D != null)%09%09%0A%09
 %09%09return
@@ -2598,16 +2598,35 @@
 onid'%5D;%0A
+%09%09%09%0A%09%09return null;%0A
 %09%7D-*/;%0A%09
@@ -2766,16 +2766,64 @@
 W_ID;%0A%09%09
+%0A%09%09if ($wnd%5BPROP_SESSION_WINDOW_ID%5D != null)%0A%09%09%09
 return $
@@ -2851,16 +2851,35 @@
 OW_ID%5D;%0A
+%09%09%09%0A%09%09return null;%0A
 %09%7D-*/;%0A%09

Fix minor bug in session mgmt