import carb
import carb.settings
import omni.kit.app
import omni.ui as ui


HUMAN_DELAY_SETTING = "/exts/omni.kit.ui_test/humanDelay"


class TestingPropertiesWindow:
    def __init__(self):
        self._window = ui.Window("Testing Properties", width=400, height=200, flags=ui.WINDOW_FLAGS_NO_DOCKING)
        self._window.visible = False

    def destroy(self):
        self._window = None

    def show(self):
        if not self._window.visible:
            self._window.visible = True
            self.refresh()

    def refresh(self):
        with self._window.frame:
            with ui.VStack(height=0):

                settings = carb.settings.get_settings()

                ui.Spacer(height=10)
                ui.Label("Test Settings:", style={"color": 0xFFB7F222, "font_size": 16})
                ui.Spacer(height=5)

                for key in ["/exts/omni.kit.test/includeTests", "/exts/omni.kit.test/excludeTests"]:
                    value = settings.get(key)
                    ui.Label(f"{key}: {value}")
                    ui.Spacer(height=5)

                # Setting specific to UI tests
                manager = omni.kit.app.get_app().get_extension_manager()
                if manager.is_extension_enabled("omni.kit.ui_test"):

                    ui.Spacer(height=10)
                    ui.Label("UI Test (omni.kit.ui_test) Settings:", style={"color": 0xFFB7F222, "font_size": 16})
                    ui.Spacer(height=5)

                    with ui.HStack(height=0):
                        ui.Label("UI Test Delay (s)")
                        delay_widget = ui.FloatDrag(min=0, max=1000000)
                        delay_widget.model.set_value(settings.get_as_float(HUMAN_DELAY_SETTING))
                        delay_widget.model.add_value_changed_fn(
                            lambda m: settings.set(HUMAN_DELAY_SETTING, m.get_value_as_float())
                        )
