package org.minimalj.frontend.vaadin;

import java.util.List;

import org.minimalj.application.Application;
import org.minimalj.frontend.page.ActionGroup;
import org.minimalj.frontend.page.Page;
import org.minimalj.frontend.page.Separator;
import org.minimalj.frontend.toolkit.Action;
import org.minimalj.frontend.toolkit.Action.ActionChangeListener;
import org.minimalj.frontend.vaadin.toolkit.VaadinClientToolkit;
import org.minimalj.util.resources.Resources;

import com.vaadin.ui.MenuBar;

public class VaadinMenuBar extends MenuBar {
	private static final long serialVersionUID = 1L;

	private final VaadinWindow vaadinWindow;
	
	public VaadinMenuBar(VaadinWindow vaadinWindow) {
		this.vaadinWindow = vaadinWindow;
	}
	
	public void updateMenu() {
		removeItems();
		
		createFileMenu();
		createViewMenu();
		createObjectMenu();
	}
	
	private void createFileMenu() {
		MenuBar.MenuItem menu = menu("file");

		boolean separator = false;
		List<Action> actionsNew = Application.getApplication().getMenu();
//		if (!actionsNew.isEmpty()) {
//			addActions(menu, "new", actionsNew);
//			separator = true;
//		}
//		List<Action> actionsImport = Application.getApplication().getActionImport();
//		List<Action> actionsExport = Application.getApplication().getActionExport();
//		if (!actionsImport.isEmpty() || !actionsExport.isEmpty()) {
//			if (separator) menu.addSeparator();
//		}
//		if (!actionsImport.isEmpty()) addActions(menu, "import", actionsImport);
//		if (!actionsExport.isEmpty()) addActions(menu, "export", actionsExport);
	}
	
	private void createViewMenu() {
//		List<Action> actionsView = Application.getApplication().getActionView();
//		if (!actionsView.isEmpty()) {
			MenuBar.MenuItem menu = menu("view");
//			addActions(menu, actionsView);
//		}
	}
	
	private void createObjectMenu() {
		Page visiblePage = vaadinWindow.getVisiblePage();
		List<Action> actions = visiblePage.getActions();
		if (actions != null && actions.size() > 0) {
			MenuBar.MenuItem menu = addItem("Actions", null);
			addActions(menu, actions);
		}
	}

	//
	
	private MenuBar.MenuItem menu(String resourceName) {
		MenuBar.MenuItem menu = addItem(Resources.getString("Menu." + resourceName), null);
		return menu;
	}

	private MenuBar.MenuItem menu(MenuBar.MenuItem menu, String resourceName) {
		return menu.addItem(Resources.getString("Menu." + resourceName), null);
	}

	private void addActions(MenuBar.MenuItem menu, String type, List<Action> actions) {
		MenuBar.MenuItem subMenu = menu(menu, type);
		addActions(subMenu, actions);
	}
	
	private void addActions(MenuBar.MenuItem menu, List<Action> actions) {
		for (Action action : actions) {
			if (action instanceof org.minimalj.frontend.page.ActionGroup) {
				ActionGroup actionGroup = (ActionGroup) action;
				MenuBar.MenuItem subMenu = menu.addItem(action.getName(), null);
				addActions(subMenu, actionGroup.getItems());
			} else if (action instanceof Separator) {
				menu.addSeparator();
			} else {
				MenuItem menuItem = menu.addItem(action.getName(), new ActionCommand(action));
				updateMenuItem(menuItem, action);
				installAdditionalActionListener(action, menuItem);
			}
		}
	}
	
	private class ActionCommand implements Command {
		private static final long serialVersionUID = 1L;
		private final Action action;
		
		public ActionCommand(Action action) {
			this.action = action;
		}

		@Override
		public void menuSelected(MenuItem selectedItem) {
			VaadinClientToolkit.setWindow(vaadinWindow);
			action.action();
			VaadinClientToolkit.setWindow(null);
		}
	}
	
	private static void installAdditionalActionListener(final Action action, final MenuBar.MenuItem menuItem) {
		action.setChangeListener(new ActionChangeListener() {
			@Override
			public void change() {
				updateMenuItem(menuItem, action);
			}
		});
	}

	private static void updateMenuItem(final MenuBar.MenuItem menuItem, final Action action) {
		menuItem.setEnabled(action.isEnabled());
		menuItem.setText(action.getName());
		menuItem.setDescription(action.getDescription());
	}

}

@@ -779,32 +779,8 @@
 eate
-FileMenu();%0A%09%09createView
 Menu
@@ -834,12 +834,8 @@
 eate
-File
 Menu
@@ -845,906 +845,155 @@
 %7B%0A%09%09
-MenuBar.MenuItem menu = menu(%22file%22);%0A%0A%09%09boolean separator = false;%0A%09%09List%3CAction%3E actionsNew = Application.getApplication().getMenu();%0A//%09%09if (!actionsNew.isEmpty()) %7B%0A//%09%09%09addActions(menu, %22new%22, actionsNew);%0A//%09%09%09separator = true;%0A//%09%09%7D%0A//%09%09List%3CAction%3E actionsImport = Application.getApplication().getActionImport();%0A//%09%09List%3CAction%3E actionsExport = Application.getApplication().getActionExport();%0A//%09%09if (!actionsImport.isEmpty() %7C%7C !actionsExport.isEmpty()) %7B%0A//%09%09%09if (separator) menu.addSeparator();%0A//%09%09%7D%0A//%09%09if (!actionsImport.isEmpty()) addActions(menu, %22import%22, actionsImport);%0A//%09%09if (!actionsExport.isEmpty()) addActions(menu, %22export%22, actionsExport);%0A%09%7D%0A%09%0A%09private void createViewMenu() %7B%0A//%09%09List%3CAction%3E actionsView = Application.getApplication().getActionView();%0A//%09%09if (!actionsView.isEmpty()) %7B%0A%09%09%09MenuBar.MenuItem 
+List%3CAction%3E menuActions = Application.getApplication().getMenu();%0A%0A%09%09MenuBar.MenuItem menu = menu(%22application%22);%0A%09%09addActions(
 menu
- =
+,
  menu
-(%22view%22);%0A//%09%09%09addActions(menu, actionsView);%0A//%09%09%7D
+Actions);
 %0A%09%7D%0A
@@ -1210,31 +1210,19 @@
 u = 
-addItem(%22Actions%22, null
+menu(%22page%22
 );%0A%09
@@ -1262,14 +1262,8 @@
 %09%7D%0A%0A
-%09//%0A%09%0A
 %09pri
@@ -1421,332 +1421,8 @@
 %09%7D%0A%0A
-%09private MenuBar.MenuItem menu(MenuBar.MenuItem menu, String resourceName) %7B%0A%09%09return menu.addItem(Resources.getString(%22Menu.%22 + resourceName), null);%0A%09%7D%0A%0A%09private void addActions(MenuBar.MenuItem menu, String type, List%3CAction%3E actions) %7B%0A%09%09MenuBar.MenuItem subMenu = menu(menu, type);%0A%09%09addActions(subMenu, actions);%0A%09%7D%0A%09%0A
 %09pri
@@ -2024,32 +2024,38 @@
 ;%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%09%0A
+%09//%0A%09%0A
 %09private class A

clean up menu / page actions