import omni.kit.test
import omni.kit.window.inspector.inspector_window

from omni.kit import ui_test


class TestInspectorWindow(omni.kit.test.AsyncTestCase):
    @classmethod
    def setUpClass(cls):
        cls.inspector_window = omni.kit.window.inspector.inspector_window.InspectorWindow()

    async def setUp(self):
        self.window_name = self.inspector_window._window.title

    @classmethod
    def tearDownClass(cls) -> None:
        cls.inspector_window._window.destroy()

    async def test_window_list(self):
        '''
        Check that the main combox box is doing the right thing

        There's a bit of an ordering assumption going on
        '''

        ui_window = ui_test.find(self.window_name)
        widgets = ui_window.find_all("**/WindowListComboBox")
        self.assertTrue(len(widgets) == 1)
        combo_box = widgets[0].widget
        self.assertTrue(isinstance(combo_box, omni.ui.ComboBox))

        # Make sure render settings is set
        combo_box.model.get_item_value_model(None, 0).set_value(0)

        window_name = combo_box.model.get_current_window()
        self.assertEqual(window_name, "Render Settings")

        # Set to Stage window
        combo_box.model.get_item_value_model(None, 0).set_value(1)
        window_name = combo_box.model.get_current_window()
        self.assertEqual(window_name, "Stage")

        await omni.kit.app.get_app().next_update_async()

    async def test_tree_expansion(self):
        # make sure render settings is the current window
        ui_window = ui_test.find(self.window_name)
        widgets = ui_window.find_all("**/WindowListComboBox")
        combo_box = widgets[0].widget
        combo_box.model.get_item_value_model(None, 0).set_value(0)

        widgets = ui_window.find_all("**/WidgetInspectorTree")
        self.assertTrue(len(widgets) == 1)
        tree_items = widgets[0].find_all("**")
        self.assertTrue(len(tree_items) == 20, f"actually was {len(tree_items)}")

        # Switch to Stage Window
        combo_box.model.get_item_value_model(None, 0).set_value(1)
        widgets = ui_window.find_all("**/WidgetInspectorTree")
        self.assertTrue(len(widgets) == 1)
        tree_items = widgets[0].find_all("**")
        self.assertTrue(len(tree_items) == 17, f"actually was {len(tree_items)}")

    async def test_tree_basic_content(self):
        # make sure render settings is the current window
        ui_window = ui_test.find(self.window_name)
        widgets = ui_window.find_all("**/WindowListComboBox")
        combo_box = widgets[0].widget
        combo_box.model.get_item_value_model(None, 0).set_value(0)

        widgets = ui_window.find_all("**/WidgetInspectorTree")
        self.assertTrue(len(widgets) == 1)
        tree_items = widgets[0].find_all("**")
        labels = [t.widget for t in tree_items if isinstance(t.widget, omni.ui.Label)]

        self.assertTrue(len(labels) == 2)
        self.assertTrue(labels[0].text == "Frame")
        self.assertTrue(labels[1].text == "Style")

    async def test_styles(self):
        ui_window = ui_test.find(self.window_name)
        buttons = ui_window.find_all("**/ToolButton[*]")
        # enable style
        for button in buttons:
            if button.widget.text == "Style":
                await button.click()

        await ui_test.wait_n_updates(2)

        trees = ui_window.find_all("**/WidgetInspectorTree")
        self.assertTrue(len(trees) == 1)
        tree_items = trees[0].find_all("**")
        labels = [t for t in tree_items if isinstance(t.widget, omni.ui.Label)]
        for label in labels:
            if label.widget.text == "Frame":
                await label.click()

        await ui_test.wait_n_updates(2)

        # check there is colorwidget widgets created
        colorwidgets = ui_window.find_all("**/ColorWidget[*]")
        self.assertTrue(len(colorwidgets) > 0)
