use std::{
    env,
    process::ExitCode,
    sync::atomic::{AtomicBool, Ordering},
};

use libcolors::{
    application::{
        window::{EventOutcome, Window},
        Application,
    },
    error::Error,
    message::{CreateWindowInfo, WindowType},
};
use raqote::{Color, DrawTarget, Gradient, GradientStop, Point, SolidSource, Source, Spread};

fn run_bar() -> Result<ExitCode, Error> {
    let mut app = Application::new()?;
    let mut window = Window::new_with_info(
        &app,
        CreateWindowInfo {
            ty: WindowType::Reservation(32),
        },
    )?;

    window.set_on_redraw_requested(|surface: &mut DrawTarget<&mut [u32]>| {
        let source = Source::new_linear_gradient(
            Gradient {
                stops: vec![
                    GradientStop {
                        position: 0.0,
                        color: Color::new(255, 255, 0, 0),
                    },
                    GradientStop {
                        position: 1.0,
                        color: Color::new(255, 255, 255, 0),
                    },
                ],
            },
            Point::new(0.0, 0.0),
            Point::new(0.0, surface.height() as _),
            Spread::Pad,
        );
        surface.fill_rect(
            0.0,
            0.0,
            surface.width() as _,
            surface.height() as _,
            &source,
            &Default::default(),
        );
    });

    app.add_window(window);
    Ok(app.run())
}

fn main() -> ExitCode {
    match run_bar() {
        Ok(code) => code,
        Err(error) => {
            eprintln!("{error}");
            ExitCode::FAILURE
        }
    }
}
