## 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 os
import omni.kit.app
import omni.usd
import omni.kit.commands
from omni.kit.test.async_unittest import AsyncTestCase

from omni.kit import ui_test
from pxr import Sdf, Usd, UsdShade
from omni.kit.material.library.test_helper import MaterialLibraryTestHelper
from omni.kit.test_suite.helpers import  (
    open_stage,
    get_test_data_path,
    select_prims,
    delete_prim_path_children,
    arrange_windows
    )
from omni.kit.material.library.test_helper import MaterialLibraryTestHelper
from omni.kit.window.content_browser.test_helper import ContentBrowserTestHelper


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

    async def test_l1_context_menu_bind_material_listview(self):
        await ui_test.find("Stage").focus()
        await ui_test.find("Content").focus()

        # grid_view_enabled = True doesn't work with item_offset
        to_select = ["/World/Cube", "/World/Sphere", "/World/Cylinder"]
        stage =  omni.usd.get_context().get_stage()

        material_test_helper = MaterialLibraryTestHelper()
        content_browser_helper = ContentBrowserTestHelper()
        await content_browser_helper.toggle_grid_view_async(False)

        mdl_list = await omni.kit.material.library.get_mdl_list_async()
        for mtl_name, mdl_path, submenu in mdl_list:
            # delete any materials in looks
            await delete_prim_path_children("/World/Looks")

            # get content browser file
            await content_browser_helper.navigate_to_async(mdl_path)
            await ui_test.human_delay(10)
            item = await content_browser_helper.get_treeview_item_async(os.path.basename(mdl_path))
            self.assertFalse(item == None)

            # get content browser treeview
            content_treeview = ui_test.find("Content//Frame/**/TreeView[*].identifier=='content_browser_treeview'")

            # select prims
            await select_prims(to_select)

            # right click content browser
            await content_treeview.right_click(item.center)

            # click on context menu item
            await ui_test.select_context_menu("Bind material to selected prim(s)")

            # use create material dialog
            await material_test_helper.handle_create_material_dialog(mdl_path, mtl_name)

            # 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() == True)
                self.assertTrue(bound_material.GetPrim().GetPrimPath().pathString == f"/World/Looks/{mtl_name}")
