# 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__ = ["PeerUserCamera"]

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_camera_manipulator_model import PeerUserCameraManipulatorModel
from .peer_user_camera_manipulator import PeerUserCameraManipulator


class PeerUserCamera:
    
    def __init__(self, user_info: layers.LiveSessionUser, camera_path, usd_context):
        self.__camera_path = Sdf.Path(camera_path)
        self.__user_info = user_info
        self.__usd_context = usd_context

        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 set_tracked_camera(self, camera_path):
        camera_path = Sdf.Path(camera_path)
        if self.__camera_path != camera_path:
            self.__camera_path = camera_path
            if self.__manipulator_model:
                self.__manipulator_model.set_tracked_camera(self.__camera_path)
    
    @property
    def position(self):
        """Realtime position of the camera prim."""
        if self.__manipulator_model:
            return self.__manipulator_model.get_camera_position()
        
        return None
    
    def on_view_translation_changed(self, viewport_camera_position=None):
        """When the view pos from the current viewport camera is changed."""
        if self.__manipulator_model:
            self.__manipulator_model.on_view_translation_changed(viewport_camera_position)

    def on_world_translation_changed(self, position=None):
        if self.__manipulator_model:
            self.__manipulator_model.on_world_translation_changed(position)
        
    def on_viewport_camera_changed(self, position):
        if self.__manipulator:
            self.__manipulator.on_viewport_camera_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("omni.kit.collaboration.viewport.camera." + self.__user_info.user_id)

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

            with self.__scene_view.scene:
                self.__manipulator_model = PeerUserCameraManipulatorModel(
                    self.__scene_view, self.__camera_path, self.__usd_context
                )

                self.__manipulator = PeerUserCameraManipulator(
                    user_info=self.__user_info,
                    model=self.__manipulator_model,
                    usd_context=self.__usd_context
                )
        
        vp_api = self.__viewport_window.viewport_api
        vp_api.add_scene_view(self.__scene_view)
