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

import omni.ext

from omni.rtx.window.settings import RendererSettingsFactory

from .importers import IndeXImporters
from .widgets import IndeXCommonSettingStack, IndeXClusterSettingStack

class IndeXSettingsExtension(omni.ext.IExt):
    """The entry point for IndeX Settings Window"""

    renderer_name = "Scientific (IndeX)"

    def on_startup(self):
        RendererSettingsFactory.register_renderer(self.renderer_name, ["IndeX Cluster", "IndeX Common"])
        RendererSettingsFactory.register_stack("IndeX Cluster", IndeXClusterSettingStack)
        RendererSettingsFactory.register_stack("IndeX Common", IndeXCommonSettingStack)

        try:
            import omni.kit.window.property as p
        except ImportError:
            # Likely running headless
            return

        w = p.get_window()
        if not w:
            return

        from .widgets.colormap_properties_widget import ColormapPropertiesWidget
        from .widgets.shader_properties_widget import ShaderPropertiesWidget
        from .widgets.volume_properties_widget import VolumePropertiesWidget

        self.colormap_widget = ColormapPropertiesWidget()
        self.shader_widget = ShaderPropertiesWidget()
        self.volume_widget = VolumePropertiesWidget()

        IndeXImporters.register_bindings(self)

    def on_shutdown(self):
        RendererSettingsFactory.unregister_renderer(self.renderer_name)
        RendererSettingsFactory.unregister_stack("IndeX Cluster")
        RendererSettingsFactory.unregister_stack("IndeX Common")
        RendererSettingsFactory.build_ui()

        try:
            import omni.kit.window.property as p
        except ImportError:
            # Likely running headless
            return

        w = p.get_window()
        if not w:
            return

        w.unregister_widget("prim", "shader")
        self.colormap_widget.on_shutdown()
        self.colormap_widget = None
        self.shader_widget.on_shutdown()
        self.shader_widget = None
        self.volume_widget.on_shutdown()
        self.volume_widget = None

        IndeXImporters.deregister_bindings(self)
