"""
Bibliography + inline citation helper.

Each entry is numbered by order of first appearance in the talk (matches paper
IEEE .bbl order where possible). cite() returns a small Text mobject placed in
the bottom-right corner of the slide; add it to the scene before next_slide().

The References scene (s17) renders the full BIB dict in two columns.
"""

from manim import Text, VGroup
from ._theme import Palette, Fonts, Sizes, Layout


BIB = {
    1:  ("Kernighan & Ritchie",      "The C Programming Language (1st ed.)",       "Prentice Hall, 1978"),
    2:  ("Aleph One",                "Smashing the Stack for Fun and Profit",      "Phrack 49, 1996"),
    3:  ("Anonymous",                "Once Upon a free()",                         "Phrack 57, 2001"),
    4:  ("Szekeres et al.",          "SoK: Eternal War in Memory",                 "IEEE S&P 2013"),
    5:  ("MITRE",                    "CWE Top 25 Most Dangerous Weaknesses",       "mitre.org, 2024"),
    6:  ("Microsoft Security",       "70% of CVEs are memory-safety issues",       "BlueHat IL 2019"),
    7:  ("Google Project Zero",      "0day In-the-Wild tracking spreadsheet",      "2024"),
    8:  ("Marczak et al.",           "FORCEDENTRY: NSO iMessage Zero-Click",       "Citizen Lab, 2021"),
    9:  ("Meta / WhatsApp",          "Security Advisory CVE-2025-55177",           "Aug 2025"),
    10: ("Apple",                    "About the security content of iOS 18.6.2",   "Aug 2025"),
    11: ("Quarkslab",                "Two bytes that make size matter (DNG/SOF3)", "blog.quarkslab.com, 2025"),
    12: ("Hyunwoo Kim",              "Copy Fail (CVE-2026-31431), Linux AF_ALG",   "May 2026"),
    13: ("Hyunwoo Kim",              "Dirty Frag (CVE-2026-43284 + 43500)",        "May 2026"),
    14: ("M. Ojeda et al.",          "Rust for Linux mainline integration",        "Linux 6.x, 2024-2025"),
    15: ("ARM Ltd.",                 "Memory Tagging Extension (ARMv8.5-A)",       "developer.arm.com, 2019"),
}


def cite(scene, refs: list[int]) -> Text:
    """
    Small [N1, N2, ...] marker placed at bottom-right corner of the slide.

    Usage in a scene:
        cite_marker = cite(self, [4, 5, 6])
        self.add(cite_marker)
    """
    label = "[" + ", ".join(str(n) for n in refs) + "]"
    t = Text(label, font=Fonts.MONO, font_size=Sizes.CITATION,
             color=Palette.FG_DIM)
    t.move_to([Layout.CITE_X - t.width / 2,
               Layout.CITE_Y, 0])
    return t


def render_refs_block(entries: list[int], *, font_size: int = 16) -> VGroup:
    """
    Render a vertical block of bibliography entries for the References scenes.
    Each entry is formatted as: [N] Author - Title - Venue
    """
    lines = []
    for n in entries:
        if n not in BIB:
            continue
        author, title, venue = BIB[n]
        line_text = f"[{n}]  {author}  -  {title}  -  {venue}"
        t = Text(line_text, font=Fonts.SANS, font_size=font_size,
                 color=Palette.FG)
        lines.append(t)

    group = VGroup(*lines).arrange(DOWN, buff=0.20, aligned_edge=LEFT)
    return group
