package edu.fichte.pbi.ide;

import org.eclipse.e4.ui.model.application.ui.basic.MTrimBar;
import org.eclipse.e4.ui.model.application.ui.basic.MTrimElement;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.eclipse.ui.internal.WorkbenchWindow;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
	private final IWorkbenchWindowConfigurer configurer = getWindowConfigurer();

	public ApplicationWorkbenchWindowAdvisor(
			IWorkbenchWindowConfigurer configurer) {
		super(configurer);
	}

	@Override
	public ActionBarAdvisor createActionBarAdvisor(
			IActionBarConfigurer configurer) {
		return new ApplicationActionBarAdvisor(configurer);
	}

	@Override
	public void preWindowOpen() {
		Rectangle clientArea = Display.getCurrent().getClientArea();
		configurer
				.setInitialSize(new Point(clientArea.width, clientArea.height));
		configurer.setShowCoolBar(false);
		configurer.setShowStatusLine(true);
		configurer.setTitle("PBasic IDE and Interpreter");
		configurer.setShowPerspectiveBar(false);
	}

	@Override
	public void postWindowOpen() {
		hideQuickAccess();
	}
	

	private void hideQuickAccess() {
		IWorkbenchWindow window = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow();
		setQuickAccessVisible(window, false);

		// final IHandlerService service = (IHandlerService)
		// window.getService(IHandlerService.class);
		// quickAccessHandlerActivation =
		// service.activateHandler(QUICK_ACCESS_COMMAND_ID, new
		// CustomQuickAccessHandler());
	}

	private void setQuickAccessVisible(IWorkbenchWindow window, boolean visible) {
		if (window instanceof WorkbenchWindow) {
			MTrimBar topTrim = ((WorkbenchWindow) window).getTopTrim();

			for (MTrimElement element : topTrim.getChildren()) {
				// System.out.println(element.getElementId());
				if ("SearchField".equals(element.getElementId())) {
					element.setVisible(false);
				}
				// if (QUICK_ACCESS_ELEMENT_ID.equals(element.getElementId())) {
				// element.setVisible(visible);
				// if (visible) {
				// Composite control = (Composite) element.getWidget();
				// control.getChildren()[0].addFocusListener(new
				// QuickAccessFocusListener());
				// }
				// break;
				// }
			}
		}
	}
	public IWorkbenchWindowConfigurer getConfigurer(){
		return configurer;
	}

}
