# Copyright (c) 2022, 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.
#

import omni.ext
from omni.kit.manipulator.viewport import ManipulatorFactory

from .prim_transform_manipulator import PrimTransformManipulator
from .prim_transform_manipulator_registry import TransformManipulatorRegistry
from .reference_prim_marker import ReferencePrimMarker
from .tools import PrimManipTools


class ManipulatorPrim(omni.ext.IExt):
    def on_startup(self, ext_id):
        self._tools = PrimManipTools()

        # For VP1
        self._legacy_manipulator = PrimTransformManipulator()
        self._legacy_marker = ManipulatorFactory.create_manipulator(
            ReferencePrimMarker, usd_context_name="", manipulator_model=self._legacy_manipulator.model, legacy=True
        )

        # For VP2
        self._manipulator_registry = TransformManipulatorRegistry()

    def on_shutdown(self):
        if self._legacy_manipulator is not None:
            self._legacy_manipulator.destroy()
            self._legacy_manipulator = None

        if self._legacy_marker is not None:
            ManipulatorFactory.destroy_manipulator(self._legacy_marker)
            self._legacy_marker = None

        if self._manipulator_registry is not None:
            self._manipulator_registry.destroy()
            self._manipulator_registry = None

        if self._tools is not None:
            self._tools.destroy()
            self._tools = None
