package com.cherokeelessons.dict.events;

import com.cherokeelessons.dict.client.HistoryChangeHandler.AppLocation;
import com.cherokeelessons.dict.ui.AnalysisView;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.event.shared.binder.EventBinder;
import com.google.web.bindery.event.shared.binder.EventHandler;

public class AppLocationHandler {
	public interface AppLocationEventBinder extends
			EventBinder<AppLocationHandler> {
	}

	private final AppLocationEventBinder binder = GWT
			.create(AppLocationEventBinder.class);

	private final RootPanel rp;
	private final EventBus eventBus;

	public AppLocationHandler(RootPanel rp, EventBus eventBus) {
		binder.bindEventHandlers(this, eventBus);
		this.rp = rp;
		this.eventBus = eventBus;
	}

	private AppLocation current = null;

	@EventHandler
	public void location(AppLocationEvent event) {
		if (!event.location.equals(current)) {
			rp.clear(true);
			switch (event.location) {
			case Analyzer:
				AnalysisView mainwindow = new AnalysisView(eventBus, rp);
				rp.add(mainwindow);
				break;
			}
		}
	}
	
	private boolean enablepushstate=true;

	/**
	 * Record as new page visit current "view state"
	 * 
	 * @param event
	 */
	@EventHandler
	public void saveState(HistoryTokenEvent event) {
		if (event.replace) {
			replaceState("#" + URL.encode(event.hash), Document.get().getTitle());
			History.replaceItem(event.hash, false);
		} else {
			pushState("#" + URL.encode(event.hash), Document.get().getTitle());
			History.newItem(event.hash, false);
		}
		if (event.trigger) {
			History.fireCurrentHistoryState();
		}
	}

	public static native boolean hasState()/*-{
		try {
			return $wnd.history && $wnd.history.pushState;
		} catch (e) {
		}
		return false;
	}-*/;

	/**
	 * Modern Chrome's do not obey location requests anymore?
	 */
	public static native void replaceState(String hash, String title)/*-{
		try {
			$wnd.history.replaceState(hash, title, hash);
		} catch (e) {
			$wnd.console.log(e);
		}

	}-*/;

	/**
	 * Modern Chrome's do not obey location requests anymore?
	 */
	public static native void pushState(String hash, String title)/*-{
		try {
			$wnd.history.pushState(hash, title, hash);
		} catch (e) {
			$wnd.console.log(e);
		}

	}-*/;
}

@@ -584,24 +584,25 @@
 ndler %7B%0A
+%0A
 %09public 
 interfac
@@ -593,16 +593,23 @@
 %09public 
+static 
 interfac
@@ -908,92 +908,220 @@
 %7B%0A%09%09
-binder.bindEventHandlers(this, eventBus);%0A%09%09this.rp = rp;%0A%09%09this.eventBus = eventBus
+this.rp = rp;%0A%09%09this.eventBus = eventBus;%0A%09%09GWT.log(this.getClass().getName() + %22#%22 + binder.toString());%0A%09%09binder.bindEventHandlers(this, eventBus);%0A%09%09GWT.log(this.getClass().getName() + %22#%22 + binder.toString())
 ;%0A%09%7D
@@ -1260,24 +1260,130 @@
 current)) %7B%0A
+%09%09%09GWT.log(%22LOCATION CHANGE: %22 + String.valueOf(current) + %22 =%3E %22%0A%09%09%09%09%09+ String.valueOf(event.location));%0A
 %09%09%09rp.clear(
@@ -1496,16 +1496,152 @@
 Bus, rp)
+ %7B%0A%0A%09%09%09%09%09@Override%0A%09%09%09%09%09protected void onLoad() %7B%0A%09%09%09%09%09%09super.onLoad();%0A%09%09%09%09%09%09onEnableSearch(new EnableSearchEvent(false));%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D
 ;%0A%09%09%09%09rp
@@ -1681,17 +1681,16 @@
 %0A%09%09%7D%0A%09%7D%0A
-%09
 %0A%09privat
@@ -1714,17 +1714,19 @@
 ushstate
-=
+ = 
 true;%0A%0A%09
@@ -1946,32 +1946,38 @@
 , Document.get()
+%0A%09%09%09%09%09
 .getTitle());%0A%09%09

disable search on load