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.PlatformUI;
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.IRunStateListener;

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

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

	private void fireEnabledStateChanged(){
		for (IHandlerListener IHandlerListener : listeners) {
			IHandlerListener.handlerChanged(new HandlerEvent(this, true, true));
		}
	}
	
	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}
	
	public static boolean somethingIsRunning = false;

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		if(somethingIsRunning) return null;
		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 {
				final ConsoleView console = (ConsoleView) window
						.getActivePage().showView(ConsoleView.ID);
				final DebugView debugView = (DebugView) window
						.getActivePage().showView(DebugView.ID);
				console.run(file,null,debugView,null, new IRunStateListener(){
					@Override
					public void runStateChanged(boolean isRunning) {
						// TODO das enabling unenabling funktioniert nicht richtig
						somethingIsRunning = isRunning;
						display.asyncExec(new Runnable(){

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

	@Override
	public boolean isEnabled() {
		return enabled();
	}
	
	public static boolean enabled(){
		return (PlatformUI.getWorkbench().getActiveWorkbenchWindow()
				.getActivePage().getActiveEditor() instanceof PBIEditor) && !somethingIsRunning;
	}

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

	@Override
	public void removeHandlerListener(IHandlerListener handlerListener) {
		listeners.remove(handlerListener);
	}

	@Override
	public void setEnabled(Object evaluationContext) {
		
	}

}
