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

import static eu.ydp.empiria.player.client.util.events.state.StateChangeEventTypes.OUTCOME_STATE_CHANGED;

import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.xml.client.Element;
import com.google.inject.Inject;

import eu.ydp.empiria.player.client.controller.extensions.internal.tutor.TutorConfig;
import eu.ydp.empiria.player.client.gin.factory.PageScopeFactory;
import eu.ydp.empiria.player.client.gin.scopes.module.ModuleScoped;
import eu.ydp.empiria.player.client.module.SimpleModuleBase;
import eu.ydp.empiria.player.client.module.tutor.presenter.TutorPresenter;
import eu.ydp.empiria.player.client.module.tutor.view.TutorView;
import eu.ydp.empiria.player.client.util.events.bus.EventsBus;
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;
import eu.ydp.empiria.player.client.util.events.state.StateChangeEvent;
import eu.ydp.empiria.player.client.util.events.state.StateChangeEventHandler;

public class TutorModule extends SimpleModuleBase {

	@Inject private EventsBus eventsBus;
	@Inject private PageScopeFactory eventScopeFactory;
	@Inject @ModuleScoped private TutorPresenter presenter;
	@Inject @ModuleScoped private TutorView view;
	@Inject @ModuleScoped private ActionEventGenerator eventGenerator;
	@Inject @ModuleScoped private TutorConfig config;
	
	@Override
	protected void initModule(Element element) {
		addHandlers();
		init();
	}

	private void init() {
		presenter.init();
	}

	private void addHandlers() {
		final CurrentPageScope modulePageScope = eventScopeFactory.getCurrentPageScope();
		eventsBus.addHandler(StateChangeEvent.getType(OUTCOME_STATE_CHANGED), new StateChangeEventHandler() {
			
			@Override
			public void onStateChange(StateChangeEvent event) {
				eventGenerator.stateChanged();
			}
		}, modulePageScope);
		eventsBus.addHandler(PlayerEvent.getType(PlayerEventTypes.PAGE_UNLOADED), new PlayerEventHandler() {
			
			@Override
			public void onPlayerEvent(PlayerEvent event) {
				eventGenerator.stop();
			}
		}, modulePageScope);
		eventsBus.addHandler(PlayerEvent.getType(PlayerEventTypes.TEST_PAGE_LOADED), new PlayerEventHandler() {
			
			@Override
			public void onPlayerEvent(PlayerEvent event) {
				eventGenerator.start();
			}

		}, modulePageScope);
	}
	
	@Override
	public Widget getView() {
		return view.asWidget();
	}

}

@@ -1593,24 +1593,619 @@
 config;%0D%0A%09%0D%0A
+%09private final PlayerEventHandler testPageLoadedHandler = new PlayerEventHandler() %7B%0D%0A%09%09%0D%0A%09%09@Override%0D%0A%09%09public void onPlayerEvent(PlayerEvent event) %7B%0D%0A%09%09%09eventGenerator.start();%0D%0A%09%09%7D%0D%0A%0D%0A%09%7D;%0D%0A%09private final PlayerEventHandler pageUnloadedhandler = new PlayerEventHandler() %7B%0D%0A%09%09%0D%0A%09%09@Override%0D%0A%09%09public void onPlayerEvent(PlayerEvent event) %7B%0D%0A%09%09%09eventGenerator.stop();%0D%0A%09%09%7D%0D%0A%09%7D;%0D%0A%09private final StateChangeEventHandler stateChangedHandler = new StateChangeEventHandler() %7B%0D%0A%09%09%0D%0A%09%09@Override%0D%0A%09%09public void onStateChange(StateChangeEvent event) %7B%0D%0A%09%09%09eventGenerator.stateChanged();%0D%0A%09%09%7D%0D%0A%09%7D;%0D%0A%09%0D%0A
 %09@Override%0D%0A
@@ -2525,21 +2525,17 @@
 ANGED), 
-new S
+s
 tateChan
@@ -2540,616 +2540,254 @@
 ange
-Event
+d
 Handler
-() %7B%0D%0A%09%09%09%0D%0A%09%09%09@Override%0D%0A%09%09%09public void onStateChange(StateChangeEvent event) %7B%0D%0A%09%09%09%09eventGenerator.stateChanged();%0D%0A%09%09%09%7D%0D%0A%09%09%7D, modulePageScope);%0D%0A%09%09eventsBus.addHandler(PlayerEvent.getType(PlayerEventTypes.PAGE_UNLOADED), new PlayerEventHandler() %7B%0D%0A%09%09%09%0D%0A%09%09%09@Override%0D%0A%09%09%09public void onPlayerEvent(PlayerEvent event) %7B%0D%0A%09%09%09%09eventGenerator.stop();%0D%0A%09%09%09%7D%0D%0A%09%09%7D, modulePageScope);%0D%0A%09%09eventsBus.addHandler(PlayerEvent.getType(PlayerEventTypes.TEST_PAGE_LOADED), new PlayerEventHandler() %7B%0D%0A%09%09%09%0D%0A%09%09%09@Override%0D%0A%09%09%09public void onPlayerEvent(PlayerEvent event) %7B%0D%0A%09%09%09%09eventGenerator.start();%0D%0A%09%09%09%7D%0D%0A%0D%0A%09%09%7D
+, modulePageScope);%0D%0A%09%09eventsBus.addHandler(PlayerEvent.getType(PlayerEventTypes.PAGE_UNLOADED), pageUnloadedhandler , modulePageScope);%0D%0A%09%09eventsBus.addHandler(PlayerEvent.getType(PlayerEventTypes.TEST_PAGE_LOADED), testPageLoadedHandler
 , mo

refactor handlers issue YPUB-5504