## Copyright (c) 2023, 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 os
import carb
import omni.kit.app
from omni.kit.test.async_unittest import AsyncTestCase
import omni.usd

from omni.kit import ui_test
from pxr import Vt, Gf
from omni.kit.test_suite.helpers import open_stage, get_test_data_path, select_prims, wait_stage_loading, wait_for_window, arrange_windows
from omni.kit.quicklayout import QuickLayout

PERSISTENT_SETTINGS_PREFIX = "/persistent"


class TestQuicklayoutPreferences(AsyncTestCase):
    # Before running each test
    async def setUp(self):
        await arrange_windows()
        carb.settings.get_settings().set(PERSISTENT_SETTINGS_PREFIX + "/app/quicklayout/defaultLayout", "$$last_used$$")
        self.assertEqual(QuickLayout.get_default_layouts(), [])
        data_path = get_test_data_path(__name__)
        self._layout_data = [ ["Test 1", f"{data_path}/layouts/test1.json", carb.input.KeyboardInput.KEY_1],
                              ["Test 2", f"{data_path}/layouts/test2.json", carb.input.KeyboardInput.KEY_2]]
        QuickLayout.add_default_layouts(self._layout_data)

    # After running each test
    async def tearDown(self):
        QuickLayout.remove_default_layouts(self._layout_data)
        carb.settings.get_settings().set(PERSISTENT_SETTINGS_PREFIX + "/app/quicklayout/defaultLayout", "$$last_used$$")
        self.assertEqual(QuickLayout.get_default_layouts(), [])

    async def test_layout_preferences(self):
        omni.kit.window.preferences.show_preferences_window()
        await ui_test.human_delay(50)

        for page in omni.kit.window.preferences.get_page_list():
            if page.get_title() == "Template Startup":
                omni.kit.window.preferences.select_page(page)
                await ui_test.human_delay(50)

                frame = ui_test.find("Preferences//Frame/**/CollapsableFrame[*].identifier=='preferences_builder_Default Layout'")
                frame.find("**/ComboBox[*].identifier=='/persistent/app/quicklayout/defaultLayout'").model._current_index.as_int = 1
                await ui_test.human_delay(10)
                self.assertEqual(carb.settings.get_settings().get(PERSISTENT_SETTINGS_PREFIX + "/app/quicklayout/defaultLayout"), "Test 1")

                frame = ui_test.find("Preferences//Frame/**/CollapsableFrame[*].identifier=='preferences_builder_Default Layout'")
                frame.find("**/ComboBox[*].identifier=='/persistent/app/quicklayout/defaultLayout'").model._current_index.as_int = 0
                await ui_test.human_delay(10)
                self.assertEqual(carb.settings.get_settings().get(PERSISTENT_SETTINGS_PREFIX + "/app/quicklayout/defaultLayout"), "$$last_used$$")
                return

        self.assertTrue(False, "Quicklayout preferences page not found")
