from typing import Optional

from attr import has
from omni import ui
from omni.ui import constant as fl

from ..model.combobox_model import ComboBoxModel
from .abstract_widget_menu_delegate import AbstractWidgetMenuDelegate

__all__ = ["ComboBoxMenuDelegate"]


class ComboBoxMenuDelegate(AbstractWidgetMenuDelegate):
    """Generic menu delegate with combo box"""

    def __init__(
        self,
        model: Optional[ComboBoxModel] = None,
        height=0,
        width=300,
        enabled: bool = True,
        text: bool = True,
        icon_name: Optional[str] = None,
        icon_width: ui.Length = 30,
        icon_height: ui.Length = 30,
        tooltip: Optional[str] = None,
        use_in_menubar: bool = False,
        has_reset: bool = False,
    ):
        super().__init__(
            model=model, height=height, width=width, enabled=enabled, has_reset=has_reset, use_in_menubar=use_in_menubar
        )
        self.__combo_width = 90
        self.__text = text
        self.__icon_name = icon_name
        self.__icon_width = icon_width
        self.__icon_height = icon_height
        self.__tooltip = tooltip

    def __del__(self):
        if self._model:
            self._model.destroy()
        self.destroy()

    def destroy(self):
        # TODO: We don't have to destroy the model here. We should do it in the
        # object created the model. But since the model is not stored anywhere,
        # it's destroyed here.
        if self._model:
            self._model.destroy()
            self._model = None
        super().destroy()

    def build_widget(self, item: ui.MenuHelper):
        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, style_type_name_override="Menu.Item.Label")
        with ui.VStack(width=self.__combo_width):
            ui.Spacer()
            ui.ComboBox(self._model, height=0)
            ui.Spacer()
