# 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 Menu"""

from omni import ui
from .doc_page import DocPage


class MenuDoc(DocPage):
    """ document for Menu"""

    def __init__(self, extension_path):
        super().__init__(extension_path)
        self._context_menu = ui.Menu(
            "Context menu",
            name="this",
            style={
                "Menu": {"background_color": 0xFFAA0000, "color": 0xFFFFFFFF, "background_selected_color": 0xFF00AA00},
                "MenuItem": {"color": 0xFFFFFFFF, "background_selected_color": 0xFF00AA00},
                "Separator": {"color": 0xFFFFFFFF},
            },
        )

        self._pushed_menu = ui.Menu(
            "Pushed menu",
            style={
                "Menu": {"background_color": 0xFF000000, "color": 0xFFFFFFFF, "background_selected_color": 0xFFAAAAAA},
                "MenuItem": {"color": 0xFFFFFFFF, "background_selected_color": 0xFFAAAAAA},
            },
        )

    def _show_context_menu(self, x, y, button, modifier):
        """The context menu example"""
        # Display context menu only if the right button is pressed
        if button != 1:
            return

        # Reset the previous context popup
        self._context_menu.clear()
        with self._context_menu:
            ui.MenuItem("Delete Shot")
            ui.Separator()
            ui.MenuItem("Attach Selected Camera")
            with ui.Menu("Sub-menu"):
                ui.MenuItem("One")
                ui.MenuItem("Two")
                ui.MenuItem("Three")
                ui.Separator()
                ui.MenuItem("Four")
                with ui.Menu("Five"):
                    ui.MenuItem("Six")
                    ui.MenuItem("Seven")

        # Show it
        self._context_menu.show()

    def _show_pushed_menu(self, x, y, button, modifier, widget):
        """The Pushed menu example"""
        # Display context menu only if the left mouse button is pressed
        if button != 0:
            return

        # Reset the previous context popup
        self._pushed_menu.clear()
        with self._pushed_menu:
            ui.MenuItem("Camera 1")
            ui.MenuItem("Camera 2")
            ui.MenuItem("Camera 3")
            ui.Separator()
            with ui.Menu("More Cameras"):
                ui.MenuItem("This Menu is Pushed")
                ui.MenuItem("and Aligned with a widget")

        # Show it
        self._pushed_menu.show_at(
            (int)(widget.screen_position_x), (int)(widget.screen_position_y + widget.computed_content_height)
        )

    def create_doc(self, navigate_to=None):
        self._section_title("Menu")
        self._text(
            "The Menu class provides a menu widget for use in menu bars, context menus, and other popup menus. It can "
            "be either a pull-down menu in a menu bar or a standalone context menu. Pull-down menus are shown by the "
            "menu bar when the user clicks on the respective item. Context menus are usually invoked by some special "
            "keyboard key or by right-clicking."
        )

        with ui.HStack(style=self._style_system):
            ui.Button("Right click to context menu", width=0, mouse_pressed_fn=self._show_context_menu)

        self._code(
            """
        def _on_startup(self):
            ....
            with ui.HStack(style=style_system):
                ui.Button(
                    "Right click to context menu",
                    width=0,
                    mouse_pressed_fn=self._show_context_menu
                )
            ....

        def _show_context_menu(self, x, y, button, modifier):
            # Display context menu only if the right button is pressed
            if button != 1:
                return

            # Reset the previous context popup
            self._context_menu.clear()
            with self._context_menu:
                with ui.Menu("Sub-menu"):
                    ui.MenuItem("One")
                    ui.MenuItem("Two")
                    ui.MenuItem("Three")
                    ui.MenuItem("Four")
                    with ui.Menu("Five"):
                        ui.MenuItem("Six")
                        ui.MenuItem("Seven")

                ui.MenuItem("Delete Shot")
                ui.MenuItem("Attach Selected Camera")
            """
        )

        with ui.HStack(style=self._style_system):
            bt = ui.Button("Pushed Button Menu", width=0, style={"background_color": 0xFF000000, "color": 0xFFFFFFFF})

            bt.set_mouse_pressed_fn(lambda x, y, b, m, widget=bt: self._show_pushed_menu(x, y, b, m, widget))

        ui.Spacer(height=100)
