# 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.
#
import omni.ext
from .content_menu import content_available
from .content_menu import ContentMenu


class StageViewerExtension(omni.ext.IExt):
    def on_startup(self, ext_id):
        # Setup a callback when any extension is loaded/unloaded
        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.window.stageviewer"
        )

        self.__context_menu = None

        self._on_event(None)

    def _on_event(self, event):
        """Called when any extension is loaded/unloaded"""
        if self.__context_menu:
            if not content_available():
                self.__context_menu.destroy()
                self.__context_menu = None
        else:
            if content_available():
                self.__context_menu = ContentMenu()

    def on_shutdown(self):
        self.__extensions_subscription = None

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

