__copyright__ = "Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved."
__license__ = """
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 os
import pathlib
import platform
import sys
import time
import unittest

from . import tc_logger


def setUpModule():
    # pre-load USD before anything else to prevent crash
    # from weird carb conflict
    # FIXME ^^^
    try:
        from . import update_path_and_load_usd

        update_path_and_load_usd()
    except ImportError:
        # not needed in Kit
        pass


try:
    import omni.kit.test

    TestClass = omni.kit.test.AsyncTestCase
    TEST_DIR = str(pathlib.Path(__file__).parent.parent.parent.parent)
except ImportError:
    from . import TestRtScenegraph

    TestClass = TestRtScenegraph
    TEST_DIR = "."


class TestSchemaBase(TestClass):
    @tc_logger
    def test_invalid_schema_base(self):
        from usdrt import Sdf, Usd

        sb = Usd.SchemaBase()
        # Conversion to bool should return False.
        self.assertFalse(sb)
        # It should still be safe to get the prim, but the prim will be invalid.
        p = sb.GetPrim()
        self.assertFalse(p.IsValid())
        # It should still be safe to get the path, but the path will be empty.
        self.assertEqual(sb.GetPrimPath(), Sdf.Path(""))
        # Try creating a CollectionAPI from it, and make sure the result is
        # a suitably invalid CollactionAPI object.
        coll = Usd.CollectionAPI(sb, "newcollection")
        self.assertFalse(coll)
        # revisit
        # with self.assertRaises(RuntimeError):
        #    coll.CreateExpansionRuleAttr()


class TestImports(TestClass):
    @tc_logger
    def test_usd_schema_imports(self):
        try:
            from usdrt import Usd
        except ImportError:
            self.fail("from usdrt import Usd failed")

        try:
            from usdrt import UsdGeom
        except ImportError:
            self.fail("from usdrt import UsdGeom failed")

        try:
            from usdrt import UsdLux
        except ImportError:
            self.fail("from usdrt import UsdLux failed")

        try:
            from usdrt import UsdMedia
        except ImportError:
            self.fail("from usdrt import UsdMedia failed")

        try:
            from usdrt import UsdRender
        except ImportError:
            self.fail("from usdrt import UsdRender failed")

        try:
            from usdrt import UsdShade
        except ImportError:
            self.fail("from usdrt import UsdShade failed")

        try:
            from usdrt import UsdSkel
        except ImportError:
            self.fail("from usdrt import UsdSkel failed")

        try:
            from usdrt import UsdUI
        except ImportError:
            self.fail("from usdrt import UsdUI failed")

        try:
            from usdrt import UsdVol
        except ImportError:
            self.fail("from usdrt import UsdVol failed")

        # Nvidia Schemas
        try:
            from usdrt import UsdPhysics
        except ImportError:
            self.fail("from usdrt import UsdPhysics failed")

        try:
            from usdrt import PhysxSchema
        except ImportError:
            self.fail("from usdrt import PhysxSchema failed")

        try:
            from usdrt import ForceFieldSchema
        except ImportError:
            self.fail("from usdrt import ForceFieldSchema failed")

        try:
            from usdrt import DestructionSchema
        except ImportError:
            self.fail("from usdrt import DestructionSchema failed")
