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

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
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.delivery.DeliveryEvent;
import eu.ydp.empiria.player.client.module.ControlModule;
import eu.ydp.empiria.player.client.module.ISimpleModule;
import eu.ydp.empiria.player.client.util.events.player.PlayerEvent;
import eu.ydp.empiria.player.client.util.events.player.PlayerEventHandler;
import eu.ydp.empiria.player.client.util.events.player.PlayerEventTypes;
import eu.ydp.empiria.player.client.util.events.scope.CurrentPageScope;

public class NavigationButtonModule extends ControlModule implements ISimpleModule, PlayerEventHandler {

	private PushButton button;
	private boolean enabled = true;
	private final NavigationButtonDirection direction;

	public NavigationButtonModule(NavigationButtonDirection dir) {
		direction = dir;
	}

	@Override
	public void initModule(Element element) {
		eventsBus.addHandler(PlayerEvent.getTypes(PlayerEventTypes.PAGE_LOADED, PlayerEventTypes.BEFORE_FLOW, PlayerEventTypes.PAGE_CHANGE), this, new CurrentPageScope());
	}

	private boolean isFirstPage(){
		return (flowDataSupplier.getCurrentPageIndex() == 0);
	}
	private boolean isEnd() {
		boolean retValue = false;
		if (direction.equals(NavigationButtonDirection.PREVIOUS)) {
			retValue = isFirstPage();
		} else if (direction.equals(NavigationButtonDirection.NEXT)) {
			retValue = isLastPage();
		}
		return retValue;
	}

	private void setStyleName(){
		String currentStyleName = getCurrentStyleName(isEnabled());
		if (currentStyleName != null) {
			button.setStylePrimaryName(currentStyleName);
		}
	}

	@Override
	public void onDeliveryEvent(DeliveryEvent flowEvent) {
		switch (flowEvent.getType()) {
		case ASSESSMENT_STARTED:
			setEnabled(!isEnd());
		case TEST_PAGE_LOADED:
			setStyleName();
			break;
		default:
			break;
		}
	}

	public void setEnabled(boolean isEnabled) {
		this.enabled = isEnabled;
	}

	public boolean isEnabled() {
		return enabled;
	}

	@Override
	public Widget getView() {
		if (button == null) {
			button = new PushButton();
			button.setStyleName(getStyleName());
			button.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					if (isEnabled() && !isEnd()) {
						flowRequestInvoker.invokeRequest(NavigationButtonDirection.getRequest(direction));
					}
				}
			});
		}

		return button;
	}

	private String getStyleName() {
		return "qp-" + NavigationButtonDirection.getName(direction) + "-button";
	}

	private Boolean isLastPage() {
		return (flowDataSupplier.getCurrentPageIndex() == dataSourceSupplier.getItemsCount() - 1);
	}

	private String getCurrentStyleName(Boolean isEnabled) {
		String styleName = null;

		if (isEnabled) {
			styleName = getStyleName();
		} else {
			styleName = getStyleName() + "-disabled";
		}

		return styleName;
	}

	@Override
	public void onPlayerEvent(PlayerEvent event) {
		if (event.getType() == PlayerEventTypes.PAGE_LOADED) {
			Timer timer = new Timer() {
				@Override
				public void run() {
					setEnabled(true && !isEnd());
					setStyleName();
				}
			};
			timer.schedule(300);
		} else if (event.getType() == PlayerEventTypes.BEFORE_FLOW) {
			setEnabled(false);
			setStyleName();
		}

	}
}

@@ -44,24 +44,305 @@
 .button;%0D%0A%0D%0A
+import static eu.ydp.empiria.player.client.util.events.player.PlayerEventTypes.BEFORE_FLOW;%0D%0Aimport static eu.ydp.empiria.player.client.util.events.player.PlayerEventTypes.PAGE_CHANGE;%0D%0Aimport static eu.ydp.empiria.player.client.util.events.player.PlayerEventTypes.PAGE_LOADED;%0D%0A%0D%0A
 import com.g
@@ -377,24 +377,24 @@
 lickEvent;%0D%0A
-
 import com.g
@@ -964,82 +964,8 @@
 r;%0D%0A
-import eu.ydp.empiria.player.client.util.events.player.PlayerEventTypes;%0D%0A
 impo
@@ -1457,84 +1457,33 @@
 es(P
-layerEventTypes.PAGE_LOADED, PlayerEventTypes.BEFORE_FLOW, PlayerEventTypes.
+AGE_LOADED, BEFORE_FLOW, 
 PAGE
@@ -2217,24 +2217,79 @@
 T_STARTED:%0D%0A
+%09%09case CONTINUE:%0D%0A%09%09case CHECK:%0D%0A%09%09case SHOW_ANSWERS:%0D%0A
 %09%09%09setEnable
@@ -3525,33 +3525,16 @@
 pe() == 
-PlayerEventTypes.
 PAGE_LOA
@@ -3706,24 +3706,24 @@
 dule(300);%0D%0A
+
 %09%09%7D else if 
@@ -3742,33 +3742,16 @@
 pe() == 
-PlayerEventTypes.
 BEFORE_F

Remove navigation block when Check, ShowAnswer event occurs bug 70292