import omni.ui as ui
import omni.kit.usd.layers as layers
import omni.kit.widget.live_session_management as lsm

from typing import Callable
from omni.kit.viewport.menubar.core import ViewportMenuDelegate, LabelMenuDelegate


class PeerUserMenuItem(ui.MenuItem):
    """
    A single menu item represent a Live Sesson User
    """
    def __init__(self, live_user: layers.LiveSessionUser, triggered_fn: Callable):
        self.__live_user = live_user

        super().__init__(
            live_user.user_name,
            checkable=True,
            hide_on_click=False,
            triggered_fn=lambda: triggered_fn(live_user),
            delegate=ViewportMenuDelegate(
                force_checked=False,
                build_custom_widgets=lambda _1, _2: self.__build_menuitem_widgets()
            )
        )

    def destroy(self):
        super().destroy()

    def __build_menuitem_widgets(self):
        ui.Spacer(width=10)
        lsm.build_live_session_user_layout(self.__live_user)

    def set_checked(self, checked: bool) -> None:
        self.checked = checked
        self.delegate.checked = checked


class NoPeerUserMenuItem(ui.MenuItem):
    def __init__(self):
        super().__init__(
            "No Users",
            hide_on_click=False,
            delegate=LabelMenuDelegate(enabled=False, width=120, alignment=ui.Alignment.CENTER)
        )
