# python -m manim division.py CleSecuriteSociale

from manim import *

# Format vertical 9:16
config.pixel_width = 1080
config.pixel_height = 1920
#config.frame_width = 9
#config.frame_height = 16
config.background_color = "#008000"

# Couleurs
BLACK = "#000000"
RED = "#D62828"


class CleSecuriteSociale(Scene):
    def construct(self):
        # ============================================================
        # SIGNATURE / SITE
        # ============================================================

        signature = Text(
            "courspasquet.fr",
            font_size=28,
            color=BLACK
        )

        signature.move_to(
            RIGHT * (config.frame_width / 2 - 2)
            + DOWN * (config.frame_height)
        )

        self.add(signature)

        # ---------------------------------------------------------
        # 1. Le numéro avec les espaces
        # ---------------------------------------------------------

        numero_espace = Text(
            "1 10 04 33 021 033",
            font="DejaVu Sans",
            font_size=48,
            color=BLACK
        )

        numero_espace.move_to(UP * 5)

        self.play(Write(numero_espace))
        self.wait(4)

        # ---------------------------------------------------------
        # 2. Rapprochement des chiffres
        # ---------------------------------------------------------

        numero = Text(
            "1100433021033",
            font="DejaVu Sans",
            font_size=52,
            color=BLACK
        )

        numero.move_to(UP * 3.8)

        self.play(
            Transform(numero_espace, numero),
            run_time=1.5
        )

        self.wait(1)

        # ---------------------------------------------------------
        # 3. Préparation de la division euclidienne
        #
        #       1100433021033 | 97
        #                         11344670319
        # ---------------------------------------------------------

        dividende = Text(
            "1100433021033",
            font="DejaVu Sans",
            font_size=34,
            color=BLACK
        )

        diviseur = Text(
            "97",
            font="DejaVu Sans",
            font_size=42,
            color=BLACK
        )

        quotient = Text(
            "11344670319",
            font="DejaVu Sans",
            font_size=40,
            color=BLACK
        )

        # Positions
        dividende.move_to(LEFT * 2.15 + UP * 1.8)
        diviseur.move_to(RIGHT * 1.7 + UP * 1.8)
        quotient.move_to(RIGHT * 2.15 + UP * 0.9)

        # Lignes de la division
        ligne_horizontale = Line(
            LEFT * 0.15 + UP * 1.45,
            RIGHT * 3.3 + UP * 1.45,
            color=BLACK,
            stroke_width=2
        )

        ligne_verticale = Line(
            LEFT * 0.15 + UP * 1.85,
            LEFT * 0.15 + DOWN * 4.7,
            color=BLACK,
            stroke_width=2
        )

        # Faire apparaître la structure
        self.play(
            Write(dividende),
            Write(diviseur),
            Create(ligne_verticale),
            Create(ligne_horizontale),
            run_time=1
        )

        self.play(Write(quotient), run_time=0.8)

        self.wait(0.5)

        # ---------------------------------------------------------
        # 4. Les étapes de la division
        # ---------------------------------------------------------
        #
        # Les nombres sont volontairement positionnés comme dans
        # une division posée classique.
        #

        etapes = [
            ("130", 1.15),
            ("334", 0.55),
            ("433", -0.05),
            ("453", -0.65),
            ("650", -1.25),
            ("682", -1.85),
            ("310", -2.45),
            ("193", -3.05),
            ("963", -3.65),
        ]

        nombres_etapes = VGroup()

        for texte, y in etapes:
            obj = Text(
                texte,
                font="DejaVu Sans",
                font_size=38,
                color=BLACK
            )

            # Décalage progressif vers la droite
            x = -2.65 + (1.0 - y) * 0.22

            obj.move_to(RIGHT * x + UP * y)

            nombres_etapes.add(obj)

        # Apparition progressive des étapes
        for obj in nombres_etapes:
            self.play(Write(obj), run_time=0.35)

        self.wait(0.5)

        # ---------------------------------------------------------
        # 5. Le reste 90
        # ---------------------------------------------------------

        reste = Text(
            "90",
            font="DejaVu Sans",
            font_size=48,
            color=RED
        )

        reste.move_to(LEFT * 1.05 + DOWN * 4.25)

        self.play(
            Write(reste),
            run_time=0.6
        )

        self.wait(0.5)

        # Encadrement du reste
        cadre = SurroundingRectangle(
            reste,
            color=RED,
            buff=0.12,
            stroke_width=3
        )

        # Mise en valeur : agrandissement + cadre
        self.play(
            reste.animate.scale(1.25),
            Create(cadre),
            run_time=0.8
        )

        self.wait(1)

        # ---------------------------------------------------------
        # 6. Calcul final : 97 - 90 = 07
        # ---------------------------------------------------------

        calcul = MathTex(
            "97", "-", "90", "=", "07",
            font_size=54,
            color=BLACK
        )

        calcul[2].set_color(RED)

        calcul.move_to(DOWN * 5.8)

        self.play(
            Write(calcul),
            run_time=1
        )

        self.wait(3)