from mudlib.render.ansi import (
    RESET,
    bg_256,
    bg_rgb,
    colorize_map,
    colorize_terrain,
    fg_256,
    fg_rgb,
)


def test_colorize_terrain_grass():
    """Grass is colored green."""
    result = colorize_terrain(".")
    assert "\033[" in result  # has ANSI code
    assert "." in result
    assert RESET in result


def test_colorize_terrain_mountain():
    """Mountain is colored."""
    result = colorize_terrain("^")
    assert "\033[" in result
    assert "^" in result
    assert RESET in result


def test_colorize_terrain_water():
    """Water is colored blue."""
    result = colorize_terrain("~")
    assert "\033[" in result
    assert "~" in result
    assert RESET in result


def test_colorize_terrain_unknown():
    """Unknown characters pass through unchanged."""
    result = colorize_terrain("?")
    assert result == "?"


def test_colorize_map():
    """colorize_map returns newline-separated colored rows."""
    grid = [
        [".", ".", "T"],
        ["~", "^", "."],
    ]
    result = colorize_map(grid)

    assert "\n" in result
    lines = result.split("\n")
    assert len(lines) == 2

    # should have ANSI codes
    assert "\033[" in result


def test_fg_256_basic():
    """Generate 256-color foreground code."""
    assert fg_256(0) == "\033[38;5;0m"
    assert fg_256(15) == "\033[38;5;15m"
    assert fg_256(42) == "\033[38;5;42m"
    assert fg_256(255) == "\033[38;5;255m"


def test_bg_256_basic():
    """Generate 256-color background code."""
    assert bg_256(0) == "\033[48;5;0m"
    assert bg_256(15) == "\033[48;5;15m"
    assert bg_256(42) == "\033[48;5;42m"
    assert bg_256(255) == "\033[48;5;255m"


def test_fg_256_clamping():
    """Clamp 256-color values to valid range (0-255)."""
    assert fg_256(-1) == "\033[38;5;0m"
    assert fg_256(256) == "\033[38;5;255m"
    assert fg_256(999) == "\033[38;5;255m"


def test_bg_256_clamping():
    """Clamp 256-color values to valid range (0-255)."""
    assert bg_256(-1) == "\033[48;5;0m"
    assert bg_256(256) == "\033[48;5;255m"
    assert bg_256(999) == "\033[48;5;255m"


def test_fg_rgb_basic():
    """Generate truecolor foreground code."""
    assert fg_rgb(0, 0, 0) == "\033[38;2;0;0;0m"
    assert fg_rgb(255, 255, 255) == "\033[38;2;255;255;255m"
    assert fg_rgb(128, 64, 192) == "\033[38;2;128;64;192m"


def test_bg_rgb_basic():
    """Generate truecolor background code."""
    assert bg_rgb(0, 0, 0) == "\033[48;2;0;0;0m"
    assert bg_rgb(255, 255, 255) == "\033[48;2;255;255;255m"
    assert bg_rgb(128, 64, 192) == "\033[48;2;128;64;192m"


def test_fg_rgb_clamping():
    """Clamp RGB values to valid range (0-255)."""
    assert fg_rgb(-1, 0, 0) == "\033[38;2;0;0;0m"
    assert fg_rgb(0, 300, 0) == "\033[38;2;0;255;0m"
    assert fg_rgb(0, 0, 999) == "\033[38;2;0;0;255m"
    assert fg_rgb(-10, 300, 500) == "\033[38;2;0;255;255m"


def test_bg_rgb_clamping():
    """Clamp RGB values to valid range (0-255)."""
    assert bg_rgb(-1, 0, 0) == "\033[48;2;0;0;0m"
    assert bg_rgb(0, 300, 0) == "\033[48;2;0;255;0m"
    assert bg_rgb(0, 0, 999) == "\033[48;2;0;0;255m"
    assert bg_rgb(-10, 300, 500) == "\033[48;2;0;255;255m"


def test_colorize_terrain_default_16color():
    """Default color_depth uses 16-color palette."""
    result = colorize_terrain(".")
    assert "\033[32m" in result  # GREEN
    assert "." in result
    assert RESET in result


def test_colorize_terrain_256color():
    """color_depth=256 uses 256-color palette."""
    result = colorize_terrain(".", "256")
    assert "\033[38;5;" in result  # 256-color code
    assert "." in result
    assert RESET in result


def test_colorize_terrain_truecolor():
    """color_depth=truecolor uses RGB palette."""
    result = colorize_terrain(".", "truecolor")
    assert "\033[38;2;" in result  # truecolor code
    assert "." in result
    assert RESET in result


def test_colorize_map_default_16color():
    """colorize_map with default uses 16-color palette."""
    grid = [[".", "~"], ["^", "T"]]
    result = colorize_map(grid)
    assert "\033[32m" in result  # GREEN for grass
    assert "\033[34m" in result  # BLUE for water


def test_colorize_map_256color():
    """colorize_map with color_depth=256 uses 256-color palette."""
    grid = [[".", "~"]]
    result = colorize_map(grid, "256")
    assert "\033[38;5;" in result  # 256-color codes present


def test_colorize_map_truecolor():
    """colorize_map with color_depth=truecolor uses RGB palette."""
    grid = [[".", "~"]]
    result = colorize_map(grid, "truecolor")
    assert "\033[38;2;" in result  # truecolor codes present
