from typing import Callable, List, Optional

import carb
import carb.settings
import omni.ui as ui

from .welcome_page_item import WelcomePageItem

SETTING_WELCOME_PAGES = "/app/welcome/page"


class WelcomeModel(ui.AbstractItemModel):
    def __init__(self):
        super().__init__()

        self.__settings = carb.settings.get_settings()
        self._items = [WelcomePageItem(page) for page in self.__settings.get(SETTING_WELCOME_PAGES)]
        self._items.sort(key=lambda item: item.order)

    def get_item_children(self, item: Optional[WelcomePageItem]) -> List[WelcomePageItem]:
        return self._items

    def register_page(self, ext_id: str, build_fn: Callable[[None], None]) -> bool:
        for item in self._items:
            if item.extension_id == ext_id:
                item.build_fn = build_fn
                return True
        carb.log_warn(f"Extension {ext_id} not defined in welcome window.")
        return False
