# Copyright (c) 2021, 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 omni.kit.app
import omni.kit.test
import omni.timeline
import carb.settings
import omni.kit.commands
from omni.kit import ui_test

class ToolbarTestModels(omni.kit.test.AsyncTestCase):
    async def test_setting_pass_through(self):
        from ..builtin_tools.builtin_tools import LEGACY_SNAP_BUTTON_ENABLED_SETTING_PATH_WINDOW, LEGACY_SNAP_BUTTON_ENABLED_SETTING_PATH_WIDGET

        settings = carb.settings.get_settings()

        orig_value = settings.get(LEGACY_SNAP_BUTTON_ENABLED_SETTING_PATH_WIDGET)

        settings.set_bool(LEGACY_SNAP_BUTTON_ENABLED_SETTING_PATH_WINDOW, True)
        self.assertEqual(settings.get(LEGACY_SNAP_BUTTON_ENABLED_SETTING_PATH_WINDOW), settings.get(LEGACY_SNAP_BUTTON_ENABLED_SETTING_PATH_WIDGET))

        settings.set_bool(LEGACY_SNAP_BUTTON_ENABLED_SETTING_PATH_WINDOW, False)
        self.assertEqual(settings.get(LEGACY_SNAP_BUTTON_ENABLED_SETTING_PATH_WINDOW), settings.get(LEGACY_SNAP_BUTTON_ENABLED_SETTING_PATH_WIDGET))

        settings.set_bool(LEGACY_SNAP_BUTTON_ENABLED_SETTING_PATH_WIDGET, orig_value)

    async def test_select_include_ref_model(self):
        from ..builtin_tools.models.select_include_ref_model import SelectIncludeRefModel
        
        model = SelectIncludeRefModel()
        orig_value = model.get_value_as_bool()
        model.set_value(not orig_value)
        self.assertEqual(model.get_value_as_bool(), not orig_value)
        model.set_value(orig_value)

    async def test_select_mode_model(self):
        from ..builtin_tools.models.select_mode_model import SelectModeModel
        
        model = SelectModeModel()
        orig_value = model.get_value_as_string()
        
        model.set_value(True)
        self.assertEqual(model.get_value_as_string(), SelectModeModel.PICKING_MODE_PRIMS)

        model.set_value(False)
        self.assertEqual(model.get_value_as_string(), SelectModeModel.PICKING_MODE_MODELS)

        model.set_value(SelectModeModel.PICKING_MODE_PRIMS)
        self.assertEqual(model.get_value_as_string(), SelectModeModel.PICKING_MODE_PRIMS)

        model.set_value(orig_value)

    async def test_select_no_kinds_model(self):
        from ..builtin_tools.models.select_no_kinds_model import SelectNoKindsModel
        
        model = SelectNoKindsModel()
        orig_value = model.get_value_as_bool()
        model.set_value(not orig_value)
        self.assertEqual(model.get_value_as_bool(), not orig_value)
        model.set_value(orig_value)

    async def test_setting_model(self):
        from ..builtin_tools.models.setting_model import BoolSettingModel, FloatSettingModel

        settings = carb.settings.get_settings()

        bool_setting_path = '/exts/omni.kit.widget.toolbar/test/boolSetting'
        float_setting_path = '/exts/omni.kit.widget.toolbar/test/floatSetting'

        bool_model = BoolSettingModel(bool_setting_path, False)
        bool_model.set_value(True)
        self.assertEqual(bool_model.get_value_as_bool(), True)
        self.assertEqual(settings.get(bool_setting_path), True)
        bool_model.set_value(False)
        self.assertEqual(bool_model.get_value_as_bool(), False)
        self.assertEqual(settings.get(bool_setting_path), False)

        inverted_bool_model = BoolSettingModel(bool_setting_path, True)
        inverted_bool_model.set_value(True)
        self.assertEqual(inverted_bool_model.get_value_as_bool(), not True)
        self.assertEqual(settings.get(bool_setting_path), not True)
        inverted_bool_model.set_value(False)
        self.assertEqual(inverted_bool_model.get_value_as_bool(), not False)
        self.assertEqual(settings.get(bool_setting_path), not False)

        float_model = FloatSettingModel(float_setting_path)
        float_model.set_value(42.0)
        self.assertEqual(float_model.get_value_as_float(), 42.0)
        self.assertEqual(settings.get(float_setting_path), 42.0)
        float_model.set_value(21.0)
        self.assertEqual(float_model.get_value_as_float(), 21.0)
        self.assertEqual(settings.get(float_setting_path), 21.0)

    async def test_timeline_model(self):
        from ..builtin_tools.models.timeline_model import TimelinePlayPauseModel
        
        model = TimelinePlayPauseModel()
        model.set_value(True)
        await ui_test.human_delay()
        self.assertTrue(model.get_value_as_bool())

        model.set_value(False)
        await ui_test.human_delay()
        self.assertFalse(model.get_value_as_bool())

    async def test_transform_mode_model(self):
        from ..builtin_tools.models.transform_mode_model import TransformModeModel, LocalGlobalTransformModeModel
        
        model = TransformModeModel(TransformModeModel.TRANSFORM_OP_MOVE)
        model.set_value(True)
        self.assertTrue(model.get_value_as_bool())

        settings = carb.settings.get_settings()
        setting_path = '/exts/omni.kit.widget.toolbar/test/localGlobalTransformOp'
        settings.set(setting_path, LocalGlobalTransformModeModel.TRANSFORM_MODE_GLOBAL)

        model = LocalGlobalTransformModeModel(TransformModeModel.TRANSFORM_OP_MOVE, setting_path)
        model.set_value(False)
        self.assertTrue(model.get_op_space_mode() == LocalGlobalTransformModeModel.TRANSFORM_MODE_LOCAL)
        model.set_value(False)
        self.assertTrue(model.get_op_space_mode() == LocalGlobalTransformModeModel.TRANSFORM_MODE_GLOBAL)

    async def test_play_pause_stop_commands(self):
        timeline = omni.timeline.get_timeline_interface()

        omni.kit.commands.execute("ToolbarPlayButtonClicked")
        await ui_test.human_delay()
        self.assertTrue(timeline.is_playing())

        omni.kit.commands.execute("ToolbarPauseButtonClicked")
        await ui_test.human_delay()
        self.assertFalse(timeline.is_playing())

        omni.kit.commands.execute("ToolbarStopButtonClicked")
        await ui_test.human_delay()
        self.assertTrue(timeline.is_stopped())

        settings = carb.settings.get_settings()
        setting_path = '/exts/omni.kit.widget.toolbar/test/playFilter'

        omni.kit.commands.execute("ToolbarPlayFilterChecked", setting_path=setting_path, enabled=True)
        self.assertTrue(settings.get(setting_path))
