"""
Example ManimGL Scenes for testing the MCP server.

These scenes demonstrate common patterns that can be generated via the MCP server.
Run with: manimgl examples/basic_scenes.py SceneName
"""

from manimlib import *


class HelloCircle(Scene):
    """A simple scene that creates and animates a circle."""
    
    def construct(self):
        # Create a blue circle
        circle = Circle(color=BLUE, radius=2)
        circle.set_fill(BLUE, opacity=0.5)
        
        # Animate its creation
        self.play(ShowCreation(circle))
        self.wait()
        
        # Move it around
        self.play(circle.animate.shift(RIGHT * 2))
        self.wait()
        
        # Scale it
        self.play(circle.animate.scale(0.5))
        self.wait()


class ShapeTransform(Scene):
    """Demonstrates transforming one shape into another."""
    
    def construct(self):
        # Create shapes
        circle = Circle(color=BLUE)
        square = Square(color=RED)
        triangle = Triangle(color=GREEN)
        
        # Position them
        circle.shift(LEFT * 3)
        triangle.shift(RIGHT * 3)
        
        # Show circle
        self.play(ShowCreation(circle))
        self.wait()
        
        # Transform to square
        self.play(Transform(circle, square))
        self.wait()
        
        # Transform to triangle
        self.play(Transform(circle, triangle))
        self.wait()


class TextAnimation(Scene):
    """Shows text creation and animation."""
    
    def construct(self):
        # Create text
        title = Text("Hello, Manim!", font_size=72)
        title.set_color(YELLOW)
        
        # Write it
        self.play(Write(title))
        self.wait()
        
        # Create subtitle
        subtitle = Text("Mathematical Animations", font_size=36)
        subtitle.next_to(title, DOWN, buff=0.5)
        subtitle.set_color(BLUE)
        
        self.play(FadeIn(subtitle, shift=UP))
        self.wait()
        
        # Transform text
        new_title = Text("Goodbye!", font_size=72, color=RED)
        self.play(TransformMatchingShapes(title, new_title))
        self.wait()


class MathEquation(Scene):
    """Demonstrates LaTeX math rendering."""
    
    def construct(self):
        # Famous equations
        equations = VGroup(
            Tex(r"E = mc^2"),
            Tex(r"e^{i\pi} + 1 = 0"),
            Tex(r"\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}"),
        )
        
        equations.arrange(DOWN, buff=1)
        equations.scale(1.5)
        
        # Animate each equation
        for eq in equations:
            self.play(Write(eq))
            self.wait(0.5)
        
        self.wait()
        
        # Highlight the middle one
        self.play(
            equations[1].animate.set_color(YELLOW).scale(1.2),
            equations[0].animate.set_opacity(0.3),
            equations[2].animate.set_opacity(0.3),
        )
        self.wait()


class GraphExample(Scene):
    """Shows how to create and animate graphs."""
    
    def construct(self):
        # Create axes
        axes = Axes(
            x_range=(-3, 3),
            y_range=(-2, 2),
            height=5,
            width=8,
        )
        axes.add_coordinate_labels()
        
        # Create a sine graph
        sine_graph = axes.get_graph(
            lambda x: np.sin(x),
            color=BLUE
        )
        sine_label = axes.get_graph_label(sine_graph, r"\sin(x)")
        
        # Animate
        self.play(Write(axes))
        self.wait()
        
        self.play(ShowCreation(sine_graph), Write(sine_label))
        self.wait()
        
        # Add a moving dot
        dot = Dot(color=RED)
        dot.move_to(axes.i2gp(-3, sine_graph))
        
        self.play(FadeIn(dot))
        self.play(
            MoveAlongPath(dot, sine_graph),
            run_time=4
        )
        self.wait()


class ThreeDExample(ThreeDScene):
    """A simple 3D scene."""
    
    def construct(self):
        # Set up 3D camera
        self.set_camera_orientation(phi=75 * DEGREES, theta=30 * DEGREES)
        
        # Create 3D axes
        axes = ThreeDAxes()
        
        # Create a sphere
        sphere = Sphere(radius=1)
        sphere.set_color(BLUE)
        sphere.set_opacity(0.7)
        
        # Create a cube
        cube = Cube(side_length=2)
        cube.set_color(RED)
        cube.set_opacity(0.5)
        cube.shift(RIGHT * 3)
        
        # Add and animate
        self.play(ShowCreation(axes))
        self.play(FadeIn(sphere), FadeIn(cube))
        
        # Rotate camera
        self.begin_ambient_camera_rotation(rate=0.2)
        self.wait(5)


class NumberLineAnimation(Scene):
    """Animates values on a number line."""
    
    def construct(self):
        # Create number line
        number_line = NumberLine(
            x_range=(-5, 5, 1),
            length=10,
            include_numbers=True,
        )
        
        self.play(ShowCreation(number_line))
        self.wait()
        
        # Create a tracker
        tracker = ValueTracker(-4)
        
        # Create a dot that follows the tracker
        dot = Dot(color=RED)
        dot.add_updater(
            lambda m: m.move_to(number_line.n2p(tracker.get_value()))
        )
        
        # Create a label
        label = DecimalNumber(num_decimal_places=1)
        label.add_updater(lambda m: m.set_value(tracker.get_value()))
        label.add_updater(lambda m: m.next_to(dot, UP))
        
        self.add(dot, label)
        
        # Animate the tracker
        self.play(tracker.animate.set_value(4), run_time=3)
        self.wait()
        self.play(tracker.animate.set_value(0), run_time=2)
        self.wait()


class MatrixTransform(Scene):
    """Visualizes a linear transformation."""
    
    def construct(self):
        # Create a grid
        grid = NumberPlane()
        grid.set_stroke(BLUE_D, 1)
        
        # Add basis vectors
        i_hat = Arrow(ORIGIN, RIGHT, buff=0, color=GREEN)
        j_hat = Arrow(ORIGIN, UP, buff=0, color=RED)
        
        i_label = Tex(r"\hat{i}", color=GREEN).next_to(i_hat, DOWN)
        j_label = Tex(r"\hat{j}", color=RED).next_to(j_hat, LEFT)
        
        basis = VGroup(i_hat, j_hat, i_label, j_label)
        
        # Show grid and basis
        self.play(ShowCreation(grid), run_time=2)
        self.play(ShowCreation(basis))
        self.wait()
        
        # Define transformation matrix
        matrix = [[2, 1], [0, 1]]
        
        # Show the matrix
        matrix_mob = IntegerMatrix(matrix)
        matrix_mob.to_corner(UL)
        self.play(Write(matrix_mob))
        self.wait()
        
        # Apply transformation
        self.play(
            grid.animate.apply_matrix(matrix),
            i_hat.animate.put_start_and_end_on(ORIGIN, [2, 0, 0]),
            j_hat.animate.put_start_and_end_on(ORIGIN, [1, 1, 0]),
            run_time=3
        )
        self.wait()


# List of all scenes for easy reference
SCENES_IN_ORDER = [
    HelloCircle,
    ShapeTransform,
    TextAnimation,
    MathEquation,
    GraphExample,
    ThreeDExample,
    NumberLineAnimation,
    MatrixTransform,
]
