///
#[allow(warnings)]
#[cfg(feature = "screenshot")]
pub mod screenshot {
    use image::{GenericImageView, ImageBuffer, Rgba, RgbaImage};

    use crate::images;

    pub fn capture_all() {
        use screenshots::Screen;
        let screens = Screen::all().unwrap();
        for (index, screen) in screens.iter().enumerate() {
            let mut image = screen.capture().unwrap();
            image.save(format!("screenshot_{}.png", index)).unwrap();
        }
    }

    pub fn cut_area(img: ImageBuffer<Rgba<u8>, Vec<u8>>, x: u32, y: u32, width: u32, height: u32) -> Option<RgbaImage> {
        let sub_image = img.view(x, y, width, height);
        let mut cut_image = RgbaImage::new(width, height);
        for (x, y, pixel) in sub_image.pixels() {
            cut_image.put_pixel(x, y, pixel);
        }
        Some(cut_image)
    }
  
    pub fn capture_area(
        x: u32,
        y: u32,
        width: u32,
        height: u32
    ) -> Option<RgbaImage>{
        use screenshots::Screen;
        let screens = Screen::all().unwrap();
        if let Some(screen) = screens.iter().nth(0){
           let image = screen.capture().unwrap();
           let new_image = cut_area(image, x, y, width, height);
           return new_image;
        }else{
            return None;
        }
    }
    pub fn get_rgb_from_position(image_path: &str, x: u32, y: u32) -> Option<(u8, u8, u8)> {
        use image::{GenericImageView, ImageBuffer, Rgb};
        let image = image::open(image_path).expect("Failed to open image");
        let width = image.width();
        let height = image.height();
        if x < width && y < height {
            let pixel = image.get_pixel(x, y);
            let r = pixel[0];
            let g = pixel[1];
            let b = pixel[2];
            let a = pixel[3];
            return Some((r, g, b));
        } else {
            return None;
        }
    }

    pub fn screen_position_is(position: (u32, u32), hex: &str) -> bool {
        capture_all();
        if crate::color::color::rgb_to_hex(
            get_rgb_from_position("screenshot_0.png", position.0, position.1).unwrap(),
        ) == hex
        {
            true
        } else {
            false
        }
    }
}

#[cfg(feature = "screenshot")]
pub use screenshot::*;
