# retoor <retoor@molodetz.nl>

from devplacepy.services.devii.interaction.parse import parse_plain_reply
from devplacepy.services.devii.interaction.schema import validate_prompt_args


def test_parse_confirm_yes():
    req = validate_prompt_args(
        {
            "title": "Delete?",
            "widgets": [{"type": "confirm", "name": "delete", "label": "Delete"}],
        }
    )
    status, values, err = parse_plain_reply(req, "yes")
    assert status == "submitted"
    assert values == {"delete": True}
    assert err is None


def test_parse_choice_by_index():
    req = validate_prompt_args(
        {
            "title": "Env?",
            "widgets": [
                {
                    "type": "choice",
                    "name": "env",
                    "label": "Env",
                    "options": [
                        {"value": "production", "label": "Production"},
                        {"value": "staging", "label": "Staging"},
                    ],
                }
            ],
        }
    )
    status, values, err = parse_plain_reply(req, "2")
    assert status == "submitted"
    assert values == {"env": "staging"}


def test_parse_cancel():
    req = validate_prompt_args(
        {
            "title": "Env?",
            "widgets": [
                {
                    "type": "choice",
                    "name": "env",
                    "label": "Env",
                    "options": [{"value": "a", "label": "A"}],
                }
            ],
        }
    )
    status, values, err = parse_plain_reply(req, "cancel")
    assert status == "cancelled"
    assert values == {}


def test_parse_date_european():
    req = validate_prompt_args(
        {
            "title": "When?",
            "widgets": [{"type": "date", "name": "day", "label": "Day"}],
        }
    )
    status, values, err = parse_plain_reply(req, "13/01/1990")
    assert status == "submitted"
    assert values == {"day": "13/01/1990"}
    assert err is None


def test_parse_date_iso_normalized_to_european():
    req = validate_prompt_args(
        {
            "title": "When?",
            "widgets": [{"type": "date", "name": "day", "label": "Day"}],
        }
    )
    status, values, err = parse_plain_reply(req, "1990-01-13")
    assert status == "submitted"
    assert values == {"day": "13/01/1990"}


def test_parse_confirm_dutch_ja():
    req = validate_prompt_args(
        {
            "title": "Ok?",
            "widgets": [{"type": "confirm", "name": "ok", "label": "Ok"}],
        }
    )
    status, values, err = parse_plain_reply(req, "ja")
    assert status == "submitted"
    assert values == {"ok": True}


def test_parse_choice_by_label():
    req = validate_prompt_args(
        {
            "title": "Lang?",
            "widgets": [
                {
                    "type": "choice",
                    "name": "lang",
                    "label": "Language",
                    "options": [
                        {"value": "python", "label": "Python"},
                        {"value": "rust", "label": "Rust"},
                    ],
                }
            ],
        }
    )
    status, values, err = parse_plain_reply(req, "Rust")
    assert status == "submitted"
    assert values == {"lang": "rust"}
