import omni.kit.test
from .test_base import KitLaunchTest

# fmt: off

class TestPersistentSettings(KitLaunchTest):
    async def test_persistent_settings(self):

        kit_file_name = "test_persistent_settings.kit"

        # In total in that test we have 3 setting values: regular, persistent set in kit file, persistent new
        kit_file = """
[settings]
test_banana.just_some_value = 2
persistent.test_banana.saved_thing = 10
"""
        # Functions / header to use in all scripts
        script_base = """
import carb.settings
s = carb.settings.get_settings()
def check(path, expected):
    v = s.get(path)
    if v != expected:
        print(f"[Test Fail] setting path: {path}, expected: {expected}, got: {v}")
        omni.kit.app.get_app().post_quit(-1)
"""

        # Clean start, set a bunch of settings persistent or not
        script = script_base + """
check("/test_banana/just_some_value", 2)
check("/persistent/test_banana/saved_thing", 10)
s.set("/test_banana/just_some_value", 3)
s.set("/persistent/test_banana/saved_thing", 1515)
s.set("/persistent/test_banana/another_saved_thing", 78)
"""
        await self._run_kit_with_script(script, args=["--reset-user"], kit_file=kit_file, kit_file_name=kit_file_name)

        # Check persistent kept
        script = script_base + """
check("/test_banana/just_some_value", 2)
check("/persistent/test_banana/saved_thing", 1515)
check("/persistent/test_banana/another_saved_thing", 78)
"""
        await self._run_kit_with_script(script, args=[], kit_file=kit_file, kit_file_name=kit_file_name)

        # Check that in different app or with different config path they are not loaded:
        script = script_base + """
check("/test_banana/just_some_value", 2)
check("/persistent/test_banana/saved_thing", 10)
check("/persistent/test_banana/another_saved_thing", None)
"""
        await self._run_kit_with_script(script, args=[], kit_file=kit_file, kit_file_name="test_persistent_settings_other_app.kit")
        await self._run_kit_with_script(script, args=["--/app/userConfigPath='${data}/lol.json'"], kit_file=kit_file, kit_file_name=kit_file_name)

        # Check we can override them with cmd
        script = script_base + """
check("/test_banana/just_some_value", 333)
check("/persistent/test_banana/saved_thing", 888)
check("/persistent/test_banana/another_saved_thing", 999)
"""
        args = [
               "--/persistent/test_banana/saved_thing=888",
               "--/persistent/test_banana/another_saved_thing=999",
               "--/test_banana/just_some_value=333"
        ]
        await self._run_kit_with_script(script, args=args, kit_file=kit_file, kit_file_name=kit_file_name)

        # Run without args to check that persistent kept
        script = script_base + """
check("/test_banana/just_some_value", 2)
check("/persistent/test_banana/saved_thing", 888)
check("/persistent/test_banana/another_saved_thing", 999)
"""
        await self._run_kit_with_script(script, args=[], kit_file=kit_file, kit_file_name=kit_file_name)

        # Reset settings
        script = script_base + """
check("/test_banana/just_some_value", 2)
check("/persistent/test_banana/saved_thing", 10)
check("/persistent/test_banana/another_saved_thing", None)
"""
        await self._run_kit_with_script(script, args=["--reset-user"], kit_file=kit_file, kit_file_name=kit_file_name)

# fmt: on
