## 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.ui.tests.test_base import OmniUiTest
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, delete_prim_path_children, arrange_windows
from omni.kit.material.library.test_helper import MaterialLibraryTestHelper


class TestCreateMenuContextMenu(OmniUiTest):
    # 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"]

    async def test_l1_stage_menu_create_custom_materials(self):
        stage_window = ui_test.find("Stage")
        await stage_window.focus()

        usd_context = omni.usd.get_context()
        stage = usd_context.get_stage()
        to_select = self.DATA

        # test custom materials
        material_test_helper = MaterialLibraryTestHelper()
        for material_url, mtl_name in [("mahogany_floorboards.mdl", "mahogany_floorboards"), ("multi_hair.mdl", "OmniHair_Green"), ("multi_hair.mdl", "OmniHair_Brown")]:
            # delete any materials in looks
            await delete_prim_path_children("/World/Looks")

            # select prims
            await select_prims(to_select)

            # right click on Cube
            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("Create/Material/Add MDL File", offset=ui_test.Vec2(50, 10))

            # use add material dialog
            mdl_path = get_test_data_path(__name__, f"mtl/{material_url}")
            await material_test_helper.handle_add_material_dialog(mdl_path, mtl_name)

            # wait for material to load & UI to refresh
            await wait_stage_loading()

            # verify item(s)
            for prim_path in to_select:
                prim = stage.GetPrimAtPath(prim_path)
                bound_material, _ = UsdShade.MaterialBindingAPI(prim).ComputeBoundMaterial()
                self.assertTrue(bound_material.GetPrim().IsValid())
                self.assertEqual(bound_material.GetPrim().GetPrimPath().pathString, f"/World/Looks/{mtl_name}")
