# Copyright (c) 2018-2020, NVIDIA CORPORATION.  All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto.  Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
#
"""The documentation for the MenuBar"""

from omni import ui
from .doc_page import DocPage

SPACING = 5


class MenuBarDoc(DocPage):
    """ document MenuBar classes"""

    def __init__(self, extension_path):
        super().__init__(extension_path)
        self._window_menu_example = None

    def create_and_show_window_with_menu(self):
        if not self._window_menu_example:
            self._window_menu_example = ui.Window(
                "Window Menu Example",
                width=300,
                height=300,
                flags=ui.WINDOW_FLAGS_MENU_BAR | ui.WINDOW_FLAGS_NO_BACKGROUND,
            )
            menu_bar = self._window_menu_example.menu_bar
            with menu_bar:
                with ui.Menu("File"):
                    ui.MenuItem("Load")
                    ui.MenuItem("Save")
                    ui.MenuItem("Export")
                with ui.Menu("Window"):
                    ui.MenuItem("Hide")

            with self._window_menu_example.frame:
                with ui.VStack():
                    ui.Button("This Window has a Menu")

                    def show_hide_menu(menubar):
                        menubar.visible = not menubar.visible

                    ui.Button("Click here to show/hide Menu", clicked_fn=lambda m=menu_bar: show_hide_menu(m))

                    def add_menu(menubar):
                        with menubar:
                            with ui.Menu("New Menu"):
                                ui.MenuItem("I don't do anything")

                    ui.Button("Add New Menu", clicked_fn=lambda m=menu_bar: add_menu(m))

        self._window_menu_example.visible = True

    def create_doc(self, navigate_to=None):
        self._section_title("MenuBar")

        self._text(
            "All the Windows in Omni.UI can have a MenuBar. To add a MenuBar to your window add this flag to your "
            "constructor - omni.ui.Window(flags=ui.WINDOW_FLAGS_MENU_BAR). The MenuBar object can then be accessed "
            "through the menu_bar read-only property on your window\n"
            "A MenuBar is a container so it is built like a Frame or Stack but only takes Menu objects as children. "
            "You can leverage the 'priority' property on the Menu to order them. They will automatically be sorted "
            "when they are added, but if you change the priority of an item then you need to explicitly call sort()."
        )

        self._caption("MenuBar", navigate_to)
        self._text("This class is used to construct a MenuBar for the Window")

        with ui.HStack(width=0):
            ui.Button("window with MenuBar Example", width=180, clicked_fn=self.create_and_show_window_with_menu)
            ui.Label("this populates the menuBar", name="text", width=180, style={"margin_width": 10})
