# Copyright (c) 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.
#
import os, sys
import carb
import omni.kit.ui
import weakref
from functools import partial
from pxr import Usd, Sdf, UsdShade, UsdGeom, UsdLux
from .prim_selection_payload import PrimSelectionPayload


class ContextMenuEvent:
    """The object comatible with ContextMenu"""

    def __init__(self, payload: PrimSelectionPayload, menu_items: list, xpos: int, ypos: int):
        self.menu_items = menu_items
        self.payload = payload
        self.type = 0
        self.xpos = xpos
        self.ypos = ypos


class ContextMenu:
    def __init__(self):
        pass

    def on_mouse_event(self, event: ContextMenuEvent):
        # check its expected event
        if event.type != int(omni.kit.ui.MenuEventType.ACTIVATE):
            return

        # setup objects, this is passed to all functions
        objects = {
            "stage": event.payload.get_stage(),
            "prim_list": event.payload._payload,
            "menu_xpos": event.xpos,
            "menu_ypos": event.ypos,
        }

        menu_list = []

        for item in event.menu_items:
            name = item.path
            parts = item.path.split("/")
            if len(parts) < 2:
                menu_list.append(item.get_dict(event.payload))
            else:
                last_name = parts.pop()
                first_name = parts.pop(0)

                menu_sublist = None
                for menu_item in menu_list:
                    if first_name in menu_item["name"]:
                        menu_sublist = menu_item["name"][first_name]
                        break

                if menu_sublist is None:
                    menu_list.append({"glyph": item.glyph, "name": {first_name: []}})
                    menu_sublist = menu_list[-1]["name"][first_name]

                for part in parts:
                    sublist = None
                    for menu_item in menu_sublist:
                        if part in menu_item["name"]:
                            sublist = menu_item["name"][part]
                            break

                    if sublist is None:
                        menu_sublist.append({"glyph": item.glyph, "name": {part: []}})
                        menu_sublist = menu_sublist[-1]["name"][part]
                    else:
                        menu_sublist = sublist

                menu_sublist.append(item.get_dict(event.payload, last_name))

        # show menu
        self.show_context_menu(objects=objects, menu_list=menu_list)

    def show_context_menu(self, objects: dict = {}, menu_list: list = []):
        # get context menu core functionality & check its enabled
        context_menu = omni.kit.context_menu.get_instance()
        if context_menu is None:
            carb.log_warn("context_menu is disabled!")
            return None
        context_menu.show_context_menu("prim_path_widget", objects, menu_list)
