"""
s04 - The C memory model. 30s.

Stack/heap/data/text regions on the left; rules on the right. No address
column on the regions (kept clean to avoid overlap with the rules block;
the "grows down"/"grows up" labels carry the semantics).
"""

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

from manim import (
    Text, Rectangle, VGroup, FadeIn,
    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, assert_no_overlap


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

    def construct_content(self):
        title = slide_title(
            "C memory model  -  designed 1972, no safety net",
            font_size=32, with_accent_rule=True,
        )
        title.move_to((0.0, 2.95, 0.0))

        def region(label_text, color, height=0.80):
            rect = Rectangle(
                width=4.6, height=height,
                fill_color=color, fill_opacity=0.18,
                stroke_color=color, stroke_width=2,
            )
            t = Text(label_text, font=Fonts.SANS, font_size=Sizes.BODY - 8,
                     color=SecurityPalette.FG, weight="BOLD")
            t.move_to(rect.get_center())
            return VGroup(rect, t)

        stack = region("stack    (grows down)",  SecurityPalette.INFO)
        heap  = region("heap     (grows up)",    SecurityPalette.SAFE)
        data  = region(".data / .bss",           SecurityPalette.FG_MUTED, height=0.55)
        text  = region(".text    (code)",        SecurityPalette.WARN,     height=0.55)

        layout = VGroup(stack, heap, data, text).arrange(DOWN, buff=0.15)
        layout.move_to((-3.40, -0.10, 0.0))

        # Rules (right column)
        rules = VGroup(
            Text("malloc(n)      -  no size in the pointer",
                 font=Fonts.SANS, font_size=Sizes.SMALL - 2,
                 color=SecurityPalette.FG),
            Text("memcpy(dst, src, n)  -  no bounds check",
                 font=Fonts.SANS, font_size=Sizes.SMALL - 2,
                 color=SecurityPalette.FG),
            Text("buffer[i]      -  i is just an integer",
                 font=Fonts.SANS, font_size=Sizes.SMALL - 2,
                 color=SecurityPalette.FG),
            Text("\"Trust the programmer.\"",
                 font=Fonts.SANS, font_size=Sizes.SUBTITLE - 8,
                 color=SecurityPalette.WARN),
            Text("-  ANSI C89 charter",
                 font=Fonts.SANS, font_size=Sizes.CAPTION,
                 color=SecurityPalette.FG_DIM),
        ).arrange(DOWN, buff=0.32, aligned_edge=LEFT)
        rules.move_to((2.85, -0.10, 0.0))

        # Sanity check: warn if layout and rules collide
        assert_no_overlap(
            [("title", title), ("layout", layout), ("rules", rules)],
            scene_name="S04MemoryModel",
        )

        self.play(FadeIn(title, shift=0.2 * UP), run_time=0.5)
        self.play(FadeIn(layout), run_time=0.7)
        self.next_slide()
        self.play(FadeIn(rules, shift=0.1 * LEFT), run_time=0.8)
        self.wait(0.5)
        self.next_slide()
