use image2::*;
use show_image::{ImageView, ImageInfo, event, create_window};

#[show_image::main]
fn main() -> Result<(), Error> {
    println!("Hello, world!");

    // Open the image
    // let image = Image::<u8, Rgb>::open("/home/sweetbread/nix/screenshot.png")?;

    // // Show the image
    // let img = ImageView::new(ImageInfo::rgb8(image.width().try_into().unwrap(), image.height().try_into().unwrap()), image.buffer());
    // let win = create_window("image", Default::default()).unwrap();
    // win.set_image("image-001", img).unwrap();

    // // Wait until Escape will be pressed
    // for event in win.event_channel().unwrap() {
    //     if let event::WindowEvent::KeyboardInput(event) = event {
    //         if event.input.key_code == Some(event::VirtualKeyCode::Escape) && event.input.state.is_pressed() {
    //             break;
    //         }
    //     }
    // }

    // Create new imahe
    // let new_image = image.new_like();
    // let mut new_image = new_image.with_color::<Hsv>();

    // for x in 0..image.width() {
    //     for y in 0..image.height() {
    //         let p = image.get_pixel(Point::new(x, y));
    //         let mut hsv_p = Pixel::<Hsv>::new();

    //         for i in 0..3 {
    //             hsv_p[i] = p[i];
    //         }

    //         new_image.set_pixel(Point::new(x, y), &hsv_p);
    //     }
    // }

    // new_image.save("result.png").expect("Failed to save resulting image");

    let mut image = Image::<u8, Hsv>::new(Size::new(100, 100));
    for x in 0..100 {
        for y in 0..100 {
            let mut p = Pixel::<Hsv>::new();
            p[1] = 1.;
            p[2] = 1.;
            image.set_pixel(Point::new(x, y), &p);
        }
    }

    image.save("result.jpg")?;

    Ok(())
}
