# 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__ = ["ViewportMenuItem"]

from ..viewport_menu_model import AbstractViewportMenuItem
from typing import Callable, Dict, Optional
import omni.ui as ui


class ViewportMenuItem(AbstractViewportMenuItem):
    def __init__(
        self,
        name: str = "",
        icon: str = "",  # glyph?
        hide_on_click: bool = True,
        appear_after: str = "",  # order/priority?
        onclick_fn: Callable = None,
        delegate: ui.MenuDelegate = None,
        visible_setting_path: Optional[str] = None,
        order_setting_path: Optional[str] = None,
        expand_setting_path: Optional[str] = None,
        style: Dict = {},
        order: int = None # deprecated legacy interface
    ):
        self._icon = icon
        self._hide_on_click = hide_on_click
        self._appear_after = appear_after
        self._onclick_fn = onclick_fn
        self._delegate = delegate
        self._style = style

        self._menu_item: Optional[ui.MenuItem] = None

        super().__init__(
            name,
            visible_setting_path=visible_setting_path,
            order_setting_path=order_setting_path,
            expand_setting_path=expand_setting_path,    
        )

        if (order_setting_path is None) and (order is not None):
            import carb
            carb.log_warn(f"ViewportMenuItem order argument is deprecated, use order_setting_path")
            self.order_model = ui.SimpleIntModel(order)

    def destroy(self):
        self._onclick_fn = None
        self._delegate = None
        super().destroy()

    @property
    def visible(self) -> bool:
        return self.visible_model.as_bool

    @visible.setter
    def visible(self, value: bool) -> None:
        self.visible_model.set_value(value)
        if self._menu_item:
            self._menu_item.visible = value

    @property
    def menu_item(self) -> ui.MenuItem:
        return self._menu_item

    def build_fn(self, factory: Dict):
        """Reimplement it to have own customized item"""
        self._menu_item = ui.MenuItem(
            self.name,
            triggered_fn=self._onclick_fn,
            delegate=self._delegate,
            hide_on_click=self._hide_on_click,
            visible=self.visible_model.as_bool,
            style=self._style,
        )

    def invalidate(self):
        pass
