from typing import Dict, List, Optional

import carb.settings
import omni.ui as ui
from omni.ui import constant as fl

from .style import WELCOME_STYLE
from .welcome_model import WelcomeModel
from .welcome_page_item import WelcomePageItem

SETTING_SHOW_AT_STARTUP = "/persistent/exts/omni.kit.welcome.window/visible_at_startup"
SETTING_SHOW_SAS_CHECKBOX = "/exts/omni.kit.welcome.window/show_sas_checkbox"

class WelcomeWindow(ui.Window):
    """
    Window to show welcome content.
    Args:
        visible (bool): If visible when window created.
    """
    def __init__(self, model: WelcomeModel, visible: bool=True):
        self._model = model
        self._delegates: List[WelcomePageItem] = []
        self.__page_frames: Dict[WelcomePageItem, ui.Frame] = {}
        self.__page_buttons: Dict[WelcomePageItem, ui.Button] = {}
        self.__selected_page: Optional[WelcomePageItem] = None

        # TODO: model window?
        # If model window and open extensions manager, new extensions window unavailable
        # And if model window, checkpoing combobox does not pop up droplist because it is a popup window
        flags = (ui.WINDOW_FLAGS_NO_SCROLLBAR | ui.WINDOW_FLAGS_NO_COLLAPSE | ui.WINDOW_FLAGS_MODAL)
        super().__init__("Welcome Screen", flags=flags, width=1380, height=780, padding_x=2, padding_y=2, visible=visible)
        self.frame.set_build_fn(self._build_ui)
        self.frame.set_style(WELCOME_STYLE)

    def destroy(self) -> None:
        super().destroy()
        self.__page_frames = {}
        self.visible = False
        self.__sub_show_at_startup = None

    def _build_ui(self):
        self._pages = self._model.get_item_children(None)
        button_height = fl._find("welcome_page_button_height")

        with self.frame:
            with ui.HStack():
                # Left for buttons to switch different pages
                with ui.VStack(width=250, spacing=0):
                    for page in self._pages:
                        self.__page_buttons[page] = ui.Button(
                            page.text.upper(),
                            height=button_height,
                            image_width=button_height,
                            image_height=button_height,
                            spacing=4,
                            image_url=page.icon,
                            clicked_fn=lambda d=page: self.__show_page(d), style_type_name_override="Page.Button"
                        )
                    ui.Spacer()
                    self.__build_show_at_startup()

                # Right for page content
                with ui.ZStack():
                    for page in self._pages:
                        self.__page_frames[page] = ui.Frame(build_fn=lambda p=page: p.build_ui(), visible=False)

        # Default always show first page
        self.__show_page(self._pages[0])

    def __build_show_at_startup(self) -> None:
        settings = carb.settings.get_settings()
        self.__show_at_startup_model = ui.SimpleBoolModel(settings.get(SETTING_SHOW_AT_STARTUP))

        def __on_show_at_startup_changed(model: ui.SimpleBoolModel):
            settings.set(SETTING_SHOW_AT_STARTUP, model.as_bool)

        self.__sub_show_at_startup = self.__show_at_startup_model.subscribe_value_changed_fn(__on_show_at_startup_changed)

        if settings.get_as_bool(SETTING_SHOW_SAS_CHECKBOX):
            with ui.HStack(height=0, spacing=5):
                ui.Spacer(width=5)
                ui.CheckBox(self.__show_at_startup_model, width=0)
                ui.Label("Show at Startup", width=0)
        ui.Spacer(height=5)

    def __show_page(self, page: WelcomePageItem):
        if self.__selected_page:
            self.__page_frames[self.__selected_page].visible = False
            self.__page_buttons[self.__selected_page].selected = False
            self.__page_buttons[self.__selected_page].image_url = self.__selected_page.icon
        if page:
            self.__page_frames[page].visible = True
            self.__page_buttons[page].selected = True
            self.__page_buttons[page].image_url = page.active_icon
        self.__selected_page = page
