__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 math
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 TestUsdTimeCode(TestClass):
    @tc_logger
    def test_timecode(self):
        from usdrt import Usd

        # ctor
        t0 = Usd.TimeCode()
        self.assertTrue(t0)
        self.assertEqual(t0.GetValue(), 0.0)

        t1 = Usd.TimeCode(1.0)
        self.assertTrue(t1)
        self.assertEqual(t1.GetValue(), 1.0)

        nan = Usd.TimeCode(float("nan"))
        self.assertTrue(nan)
        self.assertTrue(math.isnan(nan.GetValue()))

        # operators
        self.assertTrue(t1 == Usd.TimeCode(1.0))
        self.assertFalse(t0 == t1)
        self.assertFalse(t1 == nan)
        self.assertTrue(nan == nan)

        self.assertTrue(t0 != t1)
        self.assertFalse(t1 != Usd.TimeCode(1.0))
        self.assertTrue(t1 != nan)
        self.assertFalse(nan != nan)

        self.assertTrue(t0 <= t1)
        self.assertFalse(t1 <= t0)
        self.assertTrue(t1 <= Usd.TimeCode(1.0))
        self.assertFalse(t1 <= nan)
        self.assertTrue(nan <= t1)

        self.assertTrue(t0 < t1)
        self.assertFalse(t1 < t0)
        self.assertFalse(t1 < Usd.TimeCode(1.0))
        self.assertFalse(t1 < nan)
        self.assertTrue(nan < t1)

        self.assertTrue(t1 >= t0)
        self.assertFalse(t0 >= t1)
        self.assertTrue(t1 >= Usd.TimeCode(1.0))
        self.assertTrue(t1 >= nan)
        self.assertFalse(nan >= t1)

        self.assertTrue(t1 > t0)
        self.assertFalse(t0 > t1)
        self.assertFalse(t1 > Usd.TimeCode(1.0))
        self.assertTrue(t1 > nan)
        self.assertFalse(nan > t1)

        # is earliest time
        self.assertFalse(t1.IsEarliestTime())
        self.assertTrue(Usd.TimeCode(-1.7976931348623157e308).IsEarliestTime())

        # is default
        self.assertFalse(t1.IsDefault())
        self.assertTrue(nan.IsDefault())

        # static methods
        self.assertEqual(Usd.TimeCode.EarliestTime().GetValue(), -1.7976931348623157e308)
        self.assertTrue(math.isnan(Usd.TimeCode.Default().GetValue()))

    @tc_logger
    def test_time_code_repr(self):
        """
        Validates the string representation of the default time code.
        """
        from usdrt import Usd

        defaultTime = Usd.TimeCode.Default()
        timeRepr = repr(defaultTime)
        self.assertEqual(timeRepr, "Usd.TimeCode.Default()")
        self.assertEqual(eval(timeRepr), defaultTime)

    @tc_logger
    def testEarliestTimeRepr(self):
        """
        Validates the string representation of the earliest time code.
        """
        from usdrt import Usd

        earliestTime = Usd.TimeCode.EarliestTime()
        timeRepr = repr(earliestTime)
        self.assertEqual(timeRepr, "Usd.TimeCode.EarliestTime()")
        self.assertEqual(eval(timeRepr), earliestTime)

    @tc_logger
    def testDefaultConstructedTimeRepr(self):
        """
        Validates the string representation of a time code created using
        the default constructor.
        """
        from usdrt import Usd

        timeCode = Usd.TimeCode()
        timeRepr = repr(timeCode)
        self.assertEqual(timeRepr, "Usd.TimeCode()")
        self.assertEqual(eval(timeRepr), timeCode)

    @tc_logger
    def testNumericTimeRepr(self):
        """
        Validates the string representation of a numeric time code.
        """
        from usdrt import Usd

        timeCode = Usd.TimeCode(123.0)
        timeRepr = repr(timeCode)
        self.assertEqual(timeRepr, "Usd.TimeCode(123.0)")
        self.assertEqual(eval(timeRepr), timeCode)
