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

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

from pxr import Sdf
from omni.ui import scene as sc
from omni.kit.viewport.utility import get_active_viewport_window

from .peer_user_list_manipulator_model import PeerUserListManipulatorModel
from .peer_user_list_manipulator import PeerUserListManipulator


class PeerUserList:
    
    def __init__(self, usd_context: omni.usd.UsdContext, prim_path: Sdf.Path):
        self.__usd_context = usd_context
        self.__prim_path = Sdf.Path(prim_path)

        self.__manipulator = None
        self.__manipulator_model = None
        self.__scene_view = None

        self.__build_window()
    
    def destroy(self):
        if self.__manipulator:
            self.__manipulator.destroy()
        self.__manipulator = None

        if self.__manipulator_model:
            self.__manipulator_model.destroy()
        self.__manipulator_model = None

        if self.__viewport_window:
            vp_api = self.__viewport_window.viewport_api
            if self.__scene_view:
                vp_api.remove_scene_view(self.__scene_view)
                self.__scene_view.scene.clear()
                self.__scene_view.destroy()
                self.__scene_view = None
        
            self.__viewport_window = None
    
    def add_user(self, user_info: layers.LiveSessionUser):
        if self.__manipulator_model:
            self.__manipulator_model.add_user(user_info)

    def remove_user(self, user_id):
        if self.__manipulator_model:
            self.__manipulator_model.remove_user(user_id)
    
    def empty_user_list(self):
        if self.__manipulator_model:
            return self.__manipulator_model.empty_user_list()
        
        return True
    
    @property
    def position(self):
        if self.__manipulator_model:
            return self.__manipulator_model._get_position()
        
        return None
    
    def on_transform_changed(self, position=None):
        if self.__manipulator_model:
            self.__manipulator_model.on_transform_changed(position)

    def __build_window(self):
        """Called to build the widgets of the window"""
        self.__viewport_window = get_active_viewport_window()
        frame = self.__viewport_window.get_frame("selection_outline." + str(self.__prim_path))

        with frame:
            self.__scene_view = sc.SceneView()

            with self.__scene_view.scene:
                self.__manipulator_model = PeerUserListManipulatorModel(self.__prim_path, self.__usd_context)
                self.__manipulator = PeerUserListManipulator(model=self.__manipulator_model)
        
        vp_api = self.__viewport_window.viewport_api
        vp_api.add_scene_view(self.__scene_view)
