package edu.fichte.pbi.ide.commands;

import java.io.File;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandler2;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;
import org.osgi.framework.Bundle;

import edu.fichte.pbi.ide.Activator;
import edu.fichte.pbi.ide.editors.ConfigEditor;
import edu.fichte.pbi.ide.editors.FileSystemFileEditorInput;
import edu.fichte.pbi.ide.views.HelpView;

public class CASettingsCommandHandler implements IHandler,IHandler2{
	protected String filename = "ca.cfg";

	

	@Override
	public void setEnabled(Object evaluationContext) {

	}

	@Override
	public void addHandlerListener(IHandlerListener handlerListener) {

	}

	@Override
	public void dispose() {

	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		Bundle bundle = Activator.getDefault().getBundle();
		File targetRoot = bundle.getDataFile("");
		File file = new File(targetRoot, "config/" + filename);
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
		if (!file.canRead()) {
			// must copy the html from the bundle .jar to the data file location
			HelpView.copyPathFromBundleToFSRecursive(bundle, "config",
					targetRoot);
		}
		if (file.isFile()) {
			FileSystemFileEditorInput fileSystemFileEditorInput = new FileSystemFileEditorInput(
					file);
			try {
				window.getActivePage().openEditor(fileSystemFileEditorInput,
						ConfigEditor.ID);
			} catch (PartInitException e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("something went wrong");
		}

		return null;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	@Override
	public boolean isHandled() {
		return true;
	}

	@Override
	public void removeHandlerListener(IHandlerListener handlerListener) {

	}
}
