"""
s06 - 30 years of the same pattern.

Horizontal timeline: 1996 Aleph -> 2001 Phrack heap -> 2013 SoK -> 2021 FORCEDENTRY
-> 2025 WhatsApp chain -> 2026 Copy Fail + Dirty Frag (preview).
"""

import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent.parent))

from manim import (
    Text, Line, Dot, VGroup, FadeIn, Create,
    UP, DOWN, LEFT, RIGHT, ORIGIN,
)
from src.slides._core import SceneSlide
from src.slides._theme import SecurityPalette, Fonts, Sizes, Layout
from src.slides._layout import slide_title


class S06Timeline(SceneSlide):
    SECTION = None
    WITH_FOOTER = True
    ON_DARK = True

    def construct_content(self):
        title = slide_title(
            "30 years.  Same problem.",
            font_size=Sizes.TITLE - 8, with_accent_rule=True,
        )
        title.move_to((0.0, 2.7, 0.0))

        # Timeline line
        line_y = -0.2
        line = Line(
            (Layout.LEFT_EDGE + 1.7, line_y, 0),
            (Layout.RIGHT_EDGE - 1.7, line_y, 0),
            color=SecurityPalette.RULE, stroke_width=2,
        )

        events = [
            ("1996", "Aleph One",       ["Smashing the Stack"],                          SecurityPalette.FG_MUTED),
            ("2001", "Phrack 57",       ["Once Upon a free()"],                          SecurityPalette.FG_MUTED),
            ("2013", "Szekeres et al.", ["SoK: Eternal War"],                            SecurityPalette.FG_MUTED),
            ("2021", "Citizen Lab",     ["FORCEDENTRY"],                                 SecurityPalette.INFO),
            ("2025", "WhatsApp + Apple",["CVE-2025-55177", "CVE-2025-43300"],            SecurityPalette.WARN),
            ("2026", "Hyunwoo Kim",     ["Copy Fail (CVE-2026-31431)",
                                         "Dirty Frag (CVE-2026-43284 + 43500)"],         SecurityPalette.DANGER),
        ]

        n = len(events)
        x_start = Layout.LEFT_EDGE + 1.9
        x_end   = Layout.RIGHT_EDGE - 1.9
        step    = (x_end - x_start) / (n - 1)

        markers = VGroup()
        labels  = VGroup()
        for i, (year, who, what_lines, color) in enumerate(events):
            x = x_start + i * step
            dot = Dot((x, line_y, 0), radius=0.12, color=color)
            yr  = Text(year, font=Fonts.SANS, font_size=Sizes.BODY - 2,
                       color=color, weight="BOLD")
            yr.move_to((x, line_y + 0.55, 0))
            who_t  = Text(who, font=Fonts.SANS, font_size=14,
                          color=SecurityPalette.FG, weight="BOLD")
            what_mobs = [
                Text(line, font=Fonts.SANS, font_size=11,
                     color=SecurityPalette.FG_MUTED)
                for line in what_lines
            ]
            label_block = VGroup(who_t, *what_mobs).arrange(DOWN, buff=0.05)
            # Alternate above/below to avoid collisions with neighbours
            if i % 2 == 0:
                label_block.next_to((x, line_y - 0.30, 0), DOWN, buff=0.20)
            else:
                label_block.next_to((x, line_y + 0.80, 0), UP, buff=0.20)
            markers.add(dot, yr)
            labels.add(label_block)

        tag = Text(
            "Pattern continues into 2026.",
            font=Fonts.SERIF, font_size=Sizes.SMALL,
            color=SecurityPalette.FG_MUTED,
        )
        tag.move_to((0.0, Layout.BOTTOM_EDGE + 0.95, 0))

        self.play(FadeIn(title, shift=0.2 * UP), run_time=0.5)
        self.play(Create(line), run_time=0.6)
        for i in range(n):
            self.play(
                FadeIn(markers[2 * i]),
                FadeIn(markers[2 * i + 1]),
                FadeIn(labels[i]),
                run_time=0.3,
            )
        self.play(FadeIn(tag), run_time=0.4)
        self.wait(0.5)
        self.next_slide()
