# Copyright (c) 2021-2022, 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__ = ["SpinnerMenuDelegate"]

from typing import Union, Optional
import omni.ui as ui
from omni.ui import color as cl, constant as fl
from omni.kit.widget.spinner import FloatSpinner

SPINNER_STYLE = {
    "Spinner.Field": {"background_color": 0, "color": cl.viewport_menubar_light},
    "Spinner.Field:disabled": {"color": cl.viewport_menubar_medium},
    "Spinner.Arrow": {"background_color": cl.viewport_menubar_light},
    "Spinner.Arrow:disabled": {"background_color": cl.viewport_menubar_medium},
}


class SpinnerMenuDelegate(ui.MenuDelegate):
    """Example of menu item with the spinner"""

    def __init__(
        self,
        model=None,
        tooltip=None,
        width=ui.Fraction(1),
        height=0,
        min: Union[float, int, None] = None,
        max: Union[float, int, None] = None,
        step: Union[float, int] = 1,
        enabled: bool = True,
        text: bool = True,
        icon_name: Optional[str] = None,
        icon_width: ui.Length = 30,
        icon_height: ui.Length = 30,
        use_in_menubar: bool = False,
    ):
        super().__init__()
        self.__width = width
        self.__height = height
        self.__spinner_width = 60
        self.__model = model
        self.__tooltip = tooltip or ""
        self._min = min
        self._max = max
        self._step = step

        self.__enabled = enabled
        self.__frame: Optional[ui.Widget] = None

        self.__text = text
        self.__icon_name = icon_name
        self.__icon_width = icon_width
        self.__icon_height = icon_height
        self.__use_in_menubar = use_in_menubar

    def __del__(self):
        self.destroy()

    def destroy(self):
        self.__model = None

    @property
    def enabled(self) -> bool:
        return self.__enabled

    @enabled.setter
    def enabled(self, value) -> None:
        self.__enabled = value
        if self.__frame:
            self.__frame.enabled = value
            self.__frame.enabled = value

    def build_item(self, item):
        extra_kwargs = {"style_type_name_override": "Menu.Item"} if not self.__use_in_menubar else {}
        self.__frame = ui.Frame(width=self.__width, enabled=self.__enabled, **extra_kwargs)
        with self.__frame:
            with ui.HStack(height=self.__height):
                if self.__icon_name:
                    ui.ImageWithProvider(style_type_name_override="Menu.Item.Icon", tooltip=self.__tooltip, name=self.__icon_name, width=self.__icon_width, height=self.__icon_height)
                if self.__text:
                    ui.Label(item.text, tooltip=self.__tooltip, style_type_name_override="Menu.Item.Label")
                with ui.VStack(width=self.__spinner_width):
                    ui.Spacer()
                    self.__spinner = FloatSpinner(
                        self.__model, min=self._min, max=self._max, step=self._step, style=SPINNER_STYLE
                    )
                    ui.Spacer()
                ui.Spacer(width=5)

        self.__spinner.enabled = self.__enabled
