package org.sagebionetworks.web.client;

import org.sagebionetworks.web.client.cookie.CookieKeys;
import org.sagebionetworks.web.client.cookie.CookieProvider;
import org.sagebionetworks.web.client.mvp.AppPlaceHistoryMapper;

import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.inject.Inject;

public class GlobalApplicationStateImpl implements GlobalApplicationState {

	private PlaceController placeController;
	private CookieProvider cookieProvider;
	private AppPlaceHistoryMapper appPlaceHistoryMapper;
	private ActivityMapper directMapper;
	private PlaceChanger placeChanger;
	
	@Inject
	public GlobalApplicationStateImpl(CookieProvider cookieProvider) {
		this.cookieProvider = cookieProvider;
	}

	@Override
	public PlaceChanger getPlaceChanger() {
		if(placeChanger == null) {
			placeChanger = new PlaceChanger() {			
				@Override
				public void goTo(Place place) {
					// If we are not already on this page, go there.
					if(!placeController.getWhere().equals(place)){
						placeController.goTo(place);
					}else{
						// We are already on this page but we want to force it to reload.
						directMapper.getActivity(place);
					}

				}
			};
		}
		return placeChanger;
	}

	@Override
	public void setPlaceController(PlaceController placeController) {
		this.placeController = placeController;
	}

	@Override
	public Place getLastPlace() {
		String historyValue = cookieProvider.getCookie(CookieKeys.LAST_PLACE);
		return getPlaceFromHistoryValue(historyValue);		
	}

	@Override
	public void setLastPlace(Place lastPlace) {
		cookieProvider.setCookie(CookieKeys.LAST_PLACE, appPlaceHistoryMapper.getToken(lastPlace));
	}

	@Override
	public Place getCurrentPlace() {
		String historyValue = cookieProvider.getCookie(CookieKeys.CURRENT_PLACE);
		return getPlaceFromHistoryValue(historyValue);		
	}

	@Override
	public void setCurrentPlace(Place currentPlace) {		
		cookieProvider.setCookie(CookieKeys.CURRENT_PLACE, appPlaceHistoryMapper.getToken(currentPlace));
	}

	@Override
	public void setAppPlaceHistoryMapper(AppPlaceHistoryMapper appPlaceHistoryMapper) {
		this.appPlaceHistoryMapper = appPlaceHistoryMapper;
	}

	@Override
	public AppPlaceHistoryMapper getAppPlaceHistoryMapper() {
		return appPlaceHistoryMapper;
	}

	/*
	 * Private Methods
	 */
	private Place getPlaceFromHistoryValue(String historyValue) {
		if(historyValue != null) {
			Place place = appPlaceHistoryMapper.getPlace(historyValue);
			return place;
		}
		return null;
	}

	@Override
	public void setActivityMapper(ActivityMapper mapper) {
		this.directMapper = mapper;
	}
	
}

@@ -34,16 +34,40 @@
 lient;%0A%0A
+import java.util.Date;%0A%0A
 import o
@@ -115,16 +115,16 @@
 ieKeys;%0A
-
 import o
@@ -386,24 +386,83 @@
 Controller;%0A
+import com.google.gwt.user.datepicker.client.CalendarUtil;%0A
 import com.g
@@ -1538,32 +1538,34 @@
 getLastPlace() %7B
+%09%09
 %0A%09%09String histor
@@ -1733,16 +1733,103 @@
 lace) %7B%0A
+%09%09Date expires = new Date(System.currentTimeMillis() + 300000); // store for 5 minutes%0A
 %09%09cookie
@@ -1907,24 +1907,33 @@
 n(lastPlace)
+, expires
 );%0A%09%7D%0A%0A%09@Ove
@@ -2167,16 +2167,103 @@
 ce) %7B%09%09%0A
+%09%09Date expires = new Date(System.currentTimeMillis() + 300000); // store for 5 minutes%0A
 %09%09cookie
@@ -2351,16 +2351,25 @@
 ntPlace)
+, expires
 );%0A%09%7D%0A%0A%09
@@ -2952,11 +2952,12 @@
 er;%0A%09%7D%0A%09
+%09
 %0A%7D%0A

expire stored places after 5 minutes. meant only for login redirect