package org.sagebionetworks.web.client;

import java.util.Objects;

import org.sagebionetworks.web.client.cookie.CookieKeys;
import org.sagebionetworks.web.client.cookie.CookieProvider;
import org.sagebionetworks.web.client.security.AuthenticationController;

import com.google.gwt.user.client.Timer;
import com.google.inject.Inject;

public class SessionTokenDetector {

	private static final int INTERVAL_MS = 1000*5; //check every 5 seconds
	Timer timer;
	CookieProvider cookies;
	AuthenticationController authController;
	GlobalApplicationState globalAppState;
	boolean isSessionToken;
	@Inject
	public SessionTokenDetector(
			CookieProvider cookies, 
			AuthenticationController authController,
			GlobalApplicationState globalAppState) {
		this.cookies = cookies;
		this.authController = authController;
		this.globalAppState = globalAppState;
		initializeSessionTokenState();
	}
	
	private boolean isSessionTokenCookie() {
		return cookies.getCookie(CookieKeys.USER_LOGIN_TOKEN) != null;
	}
	public void start() {
		timer  = new Timer() {	
			@Override
			public void run() {
				boolean isNowSessionToken = isSessionTokenCookie();
				if (!Objects.equals(isSessionToken, isNowSessionToken)) {
					// session token state mismatch, update the app state (by loading the current session data, or clearing) and refresh the page (not an app reload).
					if (isNowSessionToken) {
						authController.reloadUserSessionData();
					} else {
						authController.logoutUser();
					}
					globalAppState.refreshPage();
				}
			}
		};
		timer.scheduleRepeating(INTERVAL_MS);		
	}
	
	public void initializeSessionTokenState() {
		//if user explicitly logs out or in, then there's no need to reload when detecting state change.  Re-initialize session token state
		isSessionToken = isSessionTokenCookie();
	}
}

@@ -257,49 +257,8 @@
 r;%0A%0A
-import com.google.gwt.user.client.Timer;%0A
 impo
@@ -369,17 +369,18 @@
  = 1000*
-5
+10
 ; //chec
@@ -391,9 +391,10 @@
 ery 
-5
+10
  sec
@@ -402,22 +402,8 @@
 nds%0A
-%09Timer timer;%0A
 %09Coo
@@ -505,16 +505,33 @@
 pState;%0A
+%09GWTWrapper gwt;%0A
 %09boolean
@@ -698,16 +698,35 @@
 AppState
+,%0A%09%09%09GWTWrapper gwt
 ) %7B%0A%09%09th
@@ -827,16 +827,34 @@
 pState;%0A
+%09%09this.gwt = gwt;%0A
 %09%09initia
@@ -1022,69 +1022,39 @@
 %7B%0A%09%09
-timer  = new Timer() %7B%09%0A%09%09%09@Override%0A%09%09%09public void run()
+gwt.scheduleFixedDelay(() -%3E
  %7B%0A
-%09
 %09%09%09b
@@ -1104,17 +1104,16 @@
 okie();%0A
-%09
 %09%09%09if (!
@@ -1161,25 +1161,24 @@
 onToken)) %7B%0A
-%09
 %09%09%09%09// sessi
@@ -1320,17 +1320,16 @@
 d).%0A%09%09%09%09
-%09
 if (isNo
@@ -1345,17 +1345,16 @@
 oken) %7B%0A
-%09
 %09%09%09%09%09aut
@@ -1394,17 +1394,16 @@
 ();%0A%09%09%09%09
-%09
 %7D else %7B
@@ -1403,17 +1403,16 @@
  else %7B%0A
-%09
 %09%09%09%09%09aut
@@ -1441,20 +1441,18 @@
 ();%0A%09%09%09%09
-%09
 %7D%0A
-%09
 %09%09%09%09glob
@@ -1484,47 +1484,15 @@
 %0A%09%09%09
-%09
 %7D%0A%09%09
-%09%7D%0A%09%09%7D;%0A%09%09timer.scheduleRepeating(
+%7D, 
 INTE
@@ -1500,18 +1500,16 @@
 VAL_MS);
-%09%09
 %0A%09%7D%0A%09%0A%09p

use gwt wrapper for tests