from manimlib.imports import *

class Intro(Scene):
    def construct(self):
        title = TextMobject("Penyelesaian Persamaan Kuadratik").scale(1.5)
        self.play(Write(title))
        self.wait(2)
        
        way1 = TexMobject("(x-\\alpha)(x-\\beta)=0").scale(0.7).shift(LEFT*4 + DOWN*.5)
        way2 = TexMobject("a(x+p)^2+q").scale(0.7).shift(DOWN*.5)
        way3 = TexMobject("x=\\frac{-b\\pm\\sqrt{b^2-4ac}}{2a}").scale(0.7).shift(RIGHT*4 + DOWN*.5)

        self.play(title.shift, UP*1.5)
        self.play(GrowFromCenter(way1))
        self.play(GrowFromCenter(way2))
        self.play(GrowFromCenter(way3))

        self.wait(10)

class Explanation(Scene):
    def construct(self):
        question = TexMobject("2", "x^2", "-8", "x", "-64", "=0").scale(1.5).shift(UP*3)

        sor_label = TextMobject("Hasil tambah punca:", color=BLUE).scale(0.8).to_edge(LEFT, buff=1.5).shift(UP*1.2)
        sor = TexMobject("-", "{", "-8", "\\over", "2", "}").next_to(sor_label, RIGHT)
        sor_b = TexMobject("b").move_to(sor[2].get_center())
        sor_a = TexMobject("a").move_to(sor[4].get_center())
        sor_ans = TexMobject("4").next_to(sor_label, RIGHT)

        por_label = TextMobject("Hasil darab punca:", color=BLUE).scale(0.8).to_edge(LEFT, buff=1.5).shift(DOWN*.3)
        por = TexMobject("{", "-64", "\\over", "2", "}").next_to(por_label, RIGHT)
        por_c = TexMobject("c").move_to(por[1].get_center())
        por_a = TexMobject("a").move_to(por[3].get_center())
        por_ans = TexMobject("-32").next_to(por_label, RIGHT)





        # self.add(question)
        self.play(Write(question))
        self.wait(3)
        # self.add(sor_label)
        self.play(Write(sor_label))
        # self.add(por_label)
        self.play(Write(por_label))
        # self.add(sor)
        self.play(
            GrowFromPoint(sor[0], sor.get_center()),
            GrowFromPoint(sor[1], sor.get_center()),
            GrowFromPoint(sor[3], sor.get_center()),
            GrowFromPoint(sor[5], sor.get_center()),
            # GrowFromPoint((sor[i] for i in [0, 1, 3, 5]), sor.get_center()),
            GrowFromPoint(sor_b, sor.get_center()),
            GrowFromPoint(sor_a, sor.get_center()),
        )
        self.wait()
        a = question[0].copy()
        b = question[2].copy()
        self.play(
            Transform(a, sor[4]),
            Transform(b, sor[2]),
            FadeOut(sor_b),
            FadeOut(sor_a),
        )
        self.wait(.5)
        self.remove(a)
        self.remove(b)
        self.play(
            Transform(sor, sor_ans),
        )
        self.wait()





        # self.add(por)
        self.play(
            GrowFromPoint(por[0], por.get_center()),
            GrowFromPoint(por[2], por.get_center()),
            GrowFromPoint(por[4], por.get_center()),
            GrowFromPoint(por_c, por.get_center()),
            GrowFromPoint(por_a, por.get_center()),
        )
        self.wait()
        a = question[0].copy()
        c = question[4].copy()
        self.play(
            Transform(a, por[3]),
            Transform(c, por[1]),
            FadeOut(por_c),
            FadeOut(por_a),
        )
        self.wait(.5)
        self.remove(a)
        self.remove(c)
        self.play(
            Transform(por, por_ans),
        )
        self.wait()



        roots_text = TextMobject("Punca:", color=GREEN).next_to((.25, 1.2, 0), RIGHT)
        roots = TexMobject("\\alpha", ",", "\\beta").next_to(roots_text, RIGHT)


        self.play(Write(roots_text))
        self.play(GrowFromCenter(roots))
        self.wait()


        a = SurroundingRectangle(sor)
        b = SurroundingRectangle(por)
        self.play(ShowCreation(a))
        self.wait()
        self.play(ShowCreation(b))
        self.wait()
        self.play(
            FadeOut(a),
            FadeOut(b),
        )
        self.wait(3)
        a = SurroundingRectangle(VGroup(sor_label, sor))
        self.play(ShowCreation(a))
        self.wait()
        self.play(FadeOut(a))





        addition = TexMobject("(2+u)", "+", "(2-u)", "=", "4").next_to((.25, .4, 0), RIGHT)
        a = TexMobject("2").set_color(ORANGE).move_to(addition[0].get_center())
        b = TexMobject("2").set_color(ORANGE).move_to(addition[2].get_center())
        # -2,6
        # 1,3
        # 7,-3
        self.play(
            Write(addition[1]),
            Write(addition[3]),
            ReplacementTransform(sor_ans, addition[4]),
        )
        self.play(Write(a))
        self.play(Write(b))
        self.wait(2)
        self.play(
            Transform(a, TexMobject("-2").set_color(ORANGE).move_to(addition[0].get_center())),
            Transform(b, TexMobject("6").set_color(ORANGE).move_to(addition[2].get_center())),
        )
        self.wait(.5)
        self.play(
            Transform(a, TexMobject("1").set_color(ORANGE).move_to(addition[0].get_center())),
            Transform(b, TexMobject("3").set_color(ORANGE).move_to(addition[2].get_center())),
        )
        self.wait(.5)
        self.play(
            Transform(a, TexMobject("7").set_color(ORANGE).move_to(addition[0].get_center())),
            Transform(b, TexMobject("(-3)").set_color(ORANGE).move_to(addition[2].get_center())),
        )
        self.wait(3)
        self.play(
            Transform(a, TexMobject("(2+u)").set_color(ORANGE).move_to(addition[0].get_center())),
            Transform(b, TexMobject("(2-u)").set_color(ORANGE).move_to(addition[2].get_center())),
        )
        self.play(Transform(roots, TexMobject("2+", "u", ",", "2-", "u").next_to(roots_text, RIGHT)))
        self.wait(3)





        multiplication1 = TexMobject("(", "2", "+", "u", ")", "(", "2", "-", "u", ")", "=", "-32").next_to((.25, -.4, 0), RIGHT)
        multiplication2 = TexMobject("4", "-", "2u", "+", "2u", "-", "u^2", "=", "-32").next_to((.25, -1.2, 0), RIGHT)

        a = SurroundingRectangle(VGroup(por_label, por))
        self.play(ShowCreation(a))
        self.wait()
        self.play(FadeOut(a))
        self.play(ReplacementTransform(por_ans, multiplication1[11]))
        self.play(Write(multiplication1[0:11]))
        self.wait()
        self.play(
            GrowFromPoint(multiplication2[1], multiplication2.get_center()),
            GrowFromPoint(multiplication2[3], multiplication2.get_center()),
            GrowFromPoint(multiplication2[5], multiplication2.get_center()),
            GrowFromPoint(multiplication2[7], multiplication2.get_center()),
            GrowFromPoint(multiplication2[8], multiplication2.get_center()),
        )
        a = VGroup(multiplication1[1].copy(), multiplication1[6].copy())
        b = VGroup(multiplication1[1].copy(), multiplication1[8].copy())
        c = VGroup(multiplication1[3].copy(), multiplication1[6].copy())
        d = VGroup(multiplication1[3].copy(), multiplication1[8].copy())
        self.play(Transform(a, multiplication2[0]))
        self.play(Transform(b, multiplication2[2]))
        self.play(Transform(c, multiplication2[4]))
        self.play(Transform(d, multiplication2[6]))
        self.wait(3)
        self.play(
            ShowCreation(Cross(multiplication2[2])),
            ShowCreation(Cross(multiplication2[4])),
        )

        multiplication3 = TexMobject("4-u^2", "=", "-32").next_to((.25, -2, 0), RIGHT)
        self.play(Write(multiplication3))
        self.wait()
        self.play(Transform(multiplication3, TexMobject("u^2", "=", "32+4").next_to((.25, -2, 0), RIGHT)))
        self.wait()
        self.play(Transform(multiplication3, TexMobject("u", "=", "\\sqrt{36}").next_to((.25, -2, 0), RIGHT)))
        self.wait()
        self.play(Transform(multiplication3, TexMobject("u", "=", "\\pm", "6").next_to((.25, -2, 0), RIGHT)))
        self.wait()
        self.play(
            multiplication3[2].set_color, GREEN,
            multiplication3[3].set_color, GREEN,
        )
        a = multiplication3[3].copy()
        b = multiplication3[3].copy()
        self.play(
            a.move_to, roots[1].get_center() + UP*.05,
            b.move_to, roots[4].get_center() + UP*.05,
            FadeOut(roots[1]),
            FadeOut(roots[4]),
        )
        self.wait()
        ans = TextMobject("$x=$ ", "$-4$", " atau ", "$8$").scale(1.2).to_edge(LEFT, buff=1.5).shift(DOWN*1.8)
        ans[1].set_color(GREEN)
        ans[3].set_color(GREEN)
        self.play(Write(ans))

        # self.add(roots_text)
        # self.add(roots)
        # self.add(addition)
        # self.add(multiplication1)
        # self.add(multiplication2)
        self.wait(3)