"""
s13 - Money shot: CTF heap layout = CVE-2025-43300 heap layout.

Two columns side by side, structurally identical.
"""

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

from manim import (
    Text, Rectangle, Arrow, VGroup, FadeIn, Write, Flash,
    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 S13MoneyShot(SceneSlide):
    SECTION = None
    WITH_FOOTER = True
    ON_DARK = True

    def construct_content(self):
        title = slide_title(
            "CTF heap  =  CVE heap",
            font_size=Sizes.TITLE - 8, with_accent_rule=True,
        )
        title.move_to((0.0, 2.8, 0.0))

        def heap_diagram(header, color):
            head = Text(header, font=Fonts.SANS, font_size=Sizes.BODY - 4,
                        color=color, weight="BOLD")

            buf_t = Text("buffer  (allocated)", font=Fonts.SANS, font_size=Sizes.SMALL - 2,
                         color=SecurityPalette.FG, weight="BOLD")
            ovf_t = Text("OOB write", font=Fonts.SANS, font_size=Sizes.SMALL - 2,
                         color=SecurityPalette.FG, weight="BOLD")
            ptr_t = Text("function pointer  /  vtable",
                         font=Fonts.SANS, font_size=Sizes.SMALL - 2,
                         color=SecurityPalette.FG, weight="BOLD")

            # All three boxes share the widest text width for visual alignment
            box_w = max(buf_t.width, ovf_t.width, ptr_t.width) + 0.50

            buf = Rectangle(width=box_w, height=1.0,
                            fill_color=SecurityPalette.SAFE, fill_opacity=0.30,
                            stroke_color=SecurityPalette.SAFE, stroke_width=2)
            buf_t.move_to(buf.get_center())

            ovf = Rectangle(width=box_w, height=0.5,
                            fill_color=SecurityPalette.DANGER, fill_opacity=0.45,
                            stroke_color=SecurityPalette.DANGER, stroke_width=2)
            ovf_t.move_to(ovf.get_center())

            ptr = Rectangle(width=box_w, height=0.7,
                            fill_color=SecurityPalette.WARN, fill_opacity=0.30,
                            stroke_color=SecurityPalette.WARN, stroke_width=2)
            ptr_t.move_to(ptr.get_center())

            stack = VGroup(
                VGroup(buf, buf_t),
                VGroup(ovf, ovf_t),
                VGroup(ptr, ptr_t),
            ).arrange(DOWN, buff=0.05)

            return VGroup(head, stack).arrange(DOWN, buff=0.30)

        left = heap_diagram("heap-two  (Phoenix CTF)",  SecurityPalette.INFO)
        right = heap_diagram("CVE-2025-43300  (ImageIO)", SecurityPalette.DANGER)

        cols = VGroup(left, right).arrange(RIGHT, buff=1.5)
        cols.move_to((0.0, -0.2, 0))

        eq = Text("=", font=Fonts.SERIF_DISPLAY, font_size=80,
                  color=SecurityPalette.WARN, weight="BOLD")
        eq.move_to((0.0, -0.2, 0))

        tag = Text(
            "Same primitive.  Same outcome.  Different decade.",
            font=Fonts.SERIF, font_size=Sizes.SUBTITLE - 6,
            color=SecurityPalette.WARN,
        )
        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(FadeIn(left), run_time=0.6)
        self.play(FadeIn(right), run_time=0.6)
        self.play(FadeIn(eq, shift=0.2 * UP), run_time=0.4)
        self.next_slide()
        self.play(Flash(eq, color=SecurityPalette.WARN, flash_radius=0.8),
                  FadeIn(tag, shift=0.2 * UP), run_time=0.6)
        self.wait(0.5)
        self.next_slide()
