# Copyright (c) 2023, NVIDIA CORPORATION.  All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto.  Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
#
__all__ = ["WelcomeWidget"]

import carb
import omni.kit.app
import omni.ui as ui
from typing import Callable, Optional


class WelcomeWidget():
    """The class that represents the window"""

    def __init__(self, buttons: dict, style: Optional[dict] = None, button_size: Optional[tuple] = None, add_background: bool = False):
        if style is None:
            from .style import welcome_widget_style
            style = welcome_widget_style
        if button_size is None:
            button_size = (150, 100)
        self.__add_background = add_background
        # Save the button_size for later use
        self.__buttons = buttons
        self.__button_size = button_size
        # Create the top-level ui.Frame
        self.__frame: ui.Frame = ui.Frame()
        # Apply the style to all the widgets of this window
        self.__frame.style = style
        # Set the function that is called to build widgets when the window is visible
        self.__frame.set_build_fn(self.create_ui)

    def destroy(self):
        # It will destroy all the children
        if self.__frame:
            self.__frame.destroy()
            self.__frame = None

    def create_label(self):
        ui.Label("Select Stage", name="title")

    def create_button(self, label: str, width: float, height: float, clicked_fn: Callable):
        ui.Button(label, width=width, height=height, clicked_fn=clicked_fn)

    def create_buttons(self, width: float, height: float):
        for label, clicked_fn in self.__buttons.items():
            self.create_button(label, width=width, height=height, clicked_fn=clicked_fn)

    def create_background(self):
        bg_color = carb.settings.get_settings().get('/exts/omni.kit.viewport.ready/background_color')
        if bg_color:
            omni.ui.Rectangle(style={"background_color": bg_color})

    def create_ui(self):
        with ui.ZStack():
            if self.__add_background:
                self.create_background()

            with ui.HStack():
                ui.Spacer(width=20)
                with ui.VStack():
                    ui.Spacer()
                    self.create_label()
                    ui.Spacer(height=20)
                    with ui.HStack(height=100):
                        ui.Spacer()
                        self.create_buttons(self.__button_size[0], self.__button_size[1])
                        ui.Spacer()
                    ui.Spacer()
                ui.Spacer(width=20)

    @property
    def visible(self):
        return self.__frame.visible if self.__frame else None

    @visible.setter
    def visible(self, visible: bool):
        if self.__frame:
            self.__frame.visible = visible
        elif visible:
            import carb
            carb.log_error("Cannot make WelcomeWidget visible after it was destroyed")
