from typing import Callable
from .viewport_menu_delegate import ViewportMenuDelegate
from ..model.category_model import CategoryStatus
import omni.ui as ui
import carb.windowing

__all__ = ["CategoryMenuDelegate"]

class CategoryMenuDelegate(ViewportMenuDelegate):
    def __init__(self, status: CategoryStatus, icon_clicked_fn: Callable[[None], None]=None):
        self._status = status
        super().__init__(icon_clicked_fn=icon_clicked_fn)

    def build_item(self, item: ui.MenuItem):
        # icons required
        super().build_item(item)
        if self.icon:
            self.icon.name = self._status
            self.icon.set_mouse_hovered_fn(self._on_icon_hovered)

    @property
    def status(self) -> CategoryStatus:
        return self._status

    @status.setter
    def status(self, value: CategoryStatus) -> None:
        self._status = value
        if self.icon:
            self.icon.name = self._status

    def _on_icon_hovered(self, hovered):
        try:
            import omni.kit.window.cursor
            main_curosr = omni.kit.window.cursor.get_main_window_cursor()
            if main_curosr:
                if hovered:
                    main_curosr.override_cursor_shape(carb.windowing.CursorStandardShape.HAND)
                else:
                    main_curosr.clear_overridden_cursor_shape()
        except ImportError:
            carb.log_info("omni.kit.window.cursor not available, omni.kit.viewport.menubar cursor won't be changed")
            pass
