# Copyright (c) 2021-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 Callable, Union, Set
from collections import OrderedDict
from omni.kit.window.filepicker import SearchDelegate
from .extension import ContentBrowserRegistryExtension, get_instance


def custom_menus() -> OrderedDict:
    registry = get_instance()
    if registry:
        return registry._custom_menus
    return OrderedDict()

def selection_handlers() -> Set:
    registry = get_instance()
    if registry:
        return registry._selection_handlers
    return set()

def search_delegate() -> SearchDelegate:
    registry = get_instance()
    if registry:
        return registry._search_delegate
    return None

def register_context_menu(name: str, glyph: str, click_fn: Callable, show_fn: Callable, index: int = -1):
    registry = get_instance()
    if registry:
        registry.register_custom_menu("context", name, glyph, click_fn, show_fn, index=index)

def deregister_context_menu(name: str):
    registry = get_instance()
    if registry:
        registry.deregister_custom_menu("context", name)

def register_listview_menu(name: str, glyph: str, click_fn: Callable, show_fn: Callable, index: int = -1):
    registry = get_instance()
    if registry:
        registry.register_custom_menu("listview", name, glyph, click_fn, show_fn, index=index)

def deregister_listview_menu(name: str):
    registry = get_instance()
    if registry:
        registry.deregister_custom_menu("listview", name)

def register_import_menu(name: str, glyph: str, click_fn: Callable, show_fn: Callable):
    registry = get_instance()
    if registry:
        registry.register_custom_menu("import", name, glyph, click_fn, show_fn)

def deregister_import_menu(name: str):
    registry = get_instance()
    if registry:
        registry.deregister_custom_menu("import", name)

def register_file_open_handler(name: str, open_fn: Callable, file_type: Union[int, Callable]):
    registry = get_instance()
    if registry:
        registry.register_custom_menu("file_open", name, None, open_fn, file_type)

def deregister_file_open_handler(name: str):
    registry = get_instance()
    if registry:
        registry.deregister_custom_menu("file_open", name)

def register_selection_handler(handler: Callable):
    registry = get_instance()
    if registry:
        registry.register_selection_handler(handler)

def deregister_selection_handler(handler: Callable):
    registry = get_instance()
    if registry:
        registry.deregister_selection_handler(handler)

def register_search_delegate(search_delegate: SearchDelegate):
    registry = get_instance()
    if registry:
        registry.register_search_delegate(search_delegate)

def deregister_search_delegate(search_delegate: SearchDelegate):
    registry = get_instance()
    if registry:
        registry.deregister_search_delegate(search_delegate)
