use roast2d::{
    prelude::*,
    ui::ui_box::{LayoutDirection, UiBox, UiItem},
};

#[derive(Default)]
pub struct Demo {}

impl Scene for Demo {
    fn draw(&mut self, g: &mut Engine) {
        // Create the main vertical UiBox
        let mut main_box = UiBox::new(400.0).with_gap(15.0);
        // main_box.debug = true; // Enable debug visualization
        main_box.padding = Vec2::new(10.0, 10.0);

        // Add a title text
        let title_text = Text::from_sections(vec![
            TextSection::new("Nested ".to_string(), 24.0, WHITE),
            TextSection::new("Ui".to_string(), 32.0, DEEP_PINK),
            TextSection::new("Box".to_string(), 24.0, YELLOW),
            TextSection::new(" Demo".to_string(), 24.0, WHITE),
        ]);
        main_box.push(UiItem::with_text(g, title_text).unwrap());

        // Create a horizontal UiBox for buttons/items
        let mut horizontal_box = UiBox::new(350.0)
            .with_direction(LayoutDirection::Horizontal)
            .with_gap(10.0);
        // horizontal_box.debug = true;
        horizontal_box.padding = Vec2::new(8.0, 8.0);

        // Add items to the horizontal box
        let button1_text = Text::new("Button 1".to_string(), 16.0, YELLOW);
        horizontal_box.push(UiItem::with_text(g, button1_text).unwrap());

        let button2_text = Text::new("Button 2".to_string(), 16.0, AQUA);
        horizontal_box.push(UiItem::with_text(g, button2_text).unwrap());

        let button3_text = Text::new("Button 3".to_string(), 16.0, PURPLE);
        horizontal_box.push(UiItem::with_text(g, button3_text).unwrap());

        // Add the horizontal box to the main vertical box
        main_box.push(UiItem::with_ui_box(horizontal_box));

        // Add some description text
        let desc_text = Text::new(
            "This demonstrates a horizontal".to_string(),
            14.0,
            LIGHT_GRAY,
        );
        main_box.push(UiItem::with_text(g, desc_text).unwrap());

        let desc_text2 = Text::new(
            "UiBox nested inside a vertical UiBox".to_string(),
            14.0,
            LIGHT_GRAY,
        );
        main_box.push(UiItem::with_text(g, desc_text2).unwrap());

        // Create another horizontal box with different content
        let mut info_box = UiBox::new(350.0)
            .with_direction(LayoutDirection::Horizontal)
            .with_gap(8.0);
        // info_box.debug = true;
        info_box.padding = Vec2::new(5.0, 5.0);

        let info_text1 = Text::new("Info:".to_string(), 12.0, GREEN);
        info_box.push(UiItem::with_text(g, info_text1).unwrap());

        let info_text2 = Text::new("Nested layouts work!".to_string(), 12.0, WHITE);
        info_box.push(UiItem::with_text(g, info_text2).unwrap());

        // Add the info box to the main box
        main_box.push(UiItem::with_ui_box(info_box));

        // Draw the entire nested structure at the center of the screen
        let pos = Vec2::ZERO.extend(1.0);
        main_box.draw(g, pos).unwrap();
    }
}

fn setup(g: &mut Engine) {
    // set resize and scale
    g.set_scale_mode(ScaleMode::Fixed {
        width: 800,
        height: 600,
    });
    g.set_scene(Demo::default());
}

fn run() {
    let app = App::default()
        .title("Nested UiBox Demo".to_string())
        .window(UVec2::new(800, 600))
        .vsync(true)
        .resizable(true);
    app.run(setup).expect("Start game");
}

fn main() {
    env_logger::init();
    run();
}
