package edu.fichte.pbi.ide.commands;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.HandlerEvent;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandler2;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;

import edu.fichte.pbi.ide.editors.PBIEditor;
import edu.fichte.pbi.ide.views.ConsoleView;
import edu.fichte.pbi.ide.views.DebugView;
import edu.fichte.pbi.ide.views.EEPROMView;
import edu.fichte.pbi.ide.views.IRunStateListener;
import edu.fichte.pbi.ide.views.VariableView;

public class MonitoredRunCommandHandler implements IHandler, IHandler2 {
	private List<IHandlerListener> listeners = new ArrayList<IHandlerListener>();

	@Override
	public void addHandlerListener(IHandlerListener handlerListener) {
		this.listeners.add(handlerListener);
	}

	@Override
	public void setEnabled(Object evaluationContext) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	private void fireEnabledStateChanged() {
		for (IHandlerListener iHandlerListener : listeners) {
			iHandlerListener.handlerChanged(new HandlerEvent(this, true, true));
		}
	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
		if (activeEditor instanceof PBIEditor) {
			PBIEditor pbi = (PBIEditor) activeEditor;
			final File file = pbi.getFile();
			IWorkbenchWindow window = HandlerUtil
					.getActiveWorkbenchWindow(event);
			final Display display = window.getShell().getDisplay();

			try {
//				IViewPart view = window.getActivePage().findView(HelpView.ID);
//				if (view != null) {
//					window.getActivePage().hideView(view);					
//				}
				final VariableView varView = (VariableView) window
						.getActivePage().showView(VariableView.ID);
				final ConsoleView console = (ConsoleView) window
						.getActivePage().showView(ConsoleView.ID);
				final EEPROMView eeprom = (EEPROMView) window.getActivePage()
						.showView(EEPROMView.ID);
				final DebugView debugView = (DebugView) window
						.getActivePage().showView(DebugView.ID);
				console.run(file, eeprom, debugView, varView,
						new IRunStateListener() {
					@Override
					public void runStateChanged(boolean isRunning) {
						RunCommandHandler.somethingIsRunning = isRunning;
						display.asyncExec(new Runnable() {

							@Override
							public void run() {
								fireEnabledStateChanged();
							}
						});
					}
				});
			} catch (PartInitException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public boolean isEnabled() {
		return RunCommandHandler.enabled();
	}

	@Override
	public boolean isHandled() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void removeHandlerListener(IHandlerListener handlerListener) {
		// TODO Auto-generated method stub

	}

}
