from robotpy_ext.control.toggle import Toggle
from robotpy_ext.misc.precise_delay import NotifierDelay


class FakeJoystick:
    def __init__(self):
        self._pressed = [False] * 2

    def getRawButton(self, num):
        return self._pressed[num]

    def press(self, num):
        self._pressed[num] = True

    def release(self, num):
        self._pressed[num] = False


def test_toggle():
    joystick = FakeJoystick()
    toggleButton = Toggle(joystick, 0)
    toggleButton2 = Toggle(joystick, 1)
    assert toggleButton.off
    joystick.press(0)
    assert toggleButton.on
    assert toggleButton2.off
    joystick.release(0)
    assert toggleButton.on
    joystick.press(0)
    assert toggleButton.off
    joystick.release(0)
    assert toggleButton.off
    joystick.press(1)
    assert toggleButton.off
    assert toggleButton2.on


def test_toggle_debounce():
    # TODO: use simulated time
    delay = NotifierDelay(0.5)
    joystick = FakeJoystick()
    toggleButton = Toggle(joystick, 1, 0.1)
    assert toggleButton.off
    joystick.press(1)
    assert toggleButton.on
    joystick.release(1)
    joystick.press(1)
    joystick.release(1)
    assert toggleButton.on
    delay.wait()
    assert toggleButton.on
    joystick.press(1)
    assert toggleButton.off

[((21, 19, 21, 38), 'robotpy_ext.control.toggle.Toggle', 'Toggle', ({(21, 26, 21, 34): 'joystick', (21, 36, 21, 37): '0'}, {}), '(joystick, 0)', False, 'from robotpy_ext.control.toggle import Toggle\n'), ((22, 20, 22, 39), 'robotpy_ext.control.toggle.Toggle', 'Toggle', ({(22, 27, 22, 35): 'joystick', (22, 37, 22, 38): '1'}, {}), '(joystick, 1)', False, 'from robotpy_ext.control.toggle import Toggle\n'), ((40, 12, 40, 30), 'robotpy_ext.misc.precise_delay.NotifierDelay', 'NotifierDelay', ({(40, 26, 40, 29): '0.5'}, {}), '(0.5)', False, 'from robotpy_ext.misc.precise_delay import NotifierDelay\n'), ((42, 19, 42, 43), 'robotpy_ext.control.toggle.Toggle', 'Toggle', ({(42, 26, 42, 34): 'joystick', (42, 36, 42, 37): '1', (42, 39, 42, 42): '0.1'}, {}), '(joystick, 1, 0.1)', False, 'from robotpy_ext.control.toggle import Toggle\n')]