import os
import carb
import omni.ext
from pathlib import Path
from pxr import Sdf, UsdLux, Usd

TEST_DATA_PATH = ""

class LightPropertyExtension(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 .prim_light_widget import LightSchemaAttributesWidget

        w = p.get_window()
        if w:

            # https://github.com/PixarAnimationStudios/USD/commit/7540fdf3b2aa6b6faa0fce8e7b4c72b756286f51
            w.register_widget(
                "prim",
                "light",
                LightSchemaAttributesWidget(
                    "Light",

                    # https://github.com/PixarAnimationStudios/USD/commit/7540fdf3b2aa6b6faa0fce8e7b4c72b756286f51
                    UsdLux.LightAPI if hasattr(UsdLux, 'LightAPI') else UsdLux.Light,
                    [
                        UsdLux.CylinderLight,
                        UsdLux.DiskLight,
                        UsdLux.DistantLight,
                        UsdLux.DomeLight,
                        UsdLux.GeometryLight,
                        UsdLux.RectLight,
                        UsdLux.SphereLight,
                        UsdLux.ShapingAPI,
                        UsdLux.ShadowAPI,
                        UsdLux.LightFilter,

                        # https://github.com/PixarAnimationStudios/USD/commit/9eda37ec9e1692dd290efd9a26526e0d2c21bb03
                        UsdLux.PortalLight if hasattr(UsdLux, 'PortalLight') else UsdLux.LightPortal,

                        UsdLux.ListAPI,
                    ],
                    [
                        "color",
                        "enableColorTemperature",
                        "colorTemperature",
                        "intensity",
                        "exposure",
                        "normalize",
                        "angle",
                        "radius",
                        "height",
                        "width",
                        "radius",
                        "length",
                        "texture:file",
                        "texture:format",
                        "diffuse",
                        "specular",
                        "shaping:focus",
                        "shaping:focusTint",
                        "shaping:cone:angle",
                        "shaping:cone:softness",
                        "shaping:ies:file",
                        "shaping:ies:angleScale",
                        "shaping:ies:normalize",
                        "collection:shadowLink:expansionRule",
                        "collection:shadowLink:excludes",
                        "collection:shadowLink:includes",
                        "collection:lightLink:expansionRule",
                        "collection:lightLink:excludes",
                        "collection:lightLink:includes",                        
                        "light:enableCaustics",
                        "visibleInPrimaryRay",
                        "disableFogInteraction",
                        "isProjector",
                    ] \
                    # https://github.com/PixarAnimationStudios/USD/commit/c8cd344af6be342911e50d2350c228ed329be6b2
                    # USD v22.08 adds this to LightAPI as an API schema override of CollectionAPI
                    + (["collection:lightLink:includeRoot"] if Usd.GetVersion() >= (0,22,8) else []),
                    [],
                ),
            )
            self._registered = True

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

        w = p.get_window()
        if w:
            w.unregister_widget("prim", "light")
            self._registered = False
