__all__ = ["MenuBarLiveSessionCameraFollowerButtonDelegate", "MenuBarLiveSessionCameraFollowerMenuItemDelegate"]

import omni.ui as ui

from omni.kit.viewport.menubar.camera import AbstractCameraButtonDelegate, AbstractCameraMenuItemDelegate
from omni.kit.widget.live_session_management import LiveSessionCameraFollowerList
from pxr import Sdf
from typing import Optional


class MenuBarLiveSessionCameraFollowerBaseDelegate:
    def __init__(self):
        super().__init__()
        self.__all_follower_widgets = {}
    
    def build_widget_with_option(
        self, parent_id, viewport_api, camera_path, camera_button_or_menu_item
    ) -> Optional[ui.Widget]:
        widget = ui.VStack(width=0)
        with widget:
            ui.Spacer()
            follower_widget = LiveSessionCameraFollowerList(
                viewport_api.usd_context, camera_path,
                show_my_following_users=camera_button_or_menu_item
            )
            ui.Spacer()
        
        old_widget = self.__all_follower_widgets.pop(parent_id, None)
        if old_widget:
            old_widget.destroy()

        self.__all_follower_widgets[parent_id] = follower_widget
        
        return widget
        
    def on_camera_path_changed(self, parent_id, new_camera_path: Sdf.Path):
        follower_widget = self.__all_follower_widgets.get(parent_id, None)
        if follower_widget:
            follower_widget.track_camera(new_camera_path)
        
    def on_parent_destroyed(self, parent_id):
        follower_widget = self.__all_follower_widgets.get(parent_id, None)
        if follower_widget:
            follower_widget.on_parent_destroyed()

    def destroy(self):
        for follower_widget in self.__all_follower_widgets.values():
            follower_widget.destroy()
        
        self.__all_follower_widgets.clear()


class MenuBarLiveSessionCameraFollowerButtonDelegate(
    MenuBarLiveSessionCameraFollowerBaseDelegate, AbstractCameraButtonDelegate
):
    
    def __init__(self):
        super().__init__()
    
    def build_widget(self, parent_id, viewport_api, camera_path) -> Optional[ui.Widget]:
        return super().build_widget_with_option(parent_id, viewport_api, camera_path, True)
    
    def on_camera_path_changed(self, parent_id, new_camera_path: Sdf.Path):
        super().on_camera_path_changed(parent_id, new_camera_path)
        
    def on_parent_destroyed(self, parent_id):
        super().on_parent_destroyed(parent_id)
    
    def destroy(self):
        super().destroy()


class MenuBarLiveSessionCameraFollowerMenuItemDelegate(
    MenuBarLiveSessionCameraFollowerBaseDelegate, AbstractCameraMenuItemDelegate
):
    
    def __init__(self):
        super().__init__()
    
    def build_widget(self, parent_id, viewport_api, camera_path) -> Optional[ui.Widget]:
        return super().build_widget_with_option(parent_id, viewport_api, camera_path, False)
    
    def on_camera_path_changed(self, parent_id, new_camera_path: Sdf.Path):
        super().on_camera_path_changed(parent_id, new_camera_path)
        
    def on_parent_destroyed(self, parent_id):
        super().on_parent_destroyed(parent_id)
    
    def destroy(self):
        super().destroy()
    