from rich import box
from rich.align import Align
from rich.console import Group
from rich.layout import Layout
from rich.panel import Panel
from rich.text import Text

from .theme import (
    APP_LOGO,
    APP_SUBTITLE,
    APP_TITLE,
    FOOTER_BORDER,
    HEADER_BORDER,
    HISTORY_BORDER,
    STATUS_BORDER,
)


def _status_style(status: str) -> str:
    lowered = status.lower()
    if "error" in lowered or "failed" in lowered:
        return "bold red3"
    if "listening" in lowered or "wake word" in lowered:
        return "bold yellow3"
    if any(token in lowered for token in ("playing", "paused", "resumed", "volume", "skipped")):
        return "bold spring_green3"
    return "bold bright_white"


def generate_boot_banner() -> Panel:
    logo = Text.from_markup(f"[spring_green3]{APP_LOGO}[/spring_green3]")
    subtitle = Text.from_markup(f"[hot_pink2]{APP_SUBTITLE}[/hot_pink2]")
    hints = Text.from_markup(
        "[bright_cyan]Wake:[/] [white]'hey spotify'[/]    "
        "[bright_cyan]Try:[/] [white]play after hours[/], [white]volume 40[/], [white]next[/]"
    )

    return Panel(
        Group(
            Align.center(logo),
            Align.center(subtitle),
            Align.center(Text.from_markup("[grey74]Voice-first music terminal[/grey74]")),
            Align.center(hints),
        ),
        title="[bold hot_pink2]BOOT SEQUENCE[/bold hot_pink2]",
        subtitle="[bold white]ready for command capture[/bold white]",
        border_style=HEADER_BORDER,
        box=box.DOUBLE,
        padding=(1, 2),
    )


def generate_layout(current_song: str = "None", status: str = "Waiting...", last_command: str = "N/A") -> Layout:
    """
    Creates a full-screen layout for the hey-spotify CLI.
    """
    layout = Layout()
    layout.split_column(
        Layout(name="header", size=8),
        Layout(name="body", ratio=1),
        Layout(name="footer", size=3),
    )

    header_group = Group(
        Align.center(Text.from_markup(f"[bold hot_pink2]{APP_TITLE}[/bold hot_pink2]")),
        Align.center(Text.from_markup("[spring_green3]| ||| ||||| ||| |[/spring_green3]")),
        Align.center(Text.from_markup("[grey70]Hands-free Spotify control in your terminal[/grey70]")),
    )
    layout["header"].update(
        Panel(
            header_group,
            border_style=HEADER_BORDER,
            box=box.HEAVY,
        )
    )

    layout["body"].split_row(
        Layout(name="status_panel", ratio=5),
        Layout(name="log_panel", ratio=4),
    )

    playback_text = Text()
    playback_text.append("NOW PLAYING\n", style="bold bright_white")
    playback_text.append(f"{current_song}\n\n", style="spring_green3")
    playback_text.append("SYSTEM STATUS\n", style="bold bright_white")
    playback_text.append(status, style=_status_style(status))

    layout["status_panel"].update(
        Panel(
            playback_text,
            title="[bold spring_green3]Playback Deck[/bold spring_green3]",
            border_style=STATUS_BORDER,
            box=box.ROUNDED,
            padding=(1, 2),
        )
    )

    history_text = Text()
    history_text.append("LAST HEARD\n", style="bold bright_white")
    history_text.append(f"'{last_command}'\n\n", style="deep_sky_blue2")
    history_text.append("COMMANDS\n", style="bold bright_white")
    history_text.append("play <song>\n", style="white")
    history_text.append("pause | resume\n", style="white")
    history_text.append("next | previous\n", style="white")
    history_text.append("volume up/down | volume 40\n", style="white")
    history_text.append("now playing", style="white")

    layout["log_panel"].update(
        Panel(
            history_text,
            title="[bold deep_sky_blue2]Voice Log[/bold deep_sky_blue2]",
            border_style=HISTORY_BORDER,
            box=box.ROUNDED,
            padding=(1, 2),
        )
    )

    layout["footer"].update(
        Panel(
            "[bold yellow3]Wake:[/] 'hey spotify'   [bold orchid]Exit:[/] Ctrl+C",
            border_style=FOOTER_BORDER,
            box=box.SQUARE,
        )
    )

    return layout
