use std::{fs::File, io::{Cursor, Write}};

use crate::{protocol::{ProtocolReader, ProtocolWriter}, quality_settings::QualitySettings};

mod compression;
mod constants;
mod decompression;
mod png_utils;
mod protocol;
mod quality_settings;
mod quantization;
mod transform;
mod image;


fn main() {
    run_for("zonked".to_string());
    run_for("avatar2".to_string());
}
fn run_for(name: String) {
    let image = png_utils::load_image(
        format!("inputs/{}.png", name),
        QualitySettings::new(2),
        QualitySettings::new(2),
    ).unwrap();
    let mut writer = ProtocolWriter::new(vec![]);

    compression::compress(
        image,
        &mut writer
    ).unwrap();
    let compressed = writer.destroy();

    let mut output_file = File::create(format!("outputs/{}.rxi", name)).unwrap();
    output_file.write_all(&compressed).unwrap();

    let mut reader = ProtocolReader::new(Cursor::new(compressed));
    let decompressed = decompression::decompress(&mut reader).unwrap();

    png_utils::save_image(
        format!("outputs/{}_out.png", name), 
        decompressed
    ).unwrap();
}
