package eu.ydp.empiria.player.client.module.media.button;

import com.google.gwt.xml.client.Element;

import eu.ydp.empiria.player.client.PlayerGinjector;
import eu.ydp.empiria.player.client.module.media.MediaWrapper;
import eu.ydp.empiria.player.client.module.media.fullscreen.VideoFullScreenHelper;
import eu.ydp.empiria.player.client.util.events.bus.EventsBus;
import eu.ydp.empiria.player.client.util.events.media.MediaEvent;
import eu.ydp.empiria.player.client.util.events.media.MediaEventHandler;
import eu.ydp.empiria.player.client.util.events.media.MediaEventTypes;
import eu.ydp.empiria.player.client.util.events.scope.CurrentPageScope;

/**
 * Przycisk przelaczania pomiedzy trybem pelnoekranowym a zwyklym
 *
 *
 *
 */
public class FullScreenMediaButton extends AbstractMediaButton<FullScreenMediaButton> implements MediaEventHandler {
	private final VideoFullScreenHelper fullScreenHelper = PlayerGinjector.INSTANCE.getVideoFullScreenHelper();
	protected final EventsBus eventsBus = PlayerGinjector.INSTANCE.getEventsBus();
	private boolean isInfullScreen = false;
	private Element fullScreenTemplate;
	private MediaWrapper<?> fullScreenMediaWrapper;

	public FullScreenMediaButton() {
		super(PlayerGinjector.INSTANCE.getStyleNameConstants().QP_MEDIA_FULLSCREEN_BUTTON());
	}

	public void setFullScreenMediaWrapper(MediaWrapper<?> fullScreenMediaWrapper) {
		this.fullScreenMediaWrapper = fullScreenMediaWrapper;
	}

	@Override
	public FullScreenMediaButton getNewInstance() {
		return new FullScreenMediaButton();
	}

	@Override
	protected void onClick() {
		if (isInfullScreen || fullScreen) {
			fullScreenHelper.closeFullScreen();
		} else if(!fullScreen){
			fullScreenHelper.openFullScreen(fullScreenMediaWrapper, fullScreenTemplate);
		}
	}

	@Override
	public void init() {
		super.init();
		eventsBus.addHandler(MediaEvent.getType(MediaEventTypes.ON_FULL_SCREEN_EXIT), this, new CurrentPageScope());
		eventsBus.addHandlerToSource(MediaEvent.getType(MediaEventTypes.ON_FULL_SCREEN_OPEN), getMediaWrapper(), this, new CurrentPageScope());
	}

	@Override
	public boolean isSupported() {
		return getMediaAvailableOptions().isFullScreenSupported();
	}

	@Override
	public void onMediaEvent(MediaEvent event) {
		if(event.getType() == MediaEventTypes.ON_FULL_SCREEN_OPEN){
			isInfullScreen = true;
		}else if(event.getType() == MediaEventTypes.ON_FULL_SCREEN_EXIT){
			isInfullScreen = false;
		}

	}

	public void setFullScreenTemplate(Element fullScreenTemplate) {
		this.fullScreenTemplate = fullScreenTemplate;
	}

}

@@ -1653,16 +1653,17 @@
  else if
+ 
 (!fullSc
@@ -1667,16 +1667,17 @@
 lScreen)
+ 
 %7B%0A%09%09%09ful
@@ -2162,16 +2162,46 @@
 ported()
+ && fullScreenTemplate != null
 ;%0A%09%7D%0A%0A%09@
@@ -2259,16 +2259,17 @@
 ) %7B%0A%09%09if
+ 
 (event.g
@@ -2316,16 +2316,17 @@
 EN_OPEN)
+ 
 %7B%0A%09%09%09isI
@@ -2348,23 +2348,25 @@
 rue;%0A%09%09%7D
+ 
 else if
+ 
 (event.g
@@ -2413,16 +2413,17 @@
 EN_EXIT)
+ 
 %7B%0A%09%09%09isI

correct issupported to check is fullscreen template attached bug 71995