"""
s12 - CVE-2025-43300: Apple ImageIO OOB write via DNG mismatch.

The two-bytes-that-make-size-matter visualization.
"""

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,
    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 S12Cve43300(SceneSlide):
    SECTION = None
    WITH_FOOTER = True
    ON_DARK = True

    def construct_content(self):
        title = slide_title(
            "CVE-2025-43300  -  the exploitation stage",
            font_size=38, with_accent_rule=True,
        )
        title.move_to((0.0, 2.95, 0.0))

        def chip(label, color):
            t = Text(label, font=Fonts.SANS, font_size=Sizes.SMALL - 2,
                     color=SecurityPalette.FG, weight="BOLD")
            box = Rectangle(width=t.width + 0.40, height=0.60,
                            fill_color=color, fill_opacity=0.28,
                            stroke_color=color, stroke_width=1.5)
            t.move_to(box.get_center())
            return VGroup(box, t)

        chips = VGroup(
            chip("CWE-787 / OOB W",  SecurityPalette.DANGER),
            chip("CVSS 8.8",         SecurityPalette.DANGER),
            chip("Apple ImageIO",    SecurityPalette.INFO),
            chip("Patch: iOS 18.6.2",SecurityPalette.SAFE),
        ).arrange(RIGHT, buff=0.30)
        chips.move_to((0.0, 2.05, 0))

        # The two fields - boxes wider, content shrunk to fit cleanly
        def field_box(header, value, color):
            head = Text(header, font=Fonts.SANS, font_size=Sizes.SMALL - 3,
                        color=color, weight="BOLD")
            val  = Text(value, font=Fonts.SANS, font_size=Sizes.BODY - 6,
                        color=SecurityPalette.FG, weight="BOLD")
            inner = VGroup(head, val).arrange(DOWN, buff=0.22)
            w = max(head.width, val.width) + 0.60
            box = Rectangle(width=w, height=inner.height + 0.55,
                            fill_color=color, fill_opacity=0.15,
                            stroke_color=color, stroke_width=2)
            inner.move_to(box.get_center())
            return VGroup(box, inner)

        tiff = field_box("TIFF  SamplesPerPixel (tag 277)",
                         "Nf = 1   ->   allocate (1 sample)",
                         SecurityPalette.INFO)
        jpeg = field_box("JPEG  SOF3.Nf (lossless start)",
                         "Nf = 3   ->   decode (3 samples)",
                         SecurityPalette.DANGER)

        fields = VGroup(tiff, jpeg).arrange(RIGHT, buff=0.5)
        fields.move_to((0.0, 0.3, 0))

        # Arrow + tagline
        arrow = Arrow(tiff.get_right(), jpeg.get_left(),
                      color=SecurityPalette.WARN, stroke_width=4, buff=0.15)

        tag = Text(
            "Allocate for 1.   Write 3.   3x overflow on heap.",
            font=Fonts.SERIF, font_size=Sizes.SUBTITLE - 4,
            color=SecurityPalette.WARN,
        )
        tag.move_to((0.0, -1.4, 0))

        quark = Text(
            "Quarkslab: \"two bytes that make size matter\"",
            font=Fonts.SANS, font_size=Sizes.CAPTION,
            color=SecurityPalette.FG_MUTED,
        )
        quark.move_to((0.0, -2.1, 0))

        self.play(FadeIn(title, shift=0.2 * UP), run_time=0.5)
        self.play(FadeIn(chips), run_time=0.6)
        self.next_slide()
        self.play(FadeIn(tiff), run_time=0.5)
        self.play(FadeIn(jpeg), run_time=0.5)
        self.play(FadeIn(arrow), run_time=0.3)
        self.next_slide()
        self.play(FadeIn(tag, shift=0.2 * UP), run_time=0.5)
        self.play(FadeIn(quark), run_time=0.3)
        self.wait(0.5)
        self.next_slide()
