import omni.kit.test
import omni.kit.app

from pathlib import Path
from pxr import Sdf, UsdShade, UsdGeom
from omni.ui.tests.test_base import OmniUiTest
from omni.kit import ui_test
from omni.kit.test_suite.helpers import arrange_windows, select_prims
from omni.kit.window.content_browser.test_helper import ContentBrowserTestHelper


class TestAddMDLFile(OmniUiTest):
    async def test_add_mdl_file(self):
        await arrange_windows()

        extension_path = omni.kit.app.get_app().get_extension_manager().get_extension_path_by_module(__name__)
        mdl_path = Path(extension_path).joinpath("data/tests/TESTEXPORT.mdl").absolute()

        # setup
        await ui_test.find("Stage").focus()
        await ui_test.find("Content").focus()

        # new stage
        usd_context = omni.usd.get_context()
        await usd_context.new_stage_async()
        stage = usd_context.get_stage()

        # create prims
        omni.kit.commands.execute("CreatePrimWithDefaultXform", prim_type="Sphere")
        await select_prims(["/Sphere"])
        await ui_test.human_delay()

        # drag/drop
        async with ContentBrowserTestHelper() as content_browser_helper:
            property_widget = ui_test.find("Property//Frame/**/Button[*].identifier=='preview_drop_target'")
            await content_browser_helper.drag_and_drop_tree_view(str(mdl_path), drag_target=property_widget.center)

        # verify
        # NOTE: TESTEXPORT.mdl material is named "Material" so that is the prim created
        shader = UsdShade.Shader(stage.GetPrimAtPath("/Looks/Material/Shader"))
        identifier = shader.GetSourceAssetSubIdentifier("mdl")
        self.assertTrue(identifier == "Material")
