use roast2d::prelude::*;
use roast2d_internal::types::{Line, LinePattern};

struct PrimitivesExample;

impl Scene for PrimitivesExample {
    fn init(&mut self, _g: &mut Engine) {}

    fn draw(&mut self, g: &mut Engine) {
        // Draw various lines with different thicknesses and z-indices
        g.draw_line(
            Line {
                start: Vec2::new(50.0, 50.0),
                end: Vec2::new(200.0, 200.0),
                thickness: 3.0,
                color: RED,
            },
            1.0,
        );

        g.draw_line(
            Line {
                start: Vec2::new(0.0, 0.0),
                end: Vec2::new(100.0, 100.0),
                thickness: 3.0,
                color: YELLOW,
            },
            1.1,
        );

        g.draw_line(
            Line {
                start: Vec2::new(50.0, 100.0),
                end: Vec2::new(200.0, 150.0),
                thickness: 5.0,
                color: GREEN,
            },
            0.5,
        );

        g.draw_line(
            Line {
                start: Vec2::new(50.0, 200.0),
                end: Vec2::new(250.0, 180.0),
                thickness: 8.0,
                color: BLUE,
            },
            0.8,
        );

        g.draw_line(
            Line {
                start: Vec2::new(-180.0, 220.0),
                end: Vec2::new(-20.0, 220.0),
                thickness: 20.0,
                color: YELLOW,
            },
            0.5,
        );

        g.draw_line(
            Line {
                start: Vec2::new(-90.0, 250.0),
                end: Vec2::new(-90.0, 180.0),
                thickness: 20.0,
                color: PURPLE,
            },
            1.0,
        );

        // Z-Index Test: Rectangle should occlude lines
        // Draw two lines at z=0.5 (should be behind)
        g.draw_line(
            Line {
                start: Vec2::new(300.0, 100.0),
                end: Vec2::new(400.0, 200.0),
                thickness: 5.0,
                color: RED,
            },
            0.5,
        );

        g.draw_line(
            Line {
                start: Vec2::new(280.0, 120.0),
                end: Vec2::new(380.0, 220.0),
                thickness: 5.0,
                color: BLUE,
            },
            0.5,
        );

        // Draw rectangle at z=1.0 (should be in front, occluding lines)
        g.draw_rect(
            GREEN,
            Vec2::new(100.0, 100.0),
            None,
            Transform::new(Vec3::new(350.0, 150.0, 1.0)),
        );

        // Add text label for the test case
        let test_text = Text::new(
            "Z-Index Test: Green rectangle (z=1.0) should cover red/blue lines (z=0.5)".to_string(),
            16.0,
            WHITE,
        );
        g.draw_text(
            &test_text,
            None,
            Transform::new(Vec3::new(300.0, 250.0, 2.0)),
        );

        // Draw dashed lines
        g.draw_line_ex(
            Line {
                start: Vec2::new(300.0, 50.0),
                end: Vec2::new(450.0, 200.0),
                thickness: 3.0,
                color: RED,
            },
            1.1,
            LinePattern::Dashed {
                dash_length: 10.0,
                gap_length: 5.0,
            },
            0.0,
        );

        // Draw dotted lines
        g.draw_line_ex(
            Line {
                start: Vec2::new(300.0, 250.0),
                end: Vec2::new(450.0, 300.0),
                thickness: 3.0,
                color: BLUE,
            },
            1.0,
            LinePattern::Dotted { spacing: 8.0 },
            0.0,
        );

        // Animated dashed line
        g.draw_line_ex(
            Line {
                start: Vec2::new(300.0, 350.0),
                end: Vec2::new(450.0, 400.0),
                thickness: 4.0,
                color: GREEN,
            },
            1.0,
            LinePattern::Dashed {
                dash_length: 15.0,
                gap_length: 10.0,
            },
            g.time * 20.0, // Animate the pattern
        );

        // Draw circles with different radius and z-indices
        g.draw_circle(Vec3::new(100.0, 300.0, 0.3), 30.0, LIGHT_CYAN);

        g.draw_circle(Vec3::new(200.0, 350.0, 0.7), 50.0, BROWN);

        g.draw_circle(Vec3::new(300.0, 250.0, 0.1), 25.0, YELLOW);

        // Draw concentric circles to show z-ordering
        g.draw_circle(
            Vec3::new(400.0, 200.0, 0.9),
            60.0,
            Color::rgb(1.0, 0.5, 0.0), // Orange
        );

        g.draw_circle(
            Vec3::new(400.0, 200.0, 0.8),
            40.0,
            Color::rgb(0.5, 1.0, 0.0), // Lime
        );

        g.draw_circle(
            Vec3::new(400.0, 200.0, 0.7),
            20.0,
            Color::rgb(0.0, 0.5, 1.0), // Light blue
        );

        // Draw text to explain the example
        let text = Text::new(
            "Primitives Example - Lines & Circles".to_string(),
            24.0,
            WHITE,
        );
        g.draw_text(&text, None, Transform::new(Vec3::new(10.0, 10.0, 2.0)));

        let instructions = Text::new(
            "Lines: different thicknesses & patterns, Circles: different radius and z-ordering"
                .to_string(),
            16.0,
            WHITE,
        );
        g.draw_text(
            &instructions,
            None,
            Transform::new(Vec3::new(10.0, 40.0, 2.0)),
        );
    }
}

fn main() {
    App::default()
        .run(|g| {
            g.set_scene(PrimitivesExample);
        })
        .unwrap();
}
