__copyright__ = "Copyright (c) 2023, 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 TestSdfAssetPath(TestClass):
    @tc_logger
    def test_ctor(self):
        from usdrt import Sdf

        emptypath = Sdf.AssetPath()
        self.assertEqual(emptypath.path, "")
        self.assertEqual(emptypath.resolvedPath, "")

        asset_only = Sdf.AssetPath("hello")
        self.assertEqual(asset_only.path, "hello")
        self.assertEqual(asset_only.resolvedPath, "")

        asset_and_resolved = Sdf.AssetPath("hello", "hello.txt")
        self.assertEqual(asset_and_resolved.path, "hello")
        self.assertEqual(asset_and_resolved.resolvedPath, "hello.txt")

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

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

        prim = stage.GetPrimAtPath("/Cornell_Box/Root/Looks/Green1SG/Green_Side")
        self.assertTrue(prim)

        attr = prim.GetAttribute("info:mdl:sourceAsset")
        self.assertTrue(attr)
        self.assertTrue(attr.HasValue())

        val = attr.Get()

        self.assertEqual(val.path, "./Green_Side.mdl")
        self.assertTrue(os.path.normpath(val.resolvedPath).endswith(os.path.normpath("data/usd/tests/Green_Side.mdl")))

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

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

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

        attr = prim.GetAttribute("test:assetlist")
        self.assertTrue(attr)
        self.assertTrue(attr.HasValue())

        val = attr.Get()

        self.assertEqual(len(val), 3)

        unresolved = ["./asset.txt", "./asset_other.txt", "./existing_1042.txt"]
        resolved_ends = [
            os.path.normpath(i)
            for i in ["data/usd/tests/asset.txt", "data/usd/tests/asset_other.txt", "data/usd/tests/existing_1042.txt"]
        ]
        for i in range(3):
            self.assertEqual(val[i].path, unresolved[i])
            self.assertTrue(os.path.normpath(val[i].resolvedPath).endswith(resolved_ends[i]))

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

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

        prim = stage.GetPrimAtPath("/Cornell_Box/Root/Looks/Green1SG/Green_Side")
        self.assertTrue(prim)

        attr = prim.GetAttribute("info:mdl:sourceAsset")
        self.assertTrue(attr)
        self.assertTrue(attr.HasValue())

        newval = Sdf.AssetPath("hello", "hello.txt")

        self.assertTrue(attr.Set(newval))

        val = attr.Get()

        self.assertEqual(val.path, "hello")
        self.assertEqual(val.resolvedPath, "hello.txt")

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

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

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

        attr = prim.GetAttribute("test:assetlist")
        self.assertTrue(attr)
        self.assertTrue(attr.HasValue())

        newval = Vt.AssetArray(4)
        for i in range(4):
            newval[i] = Sdf.AssetPath(f"hello{i}", f"hello{i}.txt")

        attr.Set(newval)

        # Freeing the original array must be fine
        del newval

        val = attr.Get()

        self.assertEqual(len(val), 4)

        for i in range(4):
            self.assertEqual(val[i].path, f"hello{i}")
            self.assertEqual(val[i].resolvedPath, f"hello{i}.txt")
