package eu.ydp.empiria.player.client.util;

import java.util.HashSet;
import java.util.Set;

import com.google.gwt.dom.client.Element;

/**
 * Klasa pomocnicza do obslugi trybu pelnoekranowego w przegladarkach
 * 
 */
public class HTML5FullScreen {

	private static Set<FullScreenEventHandler> eventHandlers = new HashSet<HTML5FullScreen.FullScreenEventHandler>();

	public interface FullScreenEvent {
		/**
		 * zwraca true jesli przegladarka jest w trybie pelnoekranowym
		 * 
		 * @return
		 */
		public boolean isInFullScreen();
	}

	public interface FullScreenEventHandler {
		void handleEvent(FullScreenEvent event);
	}

	/**
	 * Dodaje handlera nasluchujacego na zmiany trybu pelnoekranowego
	 * 
	 * @param handler
	 */
	public static void addFullScreenEventHandler(FullScreenEventHandler handler) {
		if (handler == null)
			return;
		if (eventHandlers.size() == 0) {
			addFullScreenChangeHandler();
		}
		eventHandlers.add(handler);
	}

	/**
	 * probuje wyswietlic podany element w trybie fullScreen
	 * 
	 * @param element
	 * @return true jezeli sie uda false w przeciwnym razie
	 */
	public static boolean requestFullScreen(Element element) {
		return nativeRequestFullScrean(element);
	}

	private static native boolean nativeRequestFullScrean(Element element)/*-{
		try {
			if (element.requestFullscreen) {
				element.requestFullscreen();
				return true;
			} else if (element.mozRequestFullScreen) {
				element.mozRequestFullScreen();
				return true;
			} else if (element.webkitRequestFullScreen) {
				element.webkitRequestFullScreen();
				return true;
			}
		} catch (e) {
			console.log(e);
		}
		return false;
	}-*/;

	/**
	 * Probuje opuscic tryb fullScreen
	 * 
	 * @param element
	 * @return
	 */
	public static boolean exitFullScreen() {
		return nativeExitFullScreen();
	}

	private static native boolean nativeExitFullScreen()/*-{
		try {
			if ($doc.exitFullscreen) {
				$doc.exitFullscreen();
				return true;
			} else if ($doc.mozCancelFullScreen) {
				$doc.mozCancelFullScreen();
				return true;
			} else if ($doc.webkitCancelFullScreen) {
				$doc.webkitCancelFullScreen();
				return true;
			}

		} catch (e) {
			console.log(e);
		}
		return false;
	}-*/;

	/**
	 * metoda wywolywana z poziomu jsni dla obslugi zdarzen fullScreen
	 */
	protected static void handleEvent() {
		final boolean inFullScreen = isInFullScreen();
		FullScreenEvent fse = new FullScreenEvent() {

			@Override
			public boolean isInFullScreen() {
				return inFullScreen;
			}
		};
		for (FullScreenEventHandler h : eventHandlers) {
			h.handleEvent(fse);
		}
	}

	/**
	 * Czy w danym momencie jest aktywny tryb pelnoekranowy
	 * @return 
	 */
	public static native boolean isInFullScreen()/*-{
		try {
			return $doc.fullscreen || $doc.mozFullScreen
					|| $doc.webkitIsFullScreen ? true : false;
		} catch (e) {
			//console.log(e);
		}
		return false;
	}-*/;

	private static native void addFullScreenChangeHandler()/*-{
		$doc
				.addEventListener(
						"fullscreenchange",
						@eu.ydp.empiria.player.client.util.HTML5FullScreen::handleEvent(),
						false);
		$doc
				.addEventListener(
						"mozfullscreenchange",
						@eu.ydp.empiria.player.client.util.HTML5FullScreen::handleEvent(),
						false);
		$doc
				.addEventListener(
						"webkitfullscreenchange",
						@eu.ydp.empiria.player.client.util.HTML5FullScreen::handleEvent(),
						false);

	}-*/;

}

@@ -205,18 +205,17 @@
 rkach%0A *
- 
 %0A
+
  */%0Apubl
@@ -469,17 +469,16 @@
 wym%0A%09%09 *
- 
 %0A%09%09 * @r
@@ -689,25 +689,24 @@
 ranowego%0A%09 *
- 
 %0A%09 * @param 
@@ -820,16 +820,18 @@
 == null)
+ %7B
 %0A%09%09%09retu
@@ -834,16 +834,20 @@
 return;%0A
+%09%09%7D%0A
 %09%09if (ev
@@ -1002,33 +1002,32 @@
 e fullScreen%0A%09 *
- 
 %0A%09 * @param elem
@@ -1685,17 +1685,16 @@
 reen%0A%09 *
- 
 %0A%09 * @pa
@@ -2653,17 +2653,16 @@
  @return
- 
 %0A%09 */%0A%09p
@@ -2940,16 +2940,22 @@
 r()/*-%7B%0A
+%09try%7B%0A
 %09%09$doc%0A%09
@@ -3384,16 +3384,31 @@
 false);%0A
+%09%7Dcatch(e)%7B%0A%0A%09%7D
 %0A%09%7D-*/;%0A

remove support for fullScreenApi for IE bug 67178