from manimlib.imports import *

class Thumbnail(Scene):
    pass

class PictureThis(GraphScene):
    CONFIG = {
        "y_max": 50,
        "y_min": 0,
        "x_max": 10,
        "x_min": 0,
        "y_tick_frequency": 10, 
        "x_tick_frequency": 1, 
        # "axes_color" : PURPLE,
        "y_labeled_nums": [30,40],
        "x_labeled_nums": [10],
        "graph_origin": DOWN*3 + LEFT*4,
        "y_axis_label": "$v/ms^{-1}$",
        "x_axis_label": "$t/s$",
        "y_axis_height": 5,
        "x_axis_width": 8,
        "exclude_zero_label": False,
    }

    def construct(self):
        car = SVGMobject("car", stroke_width=0).flip(UP).scale(.5)
        u = TexMobject("30~ms^{-1}").to_edge(LEFT, buff=1)
        v = TexMobject("40~ms^{-1}").to_edge(RIGHT, buff=1)
        arrow = Arrow(u.get_left(), v.get_right()).shift(DOWN)
        accel_txt = Text("Acceleration?").set_color_by_gradient(BLUE, GREEN).shift(UP*1.5)
        accel_title = Text("Acceleration").set_color_by_gradient(BLUE, GREEN).to_edge(UP)
        VGroup(car, u, v, arrow, accel_txt).center()

        self.play(Write(car))
        self.wait()
        self.play(Write(u))
        self.wait()
        self.play(Write(arrow))
        self.play(Write(v))
        self.wait()
        self.play(Write(accel_txt))
        self.wait()

        # Graph

        self.play(
            *[FadeOut(v) for v in [car, arrow, accel_txt[12], u, v]],
            ReplacementTransform(accel_txt.find("Acceleration"), accel_title, path_arc=-np.pi/2)
        )

        self.setup_axes(animate=True)
        graph = self.get_graph(
            lambda x: x + 30,
            color=TEAL,
            x_min=0,
            x_max=10
        )
        # self.play(ShowCreation(graph))

        brace1 = Brace(graph, RIGHT).set_color(YELLOW)
        brace2 = Brace(graph, DOWN).set_color(YELLOW)
        brace1_txt = brace1.get_text("$10~ms^{-1}$").set_color(YELLOW)
        brace2_txt = brace2.get_text("$10~s$").set_color(YELLOW)

        self.play(
            ShowCreation(graph),
            *[Write(v) for v in [brace1, brace2, brace1_txt, brace2_txt]],
        )
        self.wait()

        # Answer

        gradient = TexMobject("{", "10~ms^{-1}", "\\over", "10~s", "}", "\\\\=1~ms^{-2}").to_edge(RIGHT, buff=1)
        gradient[1].set_color(YELLOW)
        gradient[3].set_color(YELLOW)

        self.play(
            VGroup(
                self.x_axis,
                self.y_axis,
                graph,
            ).to_edge, LEFT, {"buff": 1},
            FadeOut(brace1),
            FadeOut(brace2),
            Write(gradient[2]),
            brace1_txt.replace, gradient[1],
            brace2_txt.replace, gradient[3],
        )
        self.play(Write(gradient[5]))

        self.wait(3)

class Question(GraphScene):
    CONFIG = {
        "y_max": 7,
        "y_min": 0,
        "x_max": 5,
        "x_min": 0,
        "y_tick_frequency": 1, 
        "x_tick_frequency": 1, 
        # "axes_color" : PURPLE,
        "y_labeled_nums": range(1,8),
        "x_labeled_nums": range(1,6),
        "graph_origin": DOWN*3 + LEFT*4,
        "y_axis_label": "$v/ms^{-1}$",
        "x_axis_label": "$t/s$",
        "y_axis_height": 5,
        "x_axis_width": 8,
        "exclude_zero_label": False,
    }

    def construct(self):
        func = TexMobject("f(t)=5t-t^2")

        # Setup graph

        self.play(Write(func))
        self.wait()
        self.play(func.to_edge, UP)
        self.setup_axes(animate=True)
        self.wait()

        graph = self.get_graph(
            lambda x: 5*x - x**2,
            color=TEAL,
            x_min=0,
            x_max=5
        )
        vert_line = DashedLine(self.coords_to_point(1,0), self.coords_to_point(1,4), color=YELLOW)
        intersection = Circle(radius=.1, color=YELLOW).move_to(self.coords_to_point(1,4))

        # Show graph

        self.play(ShowCreation(graph))
        self.wait()

        # Ask

        self.play(ShowCreation(vert_line))
        self.play(ShowCreation(intersection))
        self.wait()

        # Two points

        point1 = Dot(color=RED).move_to(self.coords_to_point(1,4))
        point2 = Dot(color=RED).move_to(self.coords_to_point(3.5,5.25))

        self.play(
            AnimationGroup(
                FadeInFromDown(point1),
                FadeInFromDown(point2),
                lag_ratio=.2
            )
        )
        false_grad = self.get_graph(
            lambda x: .5*x + 3.5,
            color=RED,
            x_min=0,
            x_max=5
        )
        self.play(ShowCreation(false_grad))
        self.wait()

        # However

        tangent = self.get_graph(
            lambda x: 3*x + 1,
            color=ORANGE,
            x_min=0,
            x_max=2
        )
        self.play(*[FadeOut(v) for v in [false_grad, point1, point2]])
        self.play(ShowCreation(tangent), run_time=2)

        self.wait(3)