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

use protocol::{ProtocolReader, ProtocolWriter};

mod decode_image;
mod decode_tile;
mod encode_image;
mod encode_tile;
mod image;
mod png_utils;
mod protocol;

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

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

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

    let mut reader = ProtocolReader::new(Cursor::new(compressed));
    let decompressed_image = decode_image::decode_image(&mut reader).unwrap();

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

