"""Pure functions for structured room display."""

from __future__ import annotations

from mudlib.timeofday import get_sky_description, get_time_period


def render_where(zone_name: str) -> str:
    """Render the zone description line.

    Args:
        zone_name: The zone's description/name to display

    Returns:
        Formatted string like "Where: The Overworld"
    """
    return f"Where: {zone_name}"


def render_location(zone, x: int, y: int) -> str:
    """Render location with quadrant and coordinates.

    Args:
        zone: The zone object (needs width and height attributes)
        x: X coordinate
        y: Y coordinate

    Returns:
        Formatted string like "Location: northeast 47, 53"
    """
    # Calculate quadrant based on zone thirds
    third_w = zone.width / 3
    third_h = zone.height / 3

    # Determine horizontal component (west/center/east)
    if x < third_w:
        h_part = "west"
    elif x < 2 * third_w:
        h_part = "center"
    else:
        h_part = "east"

    # Determine vertical component (north/center/south)
    if y < third_h:
        v_part = "north"
    elif y < 2 * third_h:
        v_part = "center"
    else:
        v_part = "south"

    # Combine into quadrant name
    if h_part == "center" and v_part == "center":
        quadrant = "center"
    elif h_part == "center":
        quadrant = v_part
    elif v_part == "center":
        quadrant = h_part
    else:
        quadrant = f"{v_part}{h_part}"

    return f"Location: {quadrant} {x}, {y}"


def render_nearby(entities: list, viewer) -> str:
    """Render nearby entities line.

    Args:
        entities: List of entities in viewport but not on viewer's tile
        viewer: The viewing entity (unused for now)

    Returns:
        Formatted string like "Nearby: (3) saibaman / Master Roshi / Goku"
        or empty string if no nearby entities
    """
    if not entities:
        return ""

    count = len(entities)
    names = " / ".join(entity.name for entity in entities)
    return f"Nearby: ({count}) {names}"


def render_exits(zone, x: int, y: int) -> str:
    """Render available exits from current position.

    Args:
        zone: The zone object (needs is_passable method)
        x: Current X coordinate
        y: Current Y coordinate

    Returns:
        Formatted string like "Exits: north south east west"
    """
    exits = []

    # Check cardinal directions
    # NOTE: up/down exits deferred until z-axis movement is implemented
    if zone.is_passable(x, y - 1):  # north (y decreases going up)
        exits.append("north")
    if zone.is_passable(x, y + 1):  # south
        exits.append("south")
    if zone.is_passable(x + 1, y):  # east
        exits.append("east")
    if zone.is_passable(x - 1, y):  # west
        exits.append("west")

    if exits:
        return f"Exits: {' '.join(exits)}"
    return "Exits:"


_POSTURE_MESSAGES = {
    "standing": "is standing here.",
    "resting": "is resting here.",
    "flying": "is flying above.",
    "fighting": "is fighting here.",
    "unconscious": "is unconscious.",
}


def render_entity_lines(entities: list, viewer) -> str:
    """Render entity descriptions based on posture.

    Args:
        entities: List of entities on viewer's tile (excluding viewer)
        viewer: The viewing entity (unused for now)

    Returns:
        Newline-separated entity descriptions, or empty string if none
    """
    if not entities:
        return ""

    lines = []
    for entity in entities:
        # Get posture with fallback to "standing"
        posture = getattr(entity, "posture", "standing")
        msg = _POSTURE_MESSAGES.get(posture, "is standing here.")
        lines.append(f"{entity.name} {msg}")

    return "\n".join(lines)


def render_atmosphere(hour: int, weather_desc: str, season: str) -> str:
    """Render atmosphere line combining time-of-day and weather.

    Args:
        hour: Game hour (0-23)
        weather_desc: Weather description (empty string for clear weather)
        season: Season name ("spring", "summer", "autumn", "winter")

    Returns:
        Formatted atmosphere line
    """
    sky = get_sky_description(hour)
    period = get_time_period(hour)

    if weather_desc:
        return f"{sky}. {weather_desc}. [{period}, {season}]"
    else:
        return f"{sky}. [{period}, {season}]"
