package eu.ydp.empiria.player.client.module.simple.audioplayer;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.xml.client.Element;

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.module.ISimpleModule;
import eu.ydp.empiria.player.client.module.ModuleSocket;
import eu.ydp.empiria.player.client.module.listener.MediaModuleInteractionListener;
import eu.ydp.empiria.player.client.module.listener.ModuleInteractionListener;
import eu.ydp.empiria.player.client.util.xml.XMLUtils;

public class AudioPlayerModule implements ISimpleModule {
	
	public AudioPlayerModule(){
		button = new PushButton();
	}
	
	protected PushButton button;
	protected String address;
	protected MediaModuleInteractionListener mediaListener;
	protected boolean playing;
	
	protected MediaInteractionSoundEventCallforward callforward;

	@Override
	public void initModule(Element element, ModuleSocket ms, ModuleInteractionListener mil) {

		mediaListener = mil;
		
		address = XMLUtils.getAttributeAsString(element, "src");
		if (address == null  ||  "".equals(address))
			address = XMLUtils.getAttributeAsString(element, "data");
		
		playing = false;
		
		button.setStyleName("qp-audioplayer-button");
		button.getElement().setId(com.google.gwt.dom.client.Document.get().createUniqueId());
		
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				onButtonClick();
			}
		});
	}

	@Override
	public Widget getView() {
		return button;
	}
	
	private void play(){
		mediaListener.onMediaSoundPlay(address, new MediaInteractionSoundEventCallback() {
			
			@Override
			public void onStop() {
				setStopped();
			}
			
			@Override
			public void onPlay() {
				setPlaying();
			}

			@Override
			public void setCallforward(MediaInteractionSoundEventCallforward cf) {
				callforward = cf;
			}
		});
	}
	
	protected void stop(){
		if (callforward != null)
			callforward.stop();
	}
	
	protected void setPlaying(){
		playing = true;
		button.setStyleName("qp-audioplayer-button-playing");
	}
	
	
	protected void setStopped(){
		playing = false;
		button.setStyleName("qp-audioplayer-button");	
	}

	
	protected void onButtonClick() {
		if (!playing)
			play();
		else
			stop();
		
	}

}

@@ -1131,19 +1131,55 @@
 aying;%0D%0A
+%09protected boolean enabled = true;%0D%0A
 %09%0D%0A
-
 %09protect
@@ -1943,16 +1943,36 @@
 lay()%7B%0D%0A
+%09%09enabled = false;%0D%0A
 %09%09mediaL
@@ -2350,16 +2350,36 @@
 top()%7B%0D%0A
+%09%09enabled = false;%0D%0A
 %09%09if (ca
@@ -2456,24 +2456,43 @@
 Playing()%7B%0D%0A
+%09%09enabled = true;%0D%0A
 %09%09playing = 
@@ -2596,16 +2596,35 @@
 ped()%7B%0D%0A
+%09%09enabled = true;%0D%0A
 %09%09playin
@@ -2729,16 +2729,34 @@
 ck() %7B%0D%0A
+%09%09if (enabled)%7B%0D%0A%09
 %09%09if (!p
@@ -2767,16 +2767,17 @@
 ng)%0D%0A%09%09%09
+%09
 play();%0D
@@ -2777,16 +2777,17 @@
 lay();%0D%0A
+%09
 %09%09else%0D%0A
@@ -2789,16 +2789,17 @@
 lse%0D%0A%09%09%09
+%09
 stop();%0D
@@ -2801,16 +2801,17 @@
 p();%0D%0A%09%09
+%7D
 %0D%0A%09%7D%0D%0A%0D%0A

set button disabled between play/onPlay (and stop/onStop) task#57594