# Copyright (c) 2022, 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.
#
from typing import Any

import omni.kit.commands
import omni.kit.undo
import omni.usd


class GroupHeaderContextMenuEvent:
    def __init__(self, group_id: str, payload: Any):
        self.group_id = group_id
        self.payload = payload
        self.type = 0


class GroupHeaderContextMenu:
    _instance = None

    def __init__(self):
        GroupHeaderContextMenu._instance = self

    def __del__(self):
        self.destroy()

    def destroy(self):
        GroupHeaderContextMenu._instance = None

    @classmethod
    def on_mouse_event(cls, event: GroupHeaderContextMenuEvent):
        if cls._instance:
            cls._instance._on_mouse_event(event)

    def _on_mouse_event(self, event: GroupHeaderContextMenuEvent):
        import omni.kit.context_menu

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

        # setup objects, this is passed to all functions
        objects = {
            "payload": event.payload,
        }

        menu_list = omni.kit.context_menu.get_menu_dict(
            "group_context_menu." + event.group_id, "omni.kit.window.property"
        )
        omni.kit.context_menu.get_instance().show_context_menu(
            "group_context_menu." + event.group_id, objects, menu_list
        )
