use image::RgbaImage;
mod square;
use square::Square;
mod setting;
use setting::Setting;

pub fn dithor(in_path: &str, out_path: &str, color: bool) {
    let settings = Setting::new(in_path, color);
    let mut new_img: RgbaImage = image::ImageBuffer::new(settings.width, settings.height);
    let coef = settings.coef();
    for j in 0..settings.height_factor {
        for i in 0..settings.width_factor {
            let a = i * 5 * coef;
            let b = j * settings.width * 5 * coef;
            let c = settings.width * coef;
            let k = a + b;
            let groupped_pixels = [
                &settings.bytes[(k as usize)..((k + 5 * coef) as usize)],
                &settings.bytes[((k + c) as usize)..((k + c + 5 * coef) as usize)],
                &settings.bytes[((k + (2 * c)) as usize)..((k + (2 * c) + 5 * coef) as usize)],
                &settings.bytes[((k + (3 * c)) as usize)..((k + (3 * c) + 5 * coef) as usize)],
                &settings.bytes[((k + (4 * c)) as usize)..((k + (4 * c) + 5 * coef) as usize)],
            ]
            .concat();
            let end_range = 25 * coef as usize;
            let level = settings.get_level(&groupped_pixels[0..end_range]);
            let light_color = settings.light_color(&groupped_pixels[0..end_range]);
            let dark_color = settings.dark_color(&groupped_pixels[0..end_range]);
            let square = Square::new(i * 5, j * 5, level, light_color, dark_color);
            new_img = square.colorize(new_img);
        }
    }
    new_img.save(out_path).unwrap();
}
