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 com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;

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;
	protected String playerPathDir = null;
	
	@Override
	public void play(String src) {

		if (playing)
			stop();
		
		SoundController ctrl = new SoundController();
		ctrl.setGwtVoicesSwfLocation(getPlayerPathDir());
		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;
	}
	
	private String getPlayerPathDir(){
		if (playerPathDir != null)
			return playerPathDir;
		NodeList<Element> scriptNodes = Document.get().getElementsByTagName("script");
		String empiriaPlayerFileName = "/empiria.player.nocache.js";
		for (int s = 0 ; s < scriptNodes.getLength() ; s ++){
			if (((Element)scriptNodes.getItem(s)).hasAttribute("src")){
				String src = ((Element)scriptNodes.getItem(s)).getAttribute("src");				
				if (src.endsWith(empiriaPlayerFileName)){
					playerPathDir = src.substring(0, src.indexOf(empiriaPlayerFileName) +1);
					return playerPathDir;
				}
			}
		}
		playerPathDir = "";
		return playerPathDir;
	}
}

@@ -2221,16 +2221,126 @@
 thDir;%0D%0A
+%09%09playerPathDir = findPlayerPathDir();%0D%0A%09%09return playerPathDir;%0D%0A%09%7D%0D%0A%09%0D%0A%09private String findPlayerPathDir()%7B%0D%0A
 %09%09NodeLi
@@ -2727,31 +2727,22 @@
 %7B%0D%0A%09%09%09%09%09
-playerPathDir =
+return
  src.sub
@@ -2800,96 +2800,38 @@
 %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;
+%7D%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09return %22%22;%0D%0A%09%09
 %0D%0A%09%7D

update path to gwt-voices bug 64142