## 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.app
import omni.usd
from omni.kit.test.async_unittest import AsyncTestCase
from omni.kit import ui_test
from pxr import Sdf, UsdShade
from omni.kit.test_suite.helpers import open_stage, get_test_data_path, select_prims, wait_stage_loading, wait_for_window, arrange_windows
from omni.kit.material.library.test_helper import MaterialLibraryTestHelper


class StageAssignMaterialMulti(AsyncTestCase):
    # Before running each test
    async def setUp(self):
        await arrange_windows()
        await open_stage(get_test_data_path(__name__, "bound_shapes.usda"))

    # After running each test
    async def tearDown(self):
        await wait_stage_loading()

    DATA = [["/World/Cube", "/World/Cone", "/World/Sphere", "/World/Cylinder"],
            ["", "/World/Looks/OmniGlass", "/World/Looks/OmniPBR", "/World/Looks/OmniSurface_Plastic"]]

    async def test_l1_stage_assign_material_multi(self):
        usd_context = omni.usd.get_context()
        stage = usd_context.get_stage()
        to_select = self.DATA[0]
        for index, mtl_path in enumerate(self.DATA[1]):
            # select prims
            await select_prims(to_select)

            # right click on prim
            stage_widget = ui_test.find("Stage//Frame/**/ScrollingFrame/TreeView[*].visible==True")
            await stage_widget.find(f"**/StringField[*].model.path=='{to_select[0]}'").right_click()

            # click on context menu item
            await ui_test.select_context_menu("Assign Material")

            #use assign material dialog
            async with MaterialLibraryTestHelper() as material_test_helper:
                await material_test_helper.handle_assign_material_dialog(index)

            # verify
            for prim_path in to_select:
                prim = stage.GetPrimAtPath(prim_path)
                bound_material, _ = UsdShade.MaterialBindingAPI(prim).ComputeBoundMaterial()
                self.assertTrue(bound_material.GetPrim().GetPrimPath().pathString == mtl_path)
