"""Paint mode commands for terrain editing."""

from mudlib.commands import CommandDefinition, register
from mudlib.player import Player


async def cmd_paint(player: Player, args: str) -> None:
    """Toggle paint mode on/off.

    Paint mode allows admin to edit terrain tile-by-tile.
    """
    if player.paint_mode:
        # Exit paint mode
        player.paint_mode = False
        player.painting = False
        await player.send("Paint mode off.\r\n")
    else:
        # Enter paint mode
        player.paint_mode = True
        player.painting = False
        await player.send(
            "Paint mode on. Use 'p' to toggle painting, "
            "type a character to set brush.\r\n"
        )


async def cmd_toggle_painting(player: Player, args: str) -> None:
    """Toggle between survey and painting states within paint mode."""
    if not player.paint_mode:
        await player.send("You must be in paint mode to do that.\r\n")
        return

    player.painting = not player.painting
    if player.painting:
        await player.send(f"Painting with '{player.paint_brush}'.\r\n")
    else:
        await player.send("Survey mode.\r\n")


async def cmd_set_brush(player: Player, args: str) -> None:
    """Set the brush character for painting."""
    if not player.paint_mode:
        await player.send("You must be in paint mode to do that.\r\n")
        return

    char = args.strip()
    if len(char) != 1:
        await player.send("Brush must be a single character.\r\n")
        return

    player.paint_brush = char
    await player.send(f"Brush set to '{char}'.\r\n")


# Register paint mode commands
register(CommandDefinition("@paint", cmd_paint, admin=True))
register(CommandDefinition("p", cmd_toggle_painting, admin=True))
register(CommandDefinition("brush", cmd_set_brush, admin=True))
