"""Tests for MTTS capability parsing."""

from mudlib.caps import parse_mtts


def test_parse_mtts_tintin():
    """Parse tintin++ MTTS 2825 correctly.

    2825 in binary = 0b101100001001
    Bits set: 0, 3, 8, 9, 11
    = ANSI(1) + 256colors(8) + truecolor(256) + MNES(512) + SSL(2048)
    """
    caps = parse_mtts("MTTS 2825")
    assert caps.ansi is True
    assert caps.vt100 is False
    assert caps.utf8 is False
    assert caps.colors_256 is True
    assert caps.mouse_tracking is False
    assert caps.osc_color_palette is False
    assert caps.screen_reader is False
    assert caps.proxy is False
    assert caps.truecolor is True
    assert caps.mnes is True
    assert caps.mslp is False
    assert caps.ssl is True
    assert caps.color_depth == "truecolor"


def test_parse_mtts_basic():
    """Parse MTTS 137 (ANSI + 256colors + proxy)."""
    # 137 = 1 (ANSI) + 8 (256colors) + 128 (proxy)
    caps = parse_mtts("MTTS 137")
    assert caps.ansi is True
    assert caps.utf8 is False
    assert caps.colors_256 is True
    assert caps.proxy is True
    assert caps.truecolor is False
    assert caps.color_depth == "256"


def test_parse_mtts_zero():
    """Parse MTTS 0 (nothing supported)."""
    caps = parse_mtts("MTTS 0")
    assert caps.ansi is False
    assert caps.utf8 is False
    assert caps.colors_256 is False
    assert caps.truecolor is False
    assert caps.color_depth == "16"


def test_parse_mtts_256_colors_only():
    """Parse MTTS with JUST 256 colors (no truecolor)."""
    # 9 = 1 (ANSI) + 8 (256colors)
    caps = parse_mtts("MTTS 9")
    assert caps.ansi is True
    assert caps.utf8 is False
    assert caps.colors_256 is True
    assert caps.truecolor is False
    assert caps.color_depth == "256"


def test_parse_mtts_utf8_no_colors():
    """Parse MTTS with UTF-8 but no extended colors."""
    # 5 = 1 (ANSI) + 4 (UTF-8)
    caps = parse_mtts("MTTS 5")
    assert caps.ansi is True
    assert caps.utf8 is True
    assert caps.colors_256 is False
    assert caps.truecolor is False
    assert caps.color_depth == "16"


def test_parse_mtts_empty():
    """Handle empty string gracefully."""
    caps = parse_mtts("")
    assert caps.ansi is False
    assert caps.utf8 is False
    assert caps.color_depth == "16"


def test_parse_mtts_none():
    """Handle None gracefully."""
    caps = parse_mtts(None)
    assert caps.ansi is False
    assert caps.utf8 is False
    assert caps.color_depth == "16"


def test_parse_mtts_non_mtts():
    """Handle non-MTTS ttype3 strings (e.g., UNKNOWN-TERMINAL)."""
    caps = parse_mtts("UNKNOWN-TERMINAL")
    assert caps.ansi is False
    assert caps.utf8 is False
    assert caps.color_depth == "16"


def test_parse_mtts_malformed():
    """Handle malformed MTTS strings."""
    caps = parse_mtts("MTTS")
    assert caps.ansi is False
    assert caps.utf8 is False
    assert caps.color_depth == "16"


def test_color_depth_priority():
    """Verify color_depth returns best available mode."""
    # truecolor wins
    caps = parse_mtts("MTTS 2825")
    assert caps.color_depth == "truecolor"

    # 256 wins over 16
    caps = parse_mtts("MTTS 9")
    assert caps.color_depth == "256"

    # fallback to 16
    caps = parse_mtts("MTTS 5")  # 1 (ANSI) + 4 (UTF-8)
    assert caps.color_depth == "16"
