from manim import *

# ============================================================
# SECOND DEGRÉ
# Format vertical 9:16 – TikTok / Instagram
# ============================================================

config.pixel_width = 1080
config.pixel_height = 1920
config.frame_width = 9
config.frame_height = 16


class SecondDegre(Scene):

    def construct(self):

        self.camera.background_color = WHITE

        # ====================================================
        # TITRE
        # ====================================================

        titre = MathTex(
            r"P(x)=ax^2+bx+c"
        ).scale(0.8).set_color(GREEN)

        titre.to_edge(UP, buff=0.45)

        self.play(
            FadeIn(titre, shift=DOWN * 0.2),
            run_time=0.6
        )

        # ====================================================
        # DISCRIMINANT
        # ====================================================

        delta = MathTex(
            r"\Delta=b^2-4ac"
        ).scale(0.65).set_color(PURPLE)

        delta.next_to(titre, DOWN, buff=0.25)

        self.play(
            Write(delta),
            run_time=0.6
        )

        # ====================================================
        # INTRODUCTION
        # ====================================================

        intro = Text(
            "Le signe de Δ détermine\n"
            "le nombre de racines."
        ).scale(0.38).set_color(BLACK)

        intro.next_to(delta, DOWN, buff=0.3)

        self.play(
            FadeIn(intro),
            run_time=0.5
        )

        # ====================================================
        # AXES
        # ====================================================

        ax = Axes(
            x_range=[-7, 7, 1],
            y_range=[-5, 5, 1],
            x_length=7.5,
            y_length=6,
            axis_config={
                "include_tip": False,
                "stroke_width": 2,
            },
        ).set_color(BLACK)

        ax.move_to(DOWN * 0.7)

        labels = ax.get_axis_labels(
            x_label="x",
            y_label="P(x)"
        ).set_color(BLACK)

        self.play(
            Create(ax),
            FadeIn(labels),
            run_time=0.7
        )

        # ====================================================
        # CAS 1 : DELTA < 0
        # ====================================================

        def f(x):
            return 0.55 * (x + 1) ** 2 + 1

        graph = ax.plot(
            f,
            x_range=[-7, 5],
            color=MAROON,
            stroke_width=5
        )

        cas = MathTex(
            r"\Delta<0"
        ).scale(0.65).set_color(MAROON)

        cas.to_edge(DOWN, buff=3.2)

        explication = Text(
            "Aucune racine réelle"
        ).scale(0.4).set_color(BLUE)

        explication.next_to(cas, DOWN, buff=0.2)

        detail = Text(
            "La parabole ne coupe pas l'axe des abscisses."
        ).scale(0.28).set_color(BLACK)

        detail.next_to(explication, DOWN, buff=0.15)

        self.play(
            Create(graph),
            FadeIn(cas),
            FadeIn(explication),
            FadeIn(detail),
            run_time=1
        )

        self.wait(1)

        # ====================================================
        # TRANSITION VERS DELTA = 0
        # ====================================================

        def g(x):
            return 0.55 * (x - 2) ** 2

        nouveau_graph = ax.plot(
            g,
            x_range=[-5, 7],
            color=MAROON,
            stroke_width=5
        )

        nouveau_cas = MathTex(
            r"\Delta=0"
        ).scale(0.65).set_color(MAROON)

        nouveau_cas.move_to(cas)

        nouvelle_explication = Text(
            "Une racine double"
        ).scale(0.4).set_color(BLUE)

        nouvelle_explication.move_to(explication)

        nouveau_detail = Text(
            "La parabole touche l'axe en un seul point."
        ).scale(0.28).set_color(BLACK)

        nouveau_detail.move_to(detail)

        self.play(
            Transform(graph, nouveau_graph),
            Transform(cas, nouveau_cas),
            Transform(explication, nouvelle_explication),
            Transform(detail, nouveau_detail),
            run_time=0.8
        )

        # Point de tangence
        point0 = Dot(
            ax.c2p(2, 0),
            color=RED,
            radius=0.09
        )

        x0 = MathTex(
            r"x_0"
        ).scale(0.5).set_color(RED)

        x0.next_to(point0, DOWN, buff=0.12)

        self.play(
            FadeIn(point0),
            Write(x0),
            run_time=0.4
        )

        self.wait(1)

        # ====================================================
        # TRANSITION VERS DELTA > 0
        # ====================================================

        def h(x):
            return 0.55 * (x - 2) ** 2 - 2

        nouveau_graph = ax.plot(
            h,
            x_range=[-5, 7],
            color=MAROON,
            stroke_width=5
        )

        nouveau_cas = MathTex(
            r"\Delta>0"
        ).scale(0.65).set_color(MAROON)

        nouveau_cas.move_to(cas)

        nouvelle_explication = Text(
            "Deux racines réelles"
        ).scale(0.4).set_color(BLUE)

        nouvelle_explication.move_to(explication)

        nouveau_detail = Text(
            "La parabole coupe l'axe en deux points."
        ).scale(0.28).set_color(BLACK)

        nouveau_detail.move_to(detail)

        self.play(
            FadeOut(point0),
            FadeOut(x0),
            Transform(graph, nouveau_graph),
            Transform(cas, nouveau_cas),
            Transform(explication, nouvelle_explication),
            Transform(detail, nouveau_detail),
            run_time=0.8
        )

        # Racines
        x1 = ax.c2p(
            2 - np.sqrt(2 / 0.55),
            0
        )

        x2 = ax.c2p(
            2 + np.sqrt(2 / 0.55),
            0
        )

        point1 = Dot(x1, color=ORANGE, radius=0.09)
        point2 = Dot(x2, color=ORANGE, radius=0.09)

        label1 = MathTex(
            r"x_1"
        ).scale(0.5).set_color(ORANGE)

        label2 = MathTex(
            r"x_2"
        ).scale(0.5).set_color(ORANGE)

        label1.next_to(point1, DOWN, buff=0.12)
        label2.next_to(point2, DOWN, buff=0.12)

        self.play(
            FadeIn(point1),
            FadeIn(point2),
            Write(label1),
            Write(label2),
            run_time=0.5
        )

        self.wait(1)

        # ====================================================
        # FORMES FACTORISÉES
        # ====================================================

        factorisation = MathTex(
            r"P(x)=a(x-x_1)(x-x_2)"
        ).scale(0.65).set_color(ORANGE)

        factorisation.to_edge(DOWN, buff=1.3)

        self.play(
            Write(factorisation),
            run_time=0.7
        )

        self.wait(1)

        # ====================================================
        # TRANSITION : INFLUENCE DE a
        # ====================================================

        objets = VGroup(
            ax,
            labels,
            graph,
            cas,
            explication,
            detail,
            point1,
            point2,
            label1,
            label2,
            factorisation,
            delta
        )

        self.play(
            FadeOut(objets),
            FadeOut(intro),
            FadeOut(titre),
            run_time=0.6
        )

        # ====================================================
        # INFLUENCE DU SIGNE DE a
        # ====================================================

        titre_a = MathTex(
            r"\text{Le signe de }a"
        ).scale(0.75).set_color(GREEN)

        titre_a.to_edge(UP, buff=0.5)

        self.play(
            FadeIn(titre_a, shift=DOWN * 0.2),
            run_time=0.5
        )

        # Nouveaux axes
        ax2 = Axes(
            x_range=[-6, 6, 1],
            y_range=[-5, 5, 1],
            x_length=7.5,
            y_length=6,
            axis_config={
                "include_tip": False,
                "stroke_width": 2,
            }
        ).set_color(BLACK)

        ax2.move_to(DOWN * 0.8)

        labels2 = ax2.get_axis_labels(
            x_label="x",
            y_label="P(x)"
        ).set_color(BLACK)

        self.play(
            Create(ax2),
            FadeIn(labels2),
            run_time=0.6
        )

        # ----------------------------------------------------
        # a > 0
        # ----------------------------------------------------

        a_plus = MathTex(
            r"a>0"
        ).scale(0.7).set_color(RED)

        a_plus.to_edge(DOWN, buff=2.2)

        texte_plus = Text(
            "Les branches sont vers le haut."
        ).scale(0.38).set_color(BLUE)

        texte_plus.next_to(a_plus, DOWN, buff=0.2)

        def parabole_haut(x):
            return 0.55 * (x + 1) ** 2 - 2

        graph_a_plus = ax2.plot(
            parabole_haut,
            x_range=[-4, 2],
            color=MAROON,
            stroke_width=5
        )

        self.play(
            Create(graph_a_plus),
            Write(a_plus),
            FadeIn(texte_plus),
            run_time=0.9
        )

        self.wait(1)

        # ----------------------------------------------------
        # a < 0
        # ----------------------------------------------------

        a_moins = MathTex(
            r"a<0"
        ).scale(0.7).set_color(RED)

        a_moins.move_to(a_plus)

        texte_moins = Text(
            "Les branches sont vers le bas."
        ).scale(0.38).set_color(BLUE)

        texte_moins.move_to(texte_plus)

        def parabole_bas(x):
            return -0.55 * (x + 1) ** 2 + 2

        graph_a_moins = ax2.plot(
            parabole_bas,
            x_range=[-4, 2],
            color=MAROON,
            stroke_width=5
        )

        self.play(
            Transform(graph_a_plus, graph_a_moins),
            Transform(a_plus, a_moins),
            Transform(texte_plus, texte_moins),
            run_time=0.8
        )

        self.wait(1)

        # ====================================================
        # CONCLUSION
        # ====================================================

        conclusion = Text(
            "Le signe de Δ donne le nombre de racines.\n"
            "Le signe de a donne le sens des branches."
        ).scale(0.34).set_color(BLACK)

        conclusion.to_edge(DOWN, buff=0.65)

        self.play(
            FadeIn(conclusion, shift=UP * 0.2),
            run_time=0.6
        )

        # Signature
        signature = Text(
            "courspasquet.fr"
        ).scale(0.22).set_color(GREY_B)

        signature.to_corner(
            DOWN + RIGHT,
            buff=0.25
        )

        self.play(
            FadeIn(signature),
            run_time=0.3
        )

        self.wait(2)