package eu.ydp.empiria.player.client.controller.extensions.internal.sound;

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

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.internal.InternalExtension;
import eu.ydp.empiria.player.client.controller.extensions.types.DeliveryEventsListenerExtension;

public class DefaultSoundProcessorExtension extends InternalExtension implements DeliveryEventsListenerExtension, SoundExecutorListener {

	protected SoundExecutor soundExecutor;
	protected MediaInteractionSoundEventCallback callback;

	public DefaultSoundProcessorExtension(){
	}
	
	@Override
	public void init() {
		
		soundExecutor = GWT.create(SoundExecutor.class);
		soundExecutor.setSoundFinishedListener(this);
	}

	@Override
	public ExtensionType getType() {
		return ExtensionType.EXTENSION_LISTENER_DELIVERY_EVENTS;
	}
	
	@Override
	public void onDeliveryEvent(DeliveryEvent deliveryEvent) {
		if (deliveryEvent.getType() == DeliveryEventType.PAGE_UNLOADING){
			forceStop();
		}
		if (deliveryEvent.getType() == DeliveryEventType.FEEDBACK_SOUND  ||  deliveryEvent.getType() == DeliveryEventType.MEDIA_SOUND_PLAY){

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

				soundExecutor.stop();
				
				callback = null;
				if (deliveryEvent.getParams().containsKey("callback")  &&  deliveryEvent.getParams().get("callback") instanceof MediaInteractionSoundEventCallback){
					callback = ((MediaInteractionSoundEventCallback)deliveryEvent.getParams().get("callback"));
				}
				
				callback.setCallforward(new MediaInteractionSoundEventCallforward() {
					
					@Override
					public void stop() {
						forceStop();
					}
				});
				
				soundExecutor.play(url);
				
			}
		}
	}
	
	protected void forceStop(){
		soundExecutor.stop();		
	}

	@Override
	public void onSoundFinished() {
		if (callback != null)
			callback.onStop();
		
	}

	@Override
	public void onPlay() {

		if (callback != null){
			callback.onPlay();
			callback.setCallforward(new MediaInteractionSoundEventCallforward() {
				@Override
				public void stop() {
					forceStop();
				}
			});
		}
		
	}

}

@@ -2091,32 +2091,61 @@
 ;%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%09%0D%0A
+%09%09%09%09if (callback != null)%7B%0D%0A%09
 %09%09%09%09callback.set
@@ -2204,26 +2204,28 @@
 d() %7B%0D%0A%09%09%09%09%09
+%09
 %0D%0A
+%09
 %09%09%09%09%09@Overri
@@ -2224,24 +2224,25 @@
 %09@Override%0D%0A
+%09
 %09%09%09%09%09public 
@@ -2258,24 +2258,25 @@
 () %7B%0D%0A%09%09%09%09%09%09
+%09
 forceStop();
@@ -2278,27 +2278,29 @@
 op();%0D%0A%09%09%09%09%09
+%09
 %7D%0D%0A
+%09
 %09%09%09%09%7D);%0D%0A%09%09%09
@@ -2292,24 +2292,31 @@
 %0D%0A%09%09%09%09%09%7D);%0D%0A
+%09%09%09%09%7D%0D%0A
 %09%09%09%09%0D%0A%09%09%09%09so
@@ -2622,157 +2622,8 @@
 );%0D%0A
-%09%09%09callback.setCallforward(new MediaInteractionSoundEventCallforward() %7B%0D%0A%09%09%09%09@Override%0D%0A%09%09%09%09public void stop() %7B%0D%0A%09%09%09%09%09forceStop();%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D);%0D%0A
 %09%09%7D%0D

allow sound callback to be null; task#57594