## Copyright (c) 2021, NVIDIA CORPORATION.  All rights reserved.
##
## 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 omni.kit.test
import omni.kit.commands
import omni.kit.undo

from .test_hydra_common import RtxHydraTest
from .test_common import wait_for_update


class TestRtxHydraLightCollections(RtxHydraTest):
    """ To run:
from omni.kit.test import unittests
from omni.rtx.tests import test_hydra_light_collections
unittests.run_tests_in_modules([test_hydra_light_collections])
    """
    TEST_PATH = "hydra/lightCollections"
    LIGHT_PATH = "/World/defaultLight"
    LIGHT_VIS_PATH = LIGHT_PATH + ".visibility"
    LIGHT_SHADOW_EXCLUDE_PATH = LIGHT_PATH + ".collection:shadowLink:excludes"
    LIGHT_LINK_INCLUDE_PATH = LIGHT_PATH + ".collection:lightLink:includeRoot"
    CUBE_PATH = "/World/Cube"
    SHADOW_LINK_INCLUDE_PATH = LIGHT_PATH + ".collection:shadowLink:includeRoot"

    async def test_UNSTABLE_light_collection_undo(self):
        """
        Regression test for usdImaging refresh issue when undoing light deletion
        """
        self.open_usd("hydra/LightLink.usda")

        await wait_for_update(wait_frames=25)
        await self.capture_and_compare(self.TEST_PATH, "lightCollectionWorking.png", 1e-4)

        omni.kit.commands.execute("DeletePrims", paths=[self.LIGHT_PATH])

        await wait_for_update(wait_frames=25)
        await self.capture_and_compare(self.TEST_PATH, "lightCollectionBlack.png", 1e-4)

        omni.kit.undo.undo()

        await wait_for_update(wait_frames=25)
        await self.capture_and_compare(self.TEST_PATH, "lightCollectionWorking.png", 1e-4)


    # OM-56283, CI-1655 - Sporadic crash/unreliable image output
    async def test_UNSTABLE_light_collection_toggle_crash_UNSTABLE(self):
        """
        Regression test for crash in Light Collection toggle
        """
        self.open_usd("hydra/LightLink.usda")

        await wait_for_update(wait_frames=25)
        await self.capture_and_compare(self.TEST_PATH, "lightCollectionWorking.png", 1e-3)

        omni.kit.commands.execute("ChangeProperty", prop_path=self.LIGHT_LINK_INCLUDE_PATH, value=True, prev=False)

        await wait_for_update(wait_frames=25)
        await self.capture_and_compare(self.TEST_PATH, "lightCollectionInactive.png", 1e-3)

        omni.kit.commands.execute("ChangeProperty", prop_path=self.SHADOW_LINK_INCLUDE_PATH, value=False, prev=True)
        await wait_for_update(wait_frames=1)
        omni.kit.commands.execute("ChangeProperty", prop_path=self.SHADOW_LINK_INCLUDE_PATH, value=True, prev=False)
        await wait_for_update(wait_frames=1)
        omni.kit.commands.execute("ChangeProperty", prop_path=self.LIGHT_LINK_INCLUDE_PATH, value=False, prev=True)

        await wait_for_update(wait_frames=25)
        await self.capture_and_compare(self.TEST_PATH, "lightCollectionWorking.png", 1e-3)


    async def test_UNSTABLE_light_collection_lightvistoggle(self):
        """
        Regression test for refresh issue when toggling light visibility
        """
        self.open_usd("hydra/LightLink.usda")

        await wait_for_update(wait_frames=25)
        await self.capture_and_compare(self.TEST_PATH, "lightCollectionWorking.png", 1e-3)

        omni.kit.commands.execute("ChangeProperty", prop_path=self.LIGHT_VIS_PATH, value="invisible", prev="inherited")

        await wait_for_update(wait_frames=25)
        await self.capture_and_compare(self.TEST_PATH, "lightCollectionBlack.png", 1e-3)

        omni.kit.commands.execute("ChangeProperty", prop_path=self.LIGHT_VIS_PATH, value="inherited", prev="invisible")
        await wait_for_update(wait_frames=25)

        await self.capture_and_compare(self.TEST_PATH, "lightCollectionWorking.png", 1e-3)

    # OM-53278 - occasionally produces pitch black image
    async def test_UNSTABLE_light_collection_refresh_issue_when_light_and_shadow_are_the_same_UNSTABLE(self):
        """
        Regression test for refresh issue when the light and shadow collections become the same
        This is a regression test for OM-48040 Light Linking: light goes off after toggling Include Root on/off
        """
        self.open_usd("hydra/LightLinkSimple.usda")

        await wait_for_update(wait_frames=25)
        await self.capture_and_compare(self.TEST_PATH, "lightCollectionSimpleCubeNoLight.png", 1e-3)

        omni.kit.commands.execute(
            "AddRelationshipTarget",
            relationship=omni.usd.get_context().get_stage().GetPropertyAtPath(self.LIGHT_SHADOW_EXCLUDE_PATH),
            target=self.CUBE_PATH
        )

        await wait_for_update(wait_frames=25)
        await self.capture_and_compare(self.TEST_PATH, "lightCollectionSimpleCubeNoShadow.png", 1e-3)

        omni.kit.commands.execute("ChangeProperty", prop_path=self.LIGHT_LINK_INCLUDE_PATH, value=False, prev=True)
        await wait_for_update(wait_frames=25)

        await self.capture_and_compare(self.TEST_PATH, "lightCollectionBlack.png", 1e-3)

        omni.kit.commands.execute("ChangeProperty", prop_path=self.LIGHT_LINK_INCLUDE_PATH, value=True, prev=False)
        await wait_for_update(wait_frames=25)

        await self.capture_and_compare(self.TEST_PATH, "lightCollectionSimpleCubeNoShadow.png", 1e-3)
