import os
import carb
import omni.ext
from pathlib import Path
from .audio_settings_widget import AudioSettingsWidget
from pxr import Sdf, OmniAudioSchema, UsdMedia

TEST_DATA_PATH = ""

class AudioPropertyExtension(omni.ext.IExt):
    def __init__(self):
        self._registered = False
        super().__init__()

    def on_startup(self, ext_id):
        self._register_widget()

        manager = omni.kit.app.get_app().get_extension_manager()
        extension_path = manager.get_extension_path(ext_id)
        global TEST_DATA_PATH
        TEST_DATA_PATH = Path(extension_path).joinpath("data").joinpath("tests")

    def on_shutdown(self):
        if self._registered:
            self._unregister_widget()

    def _register_widget(self):
        import omni.kit.window.property as p
        from omni.kit.window.property.property_scheme_delegate import PropertySchemeDelegate
        from omni.kit.property.usd.usd_property_widget import SchemaPropertiesWidget, MultiSchemaPropertiesWidget

        w = p.get_window()
        if w:
            w.register_widget("prim", "media", SchemaPropertiesWidget("Media", UsdMedia.SpatialAudio, False))
            w.register_widget("prim", "audio_sound", SchemaPropertiesWidget("Sound", OmniAudioSchema.OmniSound, False))
            w.register_widget("prim", "audio_listener", SchemaPropertiesWidget("Listener", OmniAudioSchema.OmniListener, False))
            w.register_widget("layers", "audio_settings", AudioSettingsWidget())
            self._registered = True

    def _unregister_widget(self):
        import omni.kit.window.property as p

        w = p.get_window()
        if w:
            w.unregister_widget("prim", "media")
            w.unregister_widget("prim", "audio_sound")
            w.unregister_widget("prim", "audio_listener")
            w.unregister_widget("layers", "audio_settings")
            self._registered = False
