# 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.
#

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

from .manipulator import Axis, TransformManipulator
from .simple_transform_model import (
    SimpleRotateChangedGesture,
    SimpleScaleChangedGesture,
    SimpleTranslateChangedGesture,
)
from .types import Operation


class Select(sc.ClickGesture):
    def __init__(self, example):
        super().__init__()
        self._example = example

    def on_ended(self):
        self._example.on_point_clicked(self.sender)


class SimpleManipulatorExample:
    def __init__(self):
        projection = [0.011730205278592375, 0.0, 0.0, 0.0]
        projection += [0.0, 0.02055498458376156, 0.0, 0.0]
        projection += [0.0, 0.0, 2.00000020000002e-07, 0.0]
        projection += [-0.0, -0.0, 1.00000020000002, 1.0]
        view = [1.0, 0.0, 0.0, 0.0]
        view += [0.0, 1.0, 0.0, 0.0]
        view += [0.0, 0.0, 1.0, 0.0]
        view += [-2.2368736267089844, 13.669827461242786, -5.0, 1.0]

        self._selected_shape = None

        self._window = ui.Window("Simple Manipulator Example")
        with self._window.frame:
            scene_view = sc.SceneView(projection=projection, view=view)
            with scene_view.scene:
                self._ma = TransformManipulator(
                    size=1,
                    axes=Axis.ALL & ~Axis.Z & ~Axis.SCREEN,
                    enabled=False,
                    gestures=[
                        SimpleTranslateChangedGesture(),
                        SimpleRotateChangedGesture(),
                        SimpleScaleChangedGesture(),
                    ],
                )

                self._sub = self._ma.model.subscribe_item_changed_fn(self._on_item_changed)

                sc.Points([[0, 0, 0]], colors=[ui.color.white], sizes=[10], gestures=[Select(self)])
                sc.Points([[50, 0, 0]], colors=[ui.color.white], sizes=[10], gestures=[Select(self)])
                sc.Points([[50, -50, 0]], colors=[ui.color.white], sizes=[10], gestures=[Select(self)])
                sc.Points([[0, -50, 0]], colors=[ui.color.white], sizes=[10], gestures=[Select(self)])

    def __del__(self):
        self.destroy()

    def destroy(self):
        self._window = None

    def on_point_clicked(self, shape):
        self._selected_shape = shape
        self._ma.enabled = True

        model = self._ma.model
        model.set_floats(
            model.get_item("translate"),
            [
                self._selected_shape.positions[0][0],
                self._selected_shape.positions[0][1],
                self._selected_shape.positions[0][2],
            ],
        )

    def _on_item_changed(self, model, item):
        if self._selected_shape is not None:
            if item.operation == Operation.TRANSLATE:
                self._selected_shape.positions = model.get_as_floats(item)
