# Copyright (c) 2021, 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__ = ["ParticipantsColumnDelegate"]

from omni.kit.widget.stage.abstract_stage_column_delegate import AbstractStageColumnDelegate, StageColumnItem
from omni.kit.widget.stage import StageItem
from pxr import UsdGeom
from ..icons import Icons
from typing import List

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


class ParticipantsColumnDelegate(AbstractStageColumnDelegate):
    """The column delegate that represents the Live column"""

    def __init__(self):
        super().__init__()

        self.WIDGET_STYLE = {
            "TreeView.Header::participants": {"image_url": Icons().get("participants"), "color": 0xFF888888},
        }

        self.__user_list = {}

    def destroy(self):
        for user_list in self.__user_list.values():
            user_list.destroy()
        self.__user_list = {}

    @property
    def initial_width(self):
        """The width of the column"""
        return ui.Pixel(64)

    def build_header(self, **kwargs):
        """Build the header"""
        with ui.HStack(style=self.WIDGET_STYLE):
            ui.Spacer()
            with ui.VStack(width=0):
                ui.Spacer()
                ui.Image(width=22, height=14, name="participants", style_type_name_override="TreeView.Header")
                ui.Spacer()
            ui.Spacer()

    async def build_widget(self, item: StageColumnItem, **kwargs):
        """Build the participants widget"""
        stage_item = kwargs.get("stage_item", None)
        if not stage_item:
            return

        user_list = self.__user_list.pop(stage_item, None)
        if user_list:
            user_list.destroy()

        prim = stage_item.prim
        if not prim:
            return

        payrefs = stage_item.payrefs
        if not prim.IsA(UsdGeom.Camera) and not payrefs:
            return

        with ui.HStack():
            ui.HStack(width=4)
            with ui.HStack():
                ui.Spacer()
                if payrefs:
                    user_list = lsm.LiveSessionUserList(
                        stage_item.usd_context, payrefs[0],
                        show_myself=False, maximum_users=2,
                        prim_path=stage_item.path
                    )
                else:
                    user_list = lsm.LiveSessionCameraFollowerList(
                        stage_item.usd_context, stage_item.path, maximum_users=2,
                        show_my_following_users=True
                    )
                self.__user_list[stage_item] = user_list
                ui.Spacer()
            ui.HStack(width=4)

    def on_stage_items_destroyed(self, items: List[StageItem]):
        if not items:
            return

        for item in items:
            user_list = self.__user_list.pop(item, None)
            if user_list:
                user_list.destroy()

    @property
    def order(self):
        return -105

    @property
    def sortable(self):
        return False

    @property
    def minimum_width(self):
        return ui.Pixel(40)
