package de.renew.draw.ui.impl.services;

import java.awt.Dialog;
import java.awt.Frame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import CH.ifa.draw.DrawPlugin;
import CH.ifa.draw.application.MenuManager;
import de.renew.draw.ui.api.services.MenuService;
import de.renew.draw.ui.impl.menus.internal.MenuHelper;
import de.renew.draw.ui.ontology.MenuSectionPosition;
import de.renew.draw.ui.ontology.MenuSeparatorFactory;

/**
 * Contains the menu functionalities for the menu service.
 * It acts as a facade by not providing the functionality itself but redirecting its calls to the {@link MenuManager}.
 *
 * @author Clara von Bargen
 */
public class MenuServiceImpl implements MenuService {

    /**
     * Constructs a new MenuApi.
     */
    public MenuServiceImpl() {}

    @Override
    public void registerMenu(String parentMenu, JMenuItem menuToRegister) {
        MenuManager.getInstance().registerMenu(parentMenu, menuToRegister);
    }

    @Override
    public void registerMenu(String parentMenu, JMenuItem menuToRegister, String id) {
        MenuManager.getInstance().registerMenu(parentMenu, menuToRegister, id);
    }

    @Override
    public void registerMenu(
        String parentMenu, JMenuItem menuToRegister, int requestedSection,
        MenuSectionPosition itemPosition)
    {
        int where = switch (itemPosition) {
            case BEGINNING -> MenuManager.AT_BEGIN_SECTION;
            case END -> MenuManager.AT_END_SECTION;
            case MIDDLE -> MenuManager.SECTION_MIDDLE;
        };
        MenuManager.getInstance().registerMenu(parentMenu, menuToRegister, requestedSection, where);
    }

    @Override
    public void unregisterMenu(JMenuItem menuToUnregister) {
        MenuManager.getInstance().unregisterMenu(menuToUnregister);
    }

    @Override
    public void unregisterMenu(String id) {
        MenuManager.getInstance().unregisterMenu(id);
    }

    @Override
    public JMenu createTopLevelMenu(String parentMenu, int section) {
        return MenuManager.getInstance().createTopLevelMenu(parentMenu, section);
    }

    @Override
    public JMenuItem createMenuItem(String name, int shortcut, Runnable action) {
        return MenuHelper.createMenuItem(name, shortcut, action);
    }

    @Override
    public JMenuItem createMenuItem(String name, Runnable action) {
        return MenuHelper.createMenuItem(name, action);
    }

    @Override
    public MenuSeparatorFactory createMenuSeparatorFactory(String id) {
        return new MenuManager.SeparatorFactory(id);
    }

    @Override
    public JMenuItem createMenuSeparator(String id) {
        return MenuManager.createSeparator(id);
    }

    @Override
    public String getMenuNamePluginsMenu() {
        return DrawPlugin.PLUGINS_MENU;
    }

    @Override
    public String getMenuNamePlatformsMenu() {
        return DrawPlugin.PLATFORMS_MENU;
    }

    @Override
    public String getMenuNameFileMenu() {
        return DrawPlugin.FILE_MENU;
    }

    @Override
    public String getMenuNameEditMenu() {
        return DrawPlugin.EDIT_MENU;
    }

    @Override
    public String getMenuNameAttributesMenu() {
        return DrawPlugin.ATTRIBUTES_MENU;
    }

    @Override
    public String getMenuNameLayoutMenu() {
        return DrawPlugin.LAYOUT_MENU;
    }

    @Override
    public String getMenuNameToolsMenu() {
        return DrawPlugin.TOOLS_MENU;
    }

    @Override
    public String getMenuNamePaoseMenu() {
        return DrawPlugin.PAOSE_MENU;
    }

    @Override
    public String getMenuNameHelpMenu() {
        return DrawPlugin.HELP_MENU;
    }

    @Override
    public String getWindowsCategoryNameTools() {
        return DrawPlugin.WINDOWS_CATEGORY_TOOLS;
    }

    @Override
    public void registerWindowsMenuDialog(String category, Dialog dialog) {
        MenuManager.getInstance().getWindowsMenu().addDialog(category, dialog);
    }

    @Override
    public void unregisterWindowsMenuDialog(Dialog dialog) {
        MenuManager.getInstance().getWindowsMenu().removeDialog(dialog);

    }

    @Override
    public void registerWindowsMenuFrame(String category, Frame frame) {
        MenuManager.getInstance().getWindowsMenu().addFrame(category, frame);
    }

    @Override
    public void unregisterWindowsMenuFrame(Frame frame) {
        MenuManager.getInstance().getWindowsMenu().removeFrame(frame);
    }

    @Override
    public void registerWindowsMenuItem(JMenuItem menuItem) {
        MenuManager.getInstance().getWindowsMenu().add(menuItem);
    }

    @Override
    public void unregisterWindowsMenuItem(JMenuItem menuItem) {
        MenuManager.getInstance().getWindowsMenu().remove(menuItem);
    }
}
