/*
  The MIT License

  Copyright (c) 2009 Krzysztof Langner

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
 */
package eu.ydp.empiria.player.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.XMLParser;

import eu.ydp.empiria.player.client.util.file.xml.XmlData;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class PlayerEntryPoint implements EntryPoint {

	/** Player object */
	public static Player player;
	private static String url1;
	private static JavaScriptObject jsObject;
	private static String node_id;

	/**
	 * This is the entry point method.
	 */
	@Override
	public void onModuleLoad() {
		// Define js API
		initJavaScriptAPI();
	}

	/**
	 * Init Javascript API
	 */
	private static native void initJavaScriptAPI() /*-{
		// CreatePlayer
		$wnd.empiriaCreatePlayer = function(id) {
		  var player = @eu.ydp.empiria.player.client.PlayerEntryPoint::createPlayer(Ljava/lang/String;)(id);
		  player.load = function(url){
		    @eu.ydp.empiria.player.client.PlayerEntryPoint::load(Ljava/lang/String;)(url);
		  }
		  player.loadFromData = function(assessmentData, itemDatas){
		    @eu.ydp.empiria.player.client.PlayerEntryPoint::load(Lcom/google/gwt/core/client/JavaScriptObject;Lcom/google/gwt/core/client/JavaScriptObject;)(assessmentData, itemDatas);
		  }

		  // �adowanie rozszerze� (plugin�w i addon�w)
		  player.loadExtension = function(obj){
		  	if (typeof obj == 'object')
		  		@eu.ydp.empiria.player.client.PlayerEntryPoint::loadExtension(Lcom/google/gwt/core/client/JavaScriptObject;)(obj);
		  	else if (typeof obj == 'string')
		  		@eu.ydp.empiria.player.client.PlayerEntryPoint::loadExtension(Ljava/lang/String;)(obj);
		  }

		  return player;
		}

		// Call App loaded function
		if(typeof $wnd.empiriaPlayerAppLoaded == 'function') {
		  $wnd.empiriaPlayerAppLoaded();
		}
	}-*/;

	/**
	 * createPlayer js interface
	 *
	 * @param node_id
	 */
	public static JavaScriptObject createPlayer(String node_id) {
		PlayerEntryPoint.node_id = node_id;
		jsObject = JavaScriptObject.createFunction();
		return jsObject;
	}

	/**
	 * Load assessment from this url
	 *
	 * @param url
	 */
	public static void load(final String url) {
		GWT.runAsync(new RunAsyncCallback() {
			@Override
			public void onSuccess() {
				if(player == null){
					player = new Player(node_id, jsObject);
				}
				player.load(url);
			}

			@Override
			public void onFailure(Throwable reason) {
				throw new RuntimeException("Error while loading player!", reason);
			}
		});
	}

	/**
	 * Load assessment from string data
	 *
	 * @param url
	 */
	public static void load(final JavaScriptObject assessmentData, final JavaScriptObject itemDatas) {
		GWT.runAsync(new RunAsyncCallback() {
			@Override
			public void onSuccess() {
				Document assessmentDoc = XMLParser.parse(decodeXmlDataDocument(assessmentData));
				XmlData assessmentXmlData = new XmlData(assessmentDoc,  decodeXmlDataBaseURL(assessmentData));

				JsArray<JavaScriptObject> itemDatasArray = itemDatas.cast();

				XmlData itemXmlDatas[] = new XmlData[itemDatasArray.length()];
				for (int i = 0 ; i < itemDatasArray.length() ; i ++){
					Document itemDoc = XMLParser.parse(decodeXmlDataDocument(itemDatasArray.get(i)));
					itemXmlDatas[i] = new XmlData(itemDoc,  decodeXmlDataBaseURL(itemDatasArray.get(i)));
				}
				player = new Player(node_id, jsObject);
				player.load(assessmentXmlData, itemXmlDatas);
			}

			@Override
			public void onFailure(Throwable reason) {
				throw new RuntimeException("Error while loading player!", reason);
			}
		});
	}

	private native static String decodeXmlDataDocument(JavaScriptObject data)/*-{
		if (typeof data.document == 'string')
			return data.document;
		return "";
	 }-*/;

	private native static String decodeXmlDataBaseURL(JavaScriptObject data)/*-{
		if (typeof data.baseURL == 'string')
			return data.baseURL;
		return "";
	 }-*/;

	public static void loadExtension(JavaScriptObject extension){
		player.loadExtension(extension);
	}
	public static void loadExtension(String extension){
		//player.loadExtension(extension);
	}

}

@@ -2575,17 +2575,16 @@
 %0A%09%09  // 
-%EF%BF%BD
 adowanie
@@ -2597,17 +2597,16 @@
 erze
-%EF%BF%BD
  (plugin
 %EF%BF%BDw i
@@ -2601,17 +2601,16 @@
  (plugin
-%EF%BF%BD
 w i addo
@@ -2614,9 +2614,8 @@
 ddon
-%EF%BF%BD
 w)%0A%09
@@ -3625,33 +3625,36 @@
 load(url);%0A%09%09%09%7D%0A
+%09%09%09
 %0A
-
 %09%09%09@Override%0A%09%09%09
@@ -4213,17 +4213,21 @@
 Data));%0A
+%09%09%09%09
 %0A
-
 %09%09%09%09JsAr
@@ -4283,17 +4283,21 @@
 cast();%0A
+%09%09%09%09
 %0A
-
 %09%09%09%09XmlD
@@ -4696,17 +4696,20 @@
 );%0A%09%09%09%7D%0A
+%09%09%09
 %0A
-
 %09%09%09@Over
@@ -5327,20 +5327,18 @@
 nsion)%7B%0A
+
 %09%09
-//
 player.l

revert changes issue YPUB-5087