# 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.
#

from .layers_menu import layers_available
from .layers_menu import LayersMenu
from .content_menu import ContentMenu
from .content_browser_menu import content_browser_available
from .content_browser_menu import ContentBrowserMenu
import omni.ext
import omni.kit.app


class UsdaEditExtension(omni.ext.IExt):
    def on_startup(self, ext_id):
        # Setup a callback for the event
        app = omni.kit.app.get_app_interface()
        ext_manager = app.get_extension_manager()
        self.__extensions_subscription = ext_manager.get_change_event_stream().create_subscription_to_pop(
            self._on_event, name="omni.kit.usda_edit"
        )
        self.__layers_menu = None
        self.__content_menu = None
        self.__content_browser_menu = None

        self._on_event(None)

        # When kit exits we won't get a change event for content_browser unloading so we need to resort to
        # an explicit subscription to its enable/disable events.
        #
        # If content_browser is already enabled then we will immediately get a callback for it, so we want
        # to do this last.
        self.__content_browser_hook = ext_manager.subscribe_to_extension_enable(
            self._on_browser_enabled, self._on_browser_disabled, 'omni.kit.window.content_browser')

    def _on_browser_enabled(self, ext_id: str):
        if not self.__content_browser_menu:
            self.__content_browser_menu = ContentBrowserMenu()

    def _on_browser_disabled(self, ext_id: str):
        if self.__content_browser_menu:
            self.__content_browser_menu.destroy()
            self.__content_browser_menu = None

    def _on_event(self, event):
        # Create/destroy the menu in the Layers window
        if self.__layers_menu:
            if not layers_available():
                self.__layers_menu.destroy()
                self.__layers_menu = None
        else:
            if layers_available():
                self.__layers_menu = LayersMenu()

        # TODO: Create/destroy the menu in the Content Browser
        if self.__content_menu:
            self.__content_menu.destroy()
            self.__content_menu = None

    def on_shutdown(self):
        self.__extensions_subscription = None
        self.__content_browser_hook = None

        if self.__layers_menu:
            self.__layers_menu.destroy()
            self.__layers_menu = None

        if self.__content_menu:
            self.__content_menu.destroy()
            self.__content_menu = None

        if self.__content_browser_menu:
            self.__content_browser_menu.destroy()
            self.__content_browser_menu = None
