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

import com.google.gwt.core.client.JavaScriptObject;
import eu.ydp.empiria.player.client.controller.events.delivery.DeliveryEvent;
import eu.ydp.empiria.player.client.controller.events.delivery.DeliveryEventType;
import eu.ydp.empiria.player.client.controller.events.interaction.MediaInteractionSoundEventCallback;
import eu.ydp.empiria.player.client.controller.events.interaction.MediaInteractionSoundEventCallforward;
import eu.ydp.empiria.player.client.controller.extensions.ExtensionType;
import eu.ydp.empiria.player.client.controller.extensions.types.DeliveryEventsListenerExtension;

public class JsSoundProcessorExtension extends JsExtension implements
		DeliveryEventsListenerExtension {

	protected boolean playing;
	protected JavaScriptObject socketJs;
	protected MediaInteractionSoundEventCallback currCallback;
	
	@Override
	public void onDeliveryEvent(DeliveryEvent deliveryEvent) {
		if (deliveryEvent.getType() == DeliveryEventType.PAGE_UNLOADING){
			if (playing)
				soundStop();
		}
		if (deliveryEvent.getType() == DeliveryEventType.FEEDBACK_SOUND  ||  deliveryEvent.getType() == DeliveryEventType.MEDIA_SOUND_PLAY){

			if (deliveryEvent.getParams().containsKey("url")  &&  deliveryEvent.getParams().get("url") instanceof String){

				MediaInteractionSoundEventCallback callback = null;
				if (deliveryEvent.getParams().containsKey("callback")  &&  deliveryEvent.getParams().get("callback") instanceof MediaInteractionSoundEventCallback){
					callback = ((MediaInteractionSoundEventCallback)deliveryEvent.getParams().get("callback"));
				}
				
				if (playing)
					soundStop();				
				
				if (callback != null){
					
					currCallback = callback;	
				
					playing = true;
					callback.onPlay();
					callback.setCallforward(new MediaInteractionSoundEventCallforward() {
						
						@Override
						public void stop() {
							soundStop();
						}
					});
				}
				
				soundPlayJs(extensionJsObject, deliveryEvent.toJsObject());
				
				
			}
			
		}

	}
	
	protected void soundStop(){
		playing = false;
		soundStopJs(extensionJsObject);
		if (currCallback != null)
			currCallback.onStop();
	}

	@Override
	public ExtensionType getType() {
		return ExtensionType.EXTENSION_PROCESSOR_SOUND;
	}

	@Override
	public void init() {
		socketJs = createSoundProcessorSocketJs();
		setSoundProcessorSocketJs(extensionJsObject, socketJs);
	}
	
	private void onSoundFinished(){
		soundStop();
	}
	
	private native void soundPlayJs(JavaScriptObject extenstionObject, JavaScriptObject eventJsObject)/*-{
		if (typeof extenstionObject.soundPlay == 'function'){
			extenstionObject.soundPlay(eventJsObject);
		}
	}-*/;
	
	private native void soundStopJs(JavaScriptObject extenstionObject)/*-{
		if (typeof extenstionObject.soundStop == 'function'){
			extenstionObject.soundStop();
		}
	}-*/;
	
	private native JavaScriptObject createSoundProcessorSocketJs()/*-{
		var instance = this;
		var socket = [];
		socket.onSoundFinished = function(){
			return instance.@eu.ydp.empiria.player.client.controller.extensions.jswrappers.JsSoundProcessorExtension::onSoundFinished()();
		}
		return socket;
	}-*/;
	

	private native void setSoundProcessorSocketJs(JavaScriptObject extension, JavaScriptObject socket)/*-{
		if (typeof extension.setSoundProcessorSocket == 'function'){
			extension.setSoundProcessorSocket(socket);
		}
	}-*/;

}

@@ -2151,28 +2151,8 @@
 )%7B%0D%0A
-%09%09playing = false;%0D%0A
 %09%09so
@@ -2182,16 +2182,97 @@
 ject);%0D%0A
+%09%09soundFinished();%0D%0A%09%7D%0D%0A%09%0D%0A%09protected void soundFinished()%7B%0D%0A%09%09playing = false;%0D%0A
 %09%09if (cu
@@ -2613,28 +2613,32 @@
 ()%7B%0D%0A%09%09sound
-Stop
+Finished
 ();%0D%0A%09%7D%0D%0A%09%0D%0A

update js sound processor