"""Tests for room rendering functions."""

from mudlib.render.room import (
    render_atmosphere,
    render_entity_lines,
    render_exits,
    render_location,
    render_nearby,
    render_where,
)


class MockZone:
    """Mock zone for testing."""

    def __init__(self, width=100, height=100, passable_tiles=None):
        self.width = width
        self.height = height
        self.description = "Test Zone"
        self._passable = passable_tiles or set()

    def is_passable(self, x, y):
        return (x, y) in self._passable


class MockEntity:
    """Mock entity for testing."""

    def __init__(self, name, posture="standing"):
        self.name = name
        self.posture = posture


def test_render_where():
    """render_where should format zone description."""
    assert render_where("The Overworld") == "Where: The Overworld"


def test_render_location_center():
    """render_location should show center quadrant."""
    zone = MockZone(width=90, height=90)
    assert render_location(zone, 45, 45) == "Location: center 45, 45"


def test_render_location_northeast():
    """render_location should show northeast quadrant."""
    zone = MockZone(width=90, height=90)
    assert render_location(zone, 70, 10) == "Location: northeast 70, 10"


def test_render_location_southwest():
    """render_location should show southwest quadrant."""
    zone = MockZone(width=90, height=90)
    assert render_location(zone, 10, 70) == "Location: southwest 10, 70"


def test_render_nearby_empty():
    """render_nearby should return empty string when no entities."""
    assert render_nearby([], None) == ""


def test_render_nearby_with_entities():
    """render_nearby should show count and names."""
    entities = [MockEntity("Goku"), MockEntity("Vegeta")]
    result = render_nearby(entities, None)
    assert result == "Nearby: (2) Goku / Vegeta"


def test_render_exits_all_directions():
    """render_exits should list all passable directions."""
    zone = MockZone(passable_tiles={(5, 4), (5, 6), (6, 5), (4, 5)})
    assert render_exits(zone, 5, 5) == "Exits: north south east west"


def test_render_exits_partial():
    """render_exits should list only passable directions."""
    zone = MockZone(passable_tiles={(5, 4), (6, 5)})
    assert render_exits(zone, 5, 5) == "Exits: north east"


def test_render_exits_none():
    """render_exits should show 'Exits:' with no directions if trapped."""
    zone = MockZone(passable_tiles=set())
    assert render_exits(zone, 5, 5) == "Exits:"


def test_render_entity_lines_empty():
    """render_entity_lines should return empty string when no entities."""
    assert render_entity_lines([], None) == ""


def test_render_entity_lines_with_postures():
    """render_entity_lines should show entity names with postures."""
    entities = [
        MockEntity("Krillin", "resting"),
        MockEntity("Piccolo", "standing"),
    ]
    result = render_entity_lines(entities, None)
    assert "Krillin is resting here." in result
    assert "Piccolo is standing here." in result


def test_render_atmosphere_clear_weather():
    """render_atmosphere should omit weather desc when clear."""
    result = render_atmosphere(12, "", "summer")
    assert "[day, summer]" in result
    # Should have sky description but no weather text
    assert result.count(". [") == 1  # only one period before bracket
    # Should not have double period before bracket
    assert ". . [" not in result


def test_render_atmosphere_with_rain():
    """render_atmosphere should include weather desc when not clear."""
    result = render_atmosphere(12, "rain patters steadily", "spring")
    assert "rain patters steadily" in result
    assert "[day, spring]" in result


def test_render_atmosphere_dawn():
    """render_atmosphere should show dawn period."""
    result = render_atmosphere(5, "", "autumn")
    assert "[dawn, autumn]" in result


def test_render_atmosphere_dusk():
    """render_atmosphere should show dusk period."""
    result = render_atmosphere(18, "", "winter")
    assert "[dusk, winter]" in result


def test_render_atmosphere_night():
    """render_atmosphere should show night period."""
    result = render_atmosphere(22, "", "spring")
    assert "[night, spring]" in result


def test_render_atmosphere_with_heavy_snow():
    """render_atmosphere should include heavy weather descriptions."""
    result = render_atmosphere(22, "heavy snow blankets everything", "winter")
    assert "heavy snow blankets everything" in result
    assert "[night, winter]" in result
    # Should have format: sky. weather. [period, season]
    parts = result.split(". ")
    assert len(parts) == 3  # sky, weather, [period, season]
