## 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.app
import omni.kit.commands
import omni.kit.undo
import omni.kit.test
from .test_common import RtxTest, testSettings, postLoadTestSettings, wait_for_update
from pxr import UsdGeom


class TestRtxExample1(RtxTest):
    """
    Example of rtx test which creates box and capture screenshot

    Hints: for python debug insert line below and attach to kit process in VS Code
        omni.kit.debug.python.breakpoint()
    """
    # override resolution
    WINDOW_SIZE = (512, 512)

    async def setUp(self):
        await super().setUp()
        self.set_settings(testSettings)
        omni.usd.get_context().new_stage()  # Important: it should be called before `setUp` method as it reset settings and stage.
        self.add_dir_light()
        await omni.kit.app.get_app().next_update_async()  # Wait stage loading
        self.set_settings(postLoadTestSettings)

    def create_mesh(self):
        box = UsdGeom.Mesh.Define(self.ctx.get_stage(), "/World/box")
        box.CreatePointsAttr([(-50, -50, -50), (50, -50, -50), (-50, -50, 50), (50, -50, 50),
                              (-50, 50, -50), (50, 50, -50), (50, 50, 50), (-50, 50, 50)])
        box.CreateFaceVertexCountsAttr([4, 4, 4, 4, 4, 4])
        box.CreateFaceVertexIndicesAttr([0, 1, 3, 2, 0, 4, 5, 1, 1, 5, 6, 3, 2, 3, 6, 7, 0, 2, 7, 4, 4, 7, 6, 5])
        box.CreateSubdivisionSchemeAttr("none")
        return box

    # Unstable via OM-58681
    async def test_UNSTABLE_rtx_example_create_mesh_UNSTABLE(self):
        """
        Test example - create mesh
        """
        self.create_mesh()
        await wait_for_update()
        await self.capture_and_compare("example", "mesh.png")

    async def test_rtx_example_check_mesh(self):
        """
        Test example - check mesh
        """
        box = self.create_mesh()
        points = box.GetPointsAttr().Get()
        face_indices = box.GetFaceVertexIndicesAttr().Get()
        unique_indices = set(face_indices)
        self.assertTrue(len(points) == len(unique_indices))


class TestRtxExample2(RtxTest):
    async def setUp(self):
        await super().setUp()
        self.set_settings(testSettings)
        self.open_usd("bunny.obj.usda")
        await omni.kit.app.get_app().next_update_async()
        self.set_settings(postLoadTestSettings)
        self.set_camera((6.969233739993722, 6.969233739993723, 6.969233739993719), (0, 0, 0))

    async def test_UNSTABLE_rtx_example_open_usd_UNSTABLE(self):
        """
        Test example - open usd file
        """
        await wait_for_update()
        await self.capture_and_compare("example", "usd.png", 1e-4)
