import omni.ui as ui
import carb
from typing import Callable, Dict, List
from .rtx_settings_widget import RTXSettingsWidget


class RTXSettingsWindow:
    def __init__(self):
        self._window = None
        if carb.settings.get_settings().get("/exts/omni.rtx.window.settings/startup/autoCreateWindow"):
            self._window = ui.Window("Render Settings", dockPreference=ui.DockPreference.RIGHT_TOP)
            self._window.deferred_dock_in("Stage", ui.DockPolicy.TARGET_WINDOW_IS_ACTIVE)
            self._settings_widget = RTXSettingsWidget(self._window.frame)
            self._build_ui()
        else:
            self._settings_widget = RTXSettingsWidget(None)

    def register_renderer(self, name: str, stacks_list: List[str]) -> None:
        return self._settings_widget.register_renderer(name, stacks_list)

    def get_registered_renderers(self):
        return self._settings_widget.get_registered_renderers()

    def get_current_renderer(self):
        return self._settings_widget.get_current_renderer()

    def set_current_renderer(self, renderer_name: str) -> None:
        return self._settings_widget.set_current_renderer(renderer_name)

    def register_stack(self, name: str, stack_class: Callable) -> None:
        return self._settings_widget.register_stack(name, stack_class)

    def show_stack_from_name(self, name: str) -> None:
        return self._settings_widget.show_stack_from_name(name)

    def get_renderer_stacks(self, renderer_name):
        return self._settings_widget.get_renderer_stacks(renderer_name)

    def get_current_stack(self) -> str:
        return self._settings_widget.get_current_stack()

    def unregister_renderer(self, name):
        return self._settings_widget.unregister_renderer(name)

    def unregister_stack(self, name):
        return self._settings_widget.unregister_stack(name)

    def set_visibility_changed_listener(self, listener):
        if self._window:
            self._window.set_visibility_changed_fn(listener)

    def set_render_settings_to_viewport_renderer(self, *args, **kwargs):
        return self._settings_widget.set_render_settings_to_viewport_renderer(*args, **kwargs)

    def set_visible(self, value: bool) -> None:
        if self._window:
            self._window.visible = value

    def destroy(self) -> None:
        """ """
        self._settings_widget.destroy()
        self._settings_widget = None
        self._window = None

    def _build_ui(self) -> None:
        """ """
        self._settings_widget.build_ui()

    def _build_stacks(self) -> None:
        """
        "stacks" are the tabs in the Render Settings UI that contain groups of settings
        They can be reused amongst different renderers.

        This method is called every time the renderer is changed
        """
        self._settings_widget.build_stacks()
