import omni.kit.test
from omni.kit.widget.spinner import FloatSpinner, IntSpinner, IntDragSpinner
from omni.ui.tests.test_base import OmniUiTest
import omni.kit.app
import asyncio
import carb
import copy
import omni.ui as ui
from omni.ui import color as cl
import asyncio

SPINNER_STYLE = {
    "Spinner.Field": {"background_color": 0, "color": cl.viewport_menubar_light},
    "Spinner.Field:disabled": {"color": cl.viewport_menubar_medium},
    "Spinner.Arrow": {"background_color": cl.viewport_menubar_light},
    "Spinner.Arrow:disabled": {"background_color": cl.viewport_menubar_medium},
}

class TestSpinner(OmniUiTest):
    # Before running each test
    async def setUp(self):
        await super().setUp()

    # After running each test
    async def tearDown(self):
        await super().tearDown()

    async def test_sliderbar(self):
        float_spinner = FloatSpinner(step=0.1, min=0, max=10, style=SPINNER_STYLE)
        self.assertEqual(float_spinner._field.model.as_float, 0.0)
        float_spinner._begin_change(0, 0.1)
        self.assertEqual(float_spinner._field.model.as_float, 0.1)
        float_spinner._end_change()

        # Test this change is reduced to value + 0.1
        float_spinner._begin_change(0, 0.10000001)
        self.assertEqual(float_spinner._field.model.as_float, 0.2)
        float_spinner._end_change()

        float_spinner._begin_change(1, 0.10000001)
        self.assertEqual(float_spinner._field.model.as_float, 0.2)
        float_spinner._end_change()

        # Test for no change
        float_spinner._begin_change(0, 0.100001)
        self.assertEqual(float_spinner._field.model.as_float, 0.300001)
        float_spinner._end_change()
        float_spinner.enabled = False
        self.assertFalse(float_spinner.enabled)
        float_spinner._begin_change(0, 0.100001)
        self.assertEqual(float_spinner._field.model.as_float, 0.300001)
        float_spinner._end_change()

        int_spinner = IntSpinner(step=1, min=0, max=10, vertical=False)
        self.assertEqual(int_spinner._field.model.as_int, 0)
        int_spinner._begin_change(0, 1)
        self.assertEqual(int_spinner._field.model.as_int, 1)
        int_spinner._end_change()

        int_drag_spinner = IntDragSpinner(step=1, min=0, max=10)
        self.assertEqual(int_drag_spinner._field.model.as_int, 0)
        int_drag_spinner._begin_change(0, 1)
        self.assertEqual(int_drag_spinner._field.model.as_int, 1)
        # Check for long click
        await asyncio.sleep(0.7)
        self.assertEqual(int_drag_spinner._field.model.as_int, 2)
        int_drag_spinner._end_change()