package eu.ydp.empiria.player.client.controller.extensions.jswrappers;

import com.google.gwt.core.client.JavaScriptObject;

import eu.ydp.empiria.player.client.controller.events.interaction.FeedbackInteractionSoundEvent;
import eu.ydp.empiria.player.client.controller.events.interaction.InteractionEvent;
import eu.ydp.empiria.player.client.controller.events.interaction.InteractionEventsListener;
import eu.ydp.empiria.player.client.controller.events.interaction.StateChangedInteractionEvent;
import eu.ydp.empiria.player.client.controller.extensions.ExtensionType;
import eu.ydp.empiria.player.client.controller.extensions.types.InteractionEventSocketUserExtension;

public class JsInteractionEventSocketUserExtension extends JsExtension implements
		InteractionEventSocketUserExtension {

	protected JavaScriptObject interactionSocketJs;
	protected InteractionEventsListener interactionEventsListener;
	
	@Override
	public ExtensionType getType() {
		return ExtensionType.EXTENSION_SOCKET_USER_INTERACTION_EVENT;
	}
	
	@Override
	public void init() {
		interactionSocketJs = createInteractionRequestSocketJs();
		setInteractionRequestSocketJs(extensionJsObject, interactionSocketJs);
	}
	
	@Override
	public void setInteractionEventsListener(InteractionEventsListener listener) {
		this.interactionEventsListener = listener;
	}
	
	protected void dispatchInteractionEvent(JavaScriptObject requestJs){
		InteractionEvent event = InteractionEvent.fromJsObject(requestJs);
		if (event instanceof StateChangedInteractionEvent)
			interactionEventsListener.onStateChanged( (StateChangedInteractionEvent)event);
		if (event instanceof FeedbackInteractionSoundEvent)
			interactionEventsListener.onFeedbackSound( (FeedbackInteractionSoundEvent)event);
	}
	
	private native JavaScriptObject createInteractionRequestSocketJs()/*-{
		var instance = this;
		var socket = [];
		socket.invokeInteractionRequest = function(request){
			instance.@eu.ydp.empiria.player.client.controller.extensions.jswrappers.JsInteractionEventSocketUserExtension::dispatchInteractionEvent(Lcom/google/gwt/core/client/JavaScriptObject;)(request);
		}
		return socket;
	}-*/;
	
	private native void setInteractionRequestSocketJs(JavaScriptObject extension, JavaScriptObject socket)/*-{
		if (typeof extension.setInteractionRequestsSocket == 'function'){
			extension.setInteractionRequestsSocket(socket);
		}
	}-*/;


}

@@ -1917,14 +1917,16 @@
 ket.
-invoke
+dispatch
 Inte
@@ -1932,22 +1932,20 @@
 eraction
-Reques
+Even
 t = func

change extension method name to dispatchInteractionEvent