#!/usr/bin/env python3

# 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 carb
import omni.kit.test
import omni.usd
from omni.kit import ui_test

settings = carb.settings.get_settings()

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

    async def setUp(self):
        super().setUp()
        await omni.usd.get_context().new_stage_async()
        await omni.kit.app.get_app().next_update_async()

    def get_render_setings_window(self):
        render_settings_window = ui_test.find("Render Settings")
        render_settings_window.widget.height = 600
        render_settings_window.widget.width = 600
        render_settings_window.widget.visible = True
        return render_settings_window

    def assertSetting(self, setting_name, setting_value):
        self.assertTrue(settings.get(setting_name) == setting_value)

    async def test_settings(self):
        # Set initial state
        roi_min_org = [0, 0, 0]
        roi_max_org = [10, 10, 10]
        settings.set("/nvindex/dataBboxMin", roi_min_org)
        settings.set("/nvindex/dataBboxMax", roi_max_org)
        settings.set("/rtx/index/regionOfInterestMin", roi_min_org)
        settings.set("/rtx/index/regionOfInterestMax", roi_max_org)

        render_settings_window = self.get_render_setings_window()
        self.assertTrue(render_settings_window)

        # Select IndeX "Cluster" settings in the global "Render Settings" tab
        button = render_settings_window.find("Frame/VStack[0]/VStack[0]/HStack[0]/RadioButton[0]")
        self.assertTrue(button)
        await button.click()

        # Cluster / Cluster Compositing Settings
        collapsable_frame = render_settings_window.find("**/Cluster Compositing Settings")
        self.assertTrue(collapsable_frame)
        collapsable_frame.widget.collapsed = False
        await omni.kit.app.get_app().next_update_async()

        # Select IndeX "Common" settings in the global "Render Settings" tab
        button = render_settings_window.find("Frame/VStack[0]/VStack[0]/HStack[0]/RadioButton[1]")
        self.assertTrue(button)
        await button.click()

        # Common / Scene Settings
        collapsable_frame = render_settings_window.find("**/Scene Settings")
        self.assertTrue(collapsable_frame)
        collapsable_frame.widget.collapsed = False
        await omni.kit.app.get_app().next_update_async()

        setting_name = "/rtx/index/regionOfInterest"
        roi_min_org = settings.get(setting_name + "Min")
        roi_max_org = settings.get(setting_name + "Max")

        setting_hstack = collapsable_frame.find("/**/HStack_Region_of_Interest_Min")
        self.assertTrue(isinstance(setting_hstack.widget, omni.ui.HStack))

        setting_hstack = collapsable_frame.find("/**/HStack_Region_of_Interest_Max")
        self.assertTrue(isinstance(setting_hstack.widget, omni.ui.HStack))

        settings.set(setting_name + "Max", [5, 10, 10])
        await omni.kit.app.get_app().next_update_async()

        self.assertSetting(setting_name + "Min", roi_min_org)
        self.assertSetting(setting_name + "Max", [5, 10, 10])

        button = collapsable_frame.find("/**/Button[0]")
        self.assertTrue(button)
        await button.click();

        self.assertSetting(setting_name + "Min", roi_min_org)
        self.assertSetting(setting_name + "Max", roi_max_org)

        # Common / Render Settings
        collapsable_frame = render_settings_window.find("**/Render Settings")
        self.assertTrue(collapsable_frame)
        collapsable_frame.widget.collapsed = False
        await omni.kit.app.get_app().next_update_async()

        setting_hstack = collapsable_frame.find("/**/HStack_Background_Color")
        self.assertTrue(isinstance(setting_hstack.widget, omni.ui.HStack))

        # Background color
        setting_name = "/rtx/index/backgroundColor"
        self.assertSetting(setting_name, [0.0, 0.0, 0.0]) # default

        background_color_field = setting_hstack.find("**/MultiFloatDragField[0]")
        self.assertTrue(background_color_field)

        color_model = background_color_field.model
        sub_models = color_model.get_item_children(None)
        color_model.get_item_value_model(sub_models[0]).set_value(1.0)
        color_model.get_item_value_model(sub_models[2]).set_value(0.25)

        await omni.kit.app.get_app().next_update_async()
        self.assertSetting(setting_name, [1.0, 0.0, 0.25])
