from typing import Type
import omni.kit.test

import omni.kit.capture.viewport.capture_options as _capture_options


class TestCaptureOptions(omni.kit.test.AsyncTestCase):

    async def test_capture_options_serialisation(self):
        options = _capture_options.CaptureOptions()
        data_dict = options.to_dict()

        self.assertIsInstance(data_dict, dict)

    async def test_capture_options_deserialisation(self):
        options = _capture_options.CaptureOptions()
        data_dict = options.to_dict()
        regenerated_options = _capture_options.CaptureOptions.from_dict(data_dict)

        self.assertIsInstance(regenerated_options, _capture_options.CaptureOptions)

    async def test_capture_options_values_persisted(self):
        options = _capture_options.CaptureOptions(camera="my_camera")
        data_dict = options.to_dict()
        regenerated_options = _capture_options.CaptureOptions.from_dict(data_dict)

        self.assertEqual(regenerated_options.camera, "my_camera")

    async def test_adding_random_attribute_fails(self):
        """ Test that adding new attributes without making them configurable via the __init__ will raise an exception
        """
        options = _capture_options.CaptureOptions()
        options._my_new_value = "foo"
        data_dict = options.to_dict()
        with self.assertRaises(TypeError, msg="__init__() got an unexpected keyword argument 'my_new_value'"):
            regnerated = _capture_options.CaptureOptions.from_dict(data_dict)

    async def test_capture_options_unsupported_exr_compression_method(self):
        options = _capture_options.CaptureOptions(exr_compression_method="sth_wrong")
        self.assertEqual(options.exr_compression_method, "zips")

    async def test_capture_options_mp4_encoding_settings(self):
        options = _capture_options.CaptureOptions(
            mp4_encoding_bitrate=4194304,
            mp4_encoding_iframe_interval=10,
            mp4_encoding_preset="PRESET_LOSSLESS_HP",
            mp4_encoding_profile="H264_PROFILE_PROGRESSIVE_HIGH",
            mp4_encoding_rc_mode="RC_VBR_HQ",
            mp4_encoding_rc_target_quality=51,
            mp4_encoding_video_full_range_flag=True,
        )
        self.assertEqual(options.mp4_encoding_bitrate, 4194304)
        self.assertEqual(options.mp4_encoding_iframe_interval, 10)
        self.assertEqual(options.mp4_encoding_preset, "PRESET_LOSSLESS_HP")
        self.assertEqual(options.mp4_encoding_profile, "H264_PROFILE_PROGRESSIVE_HIGH")
        self.assertEqual(options._mp4_encoding_rc_mode, "RC_VBR_HQ")
        self.assertEqual(options._mp4_encoding_rc_target_quality, 51)
        self.assertEqual(options.mp4_encoding_video_full_range_flag, True)

    async def test_capture_options_unsupported_mp4_encoding_settings(self):
        options = _capture_options.CaptureOptions(
            mp4_encoding_bitrate=4194304,
            mp4_encoding_iframe_interval=10,
            mp4_encoding_preset="PRESET_NOT_EXISTING",
            mp4_encoding_profile="PROFILE_NOT_EXISTING",
            mp4_encoding_rc_mode="RC_NOT_EXISTING",
            mp4_encoding_rc_target_quality=52,
            mp4_encoding_video_full_range_flag=True,
        )
        self.assertEqual(options.mp4_encoding_bitrate, 4194304)
        self.assertEqual(options.mp4_encoding_iframe_interval, 10)
        self.assertEqual(options.mp4_encoding_preset, "PRESET_DEFAULT")
        self.assertEqual(options.mp4_encoding_profile, "H264_PROFILE_HIGH")
        self.assertEqual(options._mp4_encoding_rc_mode, "RC_VBR")
        self.assertEqual(options._mp4_encoding_rc_target_quality, 0)
        self.assertEqual(options.mp4_encoding_video_full_range_flag, True)
