import omni.kit.test
import carb.settings
from ..settings_model import VectorFloatSettingsModel, RadioButtonSettingModel


class TestModel(omni.kit.test.AsyncTestCase):
    async def test_vector_model(self):

        setting_name = "/rtx/pathtracing/maxBlah"
        settings = carb.settings.get_settings()

        initial_vals = (0.6, 0.7, 0.8)
        settings.set(setting_name, initial_vals)

        vector_model = VectorFloatSettingsModel(setting_name, 3, True)

        # Lets simulate a bit of what the color widget would do when getting values
        sub_items = vector_model.get_item_children(None)
        for cnt, sub_item in enumerate(sub_items):
            sub_model = vector_model.get_item_value_model(sub_item)
            val = sub_model.get_value_as_float()
            self.assertTrue(val == initial_vals[cnt])

        # Lets check if things work when we change the value
        new_vals = (0.5, 0.4, 0.3)
        settings.set(setting_name, new_vals)
        for cnt, sub_item in enumerate(sub_items):
            sub_model = vector_model.get_item_value_model(sub_item)
            val = sub_model.get_value_as_float()
            self.assertTrue(val == new_vals[cnt])

        # Let's set the value through the item model
        new_vals = [0.1, 0.15, 0.2]
        for cnt, sub_item in enumerate(sub_items):
            sub_model = vector_model.get_item_value_model(sub_item)
            val = sub_model.set_value(new_vals[cnt])
        settings_val = settings.get(setting_name)
        self.assertTrue(settings_val == new_vals)

        # Let's set the value directly through the Vector model
        new_vals = [0.2, 0.8, 0.9]
        vector_model.set_value(new_vals)
        settings_val = settings.get(setting_name)
        self.assertTrue(settings_val == new_vals)

        # TODO test begin_edit/end_edit
        # TODO test other sizes (2, 4 components
        # TODO test immediate mode on/off

    async def test_radio_button_setting_model(self):
        setting_value_path = "/ext/ui/settings/radiobutton/value"
        setting_items_path = "/ext/ui/settings/radiobutton/items"
        settings = carb.settings.get_settings()

        initial_val = "option1"
        items = ("option0", "option1", "option2", "option3")

        settings.set(setting_value_path, initial_val)
        settings.set(setting_items_path, items)

        radio_button_model = RadioButtonSettingModel(setting_value_path)

        # Lets check the initial_val and items
        self.assertEqual(radio_button_model.items, items)
        self.assertEqual(radio_button_model.get_value(), initial_val)
        self.assertEqual(radio_button_model.get_value_as_int(), 1)

        # Lets check if things work when we change the value
        # Set as int
        new_val = "option0"
        radio_button_model.set_value(0)
        self.assertEqual(radio_button_model.get_value(), new_val)
        self.assertEqual(settings.get(setting_value_path), new_val)
        self.assertEqual(radio_button_model.get_value_as_int(), 0)

        # Set as str
        new_val = "option2"
        radio_button_model.set_value(new_val)
        self.assertEqual(radio_button_model.get_value(), new_val)
        self.assertEqual(settings.get(setting_value_path), new_val)
        self.assertEqual(radio_button_model.get_value_as_int(), 2)

        # Let's the the value through settings
        new_val = "option3"
        settings.set(setting_value_path, new_val)
        self.assertEqual(radio_button_model.get_value(), new_val)
        self.assertEqual(radio_button_model.get_value_as_int(), 3)
