use std::fs;
use std::fs::File;
use std::io::Write;
use image::{ImageBuffer, RgbImage};
use nexrad2::parse_nx2_chunk;
use rtwx_render::NexradRenderExt;

fn main() {
    simple_logger::init_with_env().unwrap();
    let file = std::env::args().nth(1).unwrap();

    let mut data = File::open(file).unwrap();

    let chunk = parse_nx2_chunk(&mut data).unwrap();

    let elevation: usize = std::env::args().nth(2).unwrap().parse().unwrap();
    let mode = std::env::args().nth(3).unwrap();
    let img_size: usize = std::env::args().nth(4).unwrap().parse().unwrap();

    let img = chunk.render(elevation, mode.as_str(), img_size).unwrap();

    let out_file = std::env::args().nth(5).unwrap();

    let mut img_rgb: RgbImage = ImageBuffer::new(img_size as u32, img_size as u32);
    for x in 0..img_size {
        for y in 0..img_size {
            let pixel = img[y * img_size + x];
            img_rgb.put_pixel(x as u32, y as u32, image::Rgb([pixel.0, pixel.1, pixel.2]));
        }
    }

    img_rgb.save(out_file).unwrap();
}