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

import com.allen_sauer.gwt.voices.client.Sound;
import com.allen_sauer.gwt.voices.client.Sound.LoadState;
import com.allen_sauer.gwt.voices.client.SoundController;
import com.allen_sauer.gwt.voices.client.handler.PlaybackCompleteEvent;
import com.allen_sauer.gwt.voices.client.handler.SoundHandler;
import com.allen_sauer.gwt.voices.client.handler.SoundLoadStateChangeEvent;

import eu.ydp.empiria.player.client.controller.events.interaction.MediaInteractionSoundEventCallforward;

public class SoundExecutorSwf implements SoundExecutor {

	protected Sound currSound;
	protected SoundHandler currSoundHandler;
	protected boolean playing = false;
	protected SoundExecutorListener listener;
	
	@Override
	public void play(String src) {

		if (playing)
			stop();
		
		SoundController ctrl = new SoundController();
		currSound = ctrl.createSound(Sound.MIME_TYPE_AUDIO_MPEG_MP3, src);					
		currSoundHandler = new SoundHandler() {
			
			@Override
			public void onSoundLoadStateChange(SoundLoadStateChangeEvent event) {
				if (event.getLoadState() == LoadState.LOAD_STATE_SUPPORTED_AND_READY  ||  event.getLoadState() == LoadState.LOAD_STATE_SUPPORTED_MAYBE_READY){
					if (listener != null)
						listener.onPlay();
					playing = true;
				}
			}
			
			@Override
			public void onPlaybackComplete(PlaybackCompleteEvent event) {
				playing = false;
				listener.onSoundFinished();
			}
		};
		
		currSound.addEventHandler(currSoundHandler);
		currSound.play();

	}

	@Override
	public void stop() {
		if (playing){
			if (currSound != null)
				currSound.stop();
			if (currSoundHandler != null)
				currSoundHandler.onPlaybackComplete(null);
		}
	}

	@Override
	public void setSoundFinishedListener(SoundExecutorListener listener) {
		this.listener = listener;
	}

}

@@ -451,16 +451,147 @@
 eEvent;%0D
+%0Aimport com.google.gwt.dom.client.Document;%0D%0Aimport com.google.gwt.dom.client.Element;%0D%0Aimport com.google.gwt.dom.client.NodeList;%0D
 %0A%0D%0Aimpor
@@ -906,16 +906,57 @@
 tener;%0D%0A
+%09protected String playerPathDir = null;%0D%0A
 %09%0D%0A%09@Ove
@@ -1074,24 +1074,77 @@
 troller();%0D%0A
+%09%09ctrl.setGwtVoicesSwfLocation(getPlayerPathDir());%0D%0A
 %09%09currSound 
@@ -2125,13 +2125,675 @@
 r;%0D%0A%09%7D%0D%0A
+%09%0D%0A%09private String getPlayerPathDir()%7B%0D%0A%09%09if (playerPathDir != null)%0D%0A%09%09%09return playerPathDir;%0D%0A%09%09NodeList%3CElement%3E scriptNodes = Document.get().getElementsByTagName(%22script%22);%0D%0A%09%09String empiriaPlayerFileName = %22/empiria.player.nocache.js%22;%0D%0A%09%09for (int s = 0 ; s %3C scriptNodes.getLength() ; s ++)%7B%0D%0A%09%09%09if (((Element)scriptNodes.getItem(s)).hasAttribute(%22src%22))%7B%0D%0A%09%09%09%09String src = ((Element)scriptNodes.getItem(s)).getAttribute(%22src%22);%09%09%09%09%0D%0A%09%09%09%09if (src.endsWith(empiriaPlayerFileName))%7B%0D%0A%09%09%09%09%09playerPathDir = src.substring(0, src.indexOf(empiriaPlayerFileName) +1);%0D%0A%09%09%09%09%09return playerPathDir;%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09playerPathDir = %22%22;%0D%0A%09%09return playerPathDir;%0D%0A%09%7D
 %0D%0A%7D%0D%0A

update path to gwt-voices bug 64142