"""
R1 — Resume (wake from sleep).

A calm centered card over the dimmed, blurred last frame: "Welcome back", the
game you were in, an honest "nothing was lost" line, and a focused Resume. A
resumes, B drops back to AAhead. UI-only (no emulator backend yet).
"""

from __future__ import annotations

import pygame

from ..design import tokens as t
from ..shared import fonts, render
from ..widgets import components as c, images

CARD_W = 460


class ResumeScreen:
    name = "resume"

    def __init__(self, app, game=None):
        self.app = app
        ce = app.catalog.continue_entries()
        self.game = game or (ce[0]["game"] if ce else app.catalog.games[0])
        self.ent = app.catalog.continue_entry(self.game["id"])

    def on_action(self, action):
        if action in ("a",):
            self.app.toast(f"Resuming {self.game['title']}…")
            self.app.pop()                 # dismiss wake → back to where it opened
        elif action == "b":
            self.app.pop()

    def update(self, dt_ms):
        pass

    def draw(self, surf):
        cat = self.app.catalog
        # blurred frame + radial veil
        surf.blit(images.backdrop(cat.asset_path(self.game, "hero")
                                  or cat.asset_path(self.game, "box"),
                                  t.VIEWPORT_WIDTH, t.VIEWPORT_HEIGHT, blur=18, darken=0.7),
                  (0, 0))
        veil = pygame.Surface((t.VIEWPORT_WIDTH, t.VIEWPORT_HEIGHT), pygame.SRCALPHA)
        veil.fill(t.with_alpha(t.Color.BG_DEEP, 0.45))
        surf.blit(veil, (0, 0))
        # corner clock — live (sysfs battery + datetime); deterministic under
        # AAHEAD_FAKE_* env vars used by the screenshot harness.
        from ..shared.system import get_status
        s = get_status()
        clk = fonts.render(t.Type.MONO, f"{s.clock}   {s.battery}%",
                           t.Color.TEXT_SECONDARY)
        surf.blit(clk, (t.VIEWPORT_WIDTH - 22 - clk.get_width(), 18))

        cx = (t.VIEWPORT_WIDTH - CARD_W) // 2
        ch = 198
        cy = (t.VIEWPORT_HEIGHT - ch) // 2
        card = pygame.Surface((CARD_W, ch), pygame.SRCALPHA)
        pygame.draw.rect(card, t.with_alpha(t.Color.SURFACE_ELEVATED, 0.82),
                         (0, 0, CARD_W, ch), border_radius=16)
        pygame.draw.rect(card, t.with_alpha(t.Color.TEXT_PRIMARY, 0.06),
                         (0, 0, CARD_W, ch), width=1, border_radius=16)
        surf.blit(card, (cx, cy))

        x = cx + 28
        y = cy + 22
        # eyebrow with cyan pulse
        pygame.draw.circle(surf, t.Color.ACCENT_CYAN, (x + 3, y + 6), 3)
        surf.blit(fonts.render(t.Type.EYEBROW, "WELCOME BACK", t.Color.TEXT_MUTED),
                  (x + 12, y))
        y += 24
        # head: art + title + sub
        box = images.cover(cat.asset_path(self.game, "box"), 64, 46, radius=6, mode="cover")
        surf.blit(box, (x, y))
        tx = x + 64 + 14
        surf.blit(fonts.render(t.Type.DISPLAY, c._ellipsize(t.Type.DISPLAY,
                  self.game["title"], CARD_W - 64 - 70), t.Color.TEXT_PRIMARY), (tx, y))
        pt = self.ent["playtime"] if self.ent else None
        c.meta_line(surf, tx, y + 28, [self.game["system"], "asleep 14 min", pt],
                    color=t.Color.TEXT_MUTED)
        y += 58
        # quiet line
        c.draw_paragraph(surf, t.Type.BODY, "Your game is exactly where you left it. "
                         "Nothing was lost.", x, y, CARD_W - 56, t.Color.TEXT_SECONDARY,
                         line_h=20, max_lines=2)
        y += 44
        # Resume CTA (focused) + alt
        cta = pygame.Rect(x, y, fonts.measure(t.Type.BTN, "Resume")[0] + 30 + 16, 34)
        c.button_primary(surf, cta, "Resume", focused=True)
        # secondary hint as a proper key chip (not grey mono noise next to the CTA)
        gp = pygame.Rect(cta.right + 16, y + 8, 18, 18)
        pygame.draw.rect(surf, t.Color.SURFACE_ELEVATED, gp, border_radius=999)
        pygame.draw.rect(surf, t.Color.BORDER_BRIGHT, gp, width=1, border_radius=999)
        b = fonts.render(t.Type.GP, "B", t.Color.TEXT_PRIMARY)
        surf.blit(b, (gp.centerx - b.get_width() // 2, gp.centery - b.get_height() // 2))
        lab = fonts.render(t.Type.CAPTION, "back to AAhead", t.Color.TEXT_SECONDARY)
        surf.blit(lab, (gp.right + 8, y + (34 - lab.get_height()) // 2))
