import pytest
import time

from misty2py.basic_skills import (
    cancel_skills,
    expression,
    free_memory,
    movement,
    speak,
)
from misty2py.robot import Misty
from misty2py.utils.env_loader import EnvLoader

env_loader = EnvLoader()

MISTY = Misty(env_loader.get_ip())


def test_cancel_skills():
    result = cancel_skills.cancel_skills(MISTY)
    assert result.get("overall_success")


def test_free_memory():
    result = free_memory.free_memory(MISTY, overwrite=True)
    assert result.get("overall_success")


def test_speak():
    phrase = "Hello!"
    result = speak.speak(MISTY, phrase)
    assert (
        result.get("overall_success")
        and result.get("rest_response").get("message")
        == "Talking successful. Utterance: `%s`." % phrase
    )


def test_expression_a():
    result = expression.expression(
        MISTY,
        image="image_disgust",
        sound="sound_disoriented_4",
        colour_type="trans",
        colour="green_light",
        image_offset=0.1,
        sound_offset=0.2,
        colour_offset=0,
        duration=1,
    )
    assert result.get("overall_success")


def test_expression_b():
    result = expression.expression(MISTY, colour="cyan_light")
    assert result.get("overall_success")


def test_expression_c():
    result = expression.expression(
        MISTY,
        colour_type="trans",
        colours={"col1": "cyan_light", "col2": "green_light"},
    )
    assert result.get("overall_success")


driving = movement.Movement()


def test_drive_forward():
    result = driving.drive_forward(MISTY, 20)
    time.sleep(0.5)
    driving.stop_driving(MISTY)
    assert result.get("overall_success")


def test_drive_backward():
    result = driving.drive_backward(MISTY, 20)
    time.sleep(0.5)
    driving.stop_driving(MISTY)
    assert result.get("overall_success")


def test_drive_left():
    result = driving.drive_left(MISTY, 20, 50)
    time.sleep(0.5)
    driving.stop_driving(MISTY)
    assert result.get("overall_success")


def test_drive_right():
    result = driving.drive_right(MISTY, 20, 50)
    time.sleep(0.5)
    driving.stop_driving(MISTY)
    assert result.get("overall_success")


@pytest.mark.parametrize(
    "in_val, out_val",
    [(11, -10), (9, -9), (0, 0), (-5, -5), (-11, -10)],
)
def test_parse_vals_negative(in_val, out_val):
    assert (
        driving._parse_value(
            in_val, max=10, min=1, negative=True, negatives_allowed=False
        )
        == out_val
    )


@pytest.mark.parametrize(
    "in_val, out_val",
    [(11, 10), (-11, -10), (0, 0), (3, 3), (-7, -7)],
)
def test_parse_vals_negatives_allowed(in_val, out_val):
    assert (
        driving._parse_value(
            in_val, max=10, min=1, negative=False, negatives_allowed=True
        )
        == out_val
    )
