__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 TestUsdPrimRange(TestClass):
    @tc_logger
    def test_creation(self):
        from usdrt import Sdf, Usd

        stage = Usd.Stage.Open(TEST_DIR + "/data/usd/tests/cornell.usda")
        self.assertTrue(stage)

        prim = stage.GetPrimAtPath(Sdf.Path("/Cornell_Box"))
        self.assertTrue(prim)

        prim_range = Usd.PrimRange(prim)
        self.assertTrue(prim_range)

    @tc_logger
    def test_iterator(self):
        from usdrt import Sdf, Usd

        stage = Usd.Stage.Open(TEST_DIR + "/data/usd/tests/cornell.usda")
        self.assertTrue(stage)

        prim = stage.GetPrimAtPath(Sdf.Path("/Cornell_Box"))
        self.assertTrue(prim)

        prim_range = Usd.PrimRange(prim)
        self.assertTrue(prim_range)

        count = 0
        for this_prim in prim_range:
            count += 1

        self.assertEqual(count, 15)
        self.assertEqual(this_prim.GetPath(), Sdf.Path("/Cornell_Box/Root/Looks/Red1SG/Red_Side"))

        count = 0
        it = iter(prim_range)
        for this_prim in it:
            if this_prim.GetName() == "Green_Wall":
                it.PruneChildren()
            count += 1
        self.assertEqual(count, 14)
        self.assertEqual(this_prim.GetPath(), Sdf.Path("/Cornell_Box/Root/Looks/Red1SG/Red_Side"))
        prim = stage.GetPrimAtPath(Sdf.Path("/Emissive_square/Root"))
        self.assertTrue(prim)

        prim_range = Usd.PrimRange(prim)
        self.assertTrue(prim_range)

        count = 0
        for this_prim in prim_range:
            count += 1

        self.assertEqual(count, 5)
        self.assertEqual(this_prim.GetPath(), Sdf.Path("/Emissive_square/Root/Looks/EmissiveSG/Emissive"))

        count = 0
        it = iter(prim_range)
        for this_prim in it:
            it.PruneChildren()
            count += 1
        self.assertEqual(count, 1)
        self.assertEqual(this_prim.GetPath(), Sdf.Path("/Emissive_square/Root"))
    @tc_logger
    def test_repr_representation(self):
        from usdrt import Sdf, Usd

        stage = Usd.Stage.Open(TEST_DIR + "/data/usd/tests/cornell.usda")
        self.assertTrue(stage)

        prim = stage.GetPrimAtPath(Sdf.Path("/Emissive_square/Root"))
        self.assertTrue(prim)

        prim_range = Usd.PrimRange(prim)
        self.assertTrue(prim_range)

        expected = "PrimRange(</Emissive_square/Root>)"

        self.assertEquals(repr(prim_range), expected)
