## Copyright (c) 2018-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__ = ["PeerUserListManipulator"]

import omni.ui as ui
import omni.kit.usd.layers as layers

from omni.ui import scene as sc
from omni.ui import color as cl


class PeerUserListManipulator(sc.Manipulator):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.__position_transform = None
        self.__user_list_transform = None

        self.__hover_gesture = sc.HoverGesture(
            on_began_fn=lambda sender: self.__show_tooltip(sender),
            on_ended_fn=lambda sender: self.__show_tooltip(sender)
        )
    
    def destroy(self):
        self.__hover_gesture = None
        self.__clear()
    
    def __clear(self):
        if self.__position_transform:
            self.__position_transform.clear()
            self.__position_transform = None

        if self.__user_list_transform:
            self.__user_list_transform.clear()
            self.__user_list_transform = None
    
    def __show_tooltip(self, sender):
        pass

    def on_build(self):
        """Called when the model is changed and rebuilds the whole slider"""
        if not self.model:
            return

        position = self.model.get_as_floats(self.model.get_item("position"))
        if not position:
            return
        
        user_list_item = self.model.get_item("user_list")
        if not user_list_item:
            return

        self.__position_transform = sc.Transform(transform=sc.Matrix44.get_translation_matrix(*position))
        with self.__position_transform:
            with sc.Transform(look_at=sc.Transform.LookAt.CAMERA):
                with sc.Transform(scale_to=sc.Space.SCREEN):
                    self.__user_list_transform = sc.Transform(transform=sc.Matrix44.get_translation_matrix(0, 30, 0))
                    with self.__user_list_transform:
                        self.__build_user_list(user_list_item.value)
    
    def __invalidate(self):
        self.__clear()
        self.invalidate()
    
    def __build_user_list(self, user_list):
        size = min(3, len(user_list))
        index = 0
        current_offset = 0
        space = 4
        icon_size = 28

        self.__user_list_transform.clear()
        with self.__user_list_transform:
            for user_info in user_list.values():
                if index == size:
                    with sc.Transform(transform=sc.Matrix44.get_translation_matrix(current_offset, 0, 0)):
                        sc.Label("...", color=cl.white, alignment=ui.Alignment.CENTER, size=28)
                    break

                with sc.Transform(transform=sc.Matrix44.get_translation_matrix(current_offset, 0, 0)):
                    color = ui.color(*user_info.user_color)
                    sc.Arc(radius=icon_size, color=color)
                    sc.Label(
                        layers.get_short_user_name(user_info.user_name), color=cl.white,
                        alignment=ui.Alignment.CENTER, size=16
                    )

                current_offset += icon_size * 2 + space

                index += 1
                if index > size:
                    break

    def on_model_updated(self, item):
        if not self.__position_transform or not self.__user_list_transform or not item:
            self.__invalidate()
            return

        position_item = self.model.get_item("position")
        user_list_item = self.model.get_item("user_list")
        if item == position_item:
            position = self.model.get_as_floats(position_item)
            if not position:
                return
            
            self.__position_transform.transform = sc.Matrix44.get_translation_matrix(*position)
        elif item == user_list_item:
            self.__build_user_list(user_list_item.value)
