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

__all__ = ["ViewportSetup"]

import omni.kit.test
from omni.kit.test.async_unittest import AsyncTestCase
import omni.usd
import carb

from pxr import UsdGeom


class ViewportSetup(AsyncTestCase):
    # Before running each test
    async def setUp(self):
        super().setUp()

    async def test_camera_startup_values(self):
        usd_context = omni.usd.get_context()
        await usd_context.new_stage_async()

        stage = usd_context.get_stage()
        self.assertIsNotNone(stage)

        persp = UsdGeom.Camera.Get(stage, '/OmniverseKit_Persp')
        self.assertIsNotNone(persp)
        self.assertAlmostEqual(persp.GetFocalLengthAttr().Get(), 18.147562, places=5)
        self.assertEqual(persp.GetFStopAttr().Get(), 0)

        top = UsdGeom.Camera.Get(stage, '/OmniverseKit_Top')
        self.assertIsNotNone(top)
        self.assertEqual(top.GetHorizontalApertureAttr().Get(), 5000)
        # Legacy Viewport does some legacy hi-jinks based on resolution
        # self.assertEqual(top.GetVerticalApertureAttr().Get(), 5000)

        # Test typed-defaults and creation
        settings = carb.settings.get_settings()
        try:
            perps_key = '/persistent/app/primCreation/typedDefaults/camera'
            ortho_key = '/persistent/app/primCreation/typedDefaults/orthoCamera'
            settings.set(perps_key + '/focalLength', 150)
            settings.set(perps_key + '/fStop', 22)
            settings.set(perps_key + '/horizontalAperture', 1234)
            settings.set(ortho_key + '/horizontalAperture', 1000)
            settings.set(ortho_key + '/verticalAperture', 2000)

            await usd_context.new_stage_async()

            stage = usd_context.get_stage()
            self.assertIsNotNone(stage)

            persp = UsdGeom.Camera.Get(stage, '/OmniverseKit_Persp')
            self.assertIsNotNone(persp)
            self.assertAlmostEqual(persp.GetFocalLengthAttr().Get(), 150)
            self.assertAlmostEqual(persp.GetFStopAttr().Get(), 22)
            self.assertAlmostEqual(persp.GetHorizontalApertureAttr().Get(), 1234)
            # Legacy Viewport does some legacy hi-jinks based on resolution
            # self.assertFalse(persp.GetVerticalApertureAttr().IsAuthored())

            top = UsdGeom.Camera.Get(stage, '/OmniverseKit_Top')
            self.assertIsNotNone(top)
            # Legacy Viewport does some legacy hi-jinks based on resolution
            # self.assertAlmostEqual(top.GetHorizontalApertureAttr().Get(), 1000)
            # self.assertAlmostEqual(top.GetVerticalApertureAttr().Get(), 2000)
        finally:
            # Reset now for all other tests
            settings.destroy_item(perps_key)
            settings.destroy_item(ortho_key)
