import math
import weakref
import platform
import unittest
import pathlib
import omni.kit.test
import omni.ui as ui
import carb
from pxr import Usd, Sdf, UsdGeom
from omni.ui.tests.test_base import OmniUiTest
from omni.kit import ui_test
from omni.kit.test_suite.helpers import arrange_windows, open_stage, get_test_data_path, select_prims, wait_stage_loading


class TestMaterialRefreshWidget(OmniUiTest):
    async def setUp(self):
        await super().setUp()
        await arrange_windows()
        await open_stage(get_test_data_path(__name__, "thumbnail_test.usda"))

    async def tearDown(self):
        await super().tearDown()
        await select_prims(["/World/Sphere"])
        ui_test.find("Property//Frame/**/CollapsableFrame[*].title=='Raw USD Properties'").widget.collapsed = True

    async def test_material_refresh_ui(self):
        stage_widget = ui_test.find("Stage")
        await stage_widget.focus()

        # Select the prim.
        await select_prims(["/World/Sphere"])

        # get raw frame & open
        raw_frame = ui_test.find("Property//Frame/**/CollapsableFrame[*].title=='Raw USD Properties'")
        raw_frame.widget.collapsed = False
        await ui_test.human_delay(10)

        # get material:binding label & scroll to it
        raw_frame.find("**/Label[*].text=='material:binding'").widget.scroll_here_y(0.5)
        await ui_test.human_delay(10)

        # get raw relationship widgets
        for w in ui_test.find_all("Property//Frame/**/*.identifier!=''"):
            if w.widget.identifier == "remove_relationship_World_Looks_Material":
                # verify material binding in material widget
                self.assertEqual(ui_test.find("Property//Frame/**/StringField[*].identifier=='combo_drop_target'").widget.model.as_string, "/World/Looks/Material")

                # remove raw material binding
                await w.click()
                await ui_test.human_delay(10)

                # verify material binding updated in material widget
                self.assertEqual(ui_test.find("Property//Frame/**/StringField[*].identifier=='combo_drop_target'").widget.model.as_string, "None")

                break
