use std::collections::HashMap;
use std::fs;
use std::io::{Read, Write};
use std::path::Path;
use std::fs::File;
use image::GenericImageView;
use csv::{Reader, ReaderBuilder, StringRecord};

#[path ="src/simdata.rs"]
mod simdata;

use crate::simdata::*;

fn main() {
    linker_be_nice();
    // make sure linkall.x is the last linker script (otherwise might cause problems with flip-link)
    println!("cargo:rustc-link-arg=-Tlinkall.x");

    #[cfg(feature="simulation")]
    write_sim_data();

    compile_assets();

}

fn compile_assets() {
    let asset_path = Path::new("assets");

    let mut image_output = File::create(Path::new("target/images.rs")).unwrap();
    for image in fs::read_dir(asset_path).unwrap() {
        let fname = image.unwrap().file_name();
        let fname_str = fname.to_str().unwrap();
        if fname_str.ends_with(".pbm") {
            let img = image::open(asset_path.join(fname_str)).unwrap();
            let img_name = fname_str.rsplit_once('.').unwrap().0.to_uppercase().replace("-", "_");

            let mut converted_row = Vec::new();
            let mut byte_buf = String::new();
            image_output.write_all(format!("pub const {img_name}: ImageRaw<BinaryColor> = ImageRaw::new(&[\n").as_bytes()).unwrap();
            for (x, _, pixel) in img.pixels() {
                if pixel.0 == [0, 0, 0, 255] {
                    byte_buf.push('1');
                } else {
                    byte_buf.push('0');
                }
                if byte_buf.len() == 8 {
                    converted_row.push(byte_buf);
                    byte_buf = String::new();
                }
                if x == img.width() - 1 {
                    if !byte_buf.is_empty() {
                        byte_buf.push('_');
                        for _ in 0..(9 - byte_buf.len()) {
                            byte_buf.push('0');
                        }
                        converted_row.push(byte_buf);
                        byte_buf = String::new();
                    }
                    image_output.write_all(b"    ").unwrap();
                    for pix in converted_row.iter() {
                        image_output.write_all(format!("0b{pix}, ").as_bytes()).unwrap();
                    }
                    image_output.write_all(b"\n").unwrap();
                    converted_row = Vec::new();
                }
            }

            image_output.write_all(format!("], {});\n", img.width()).as_bytes()).unwrap();
            println!("cargo::rerun-if-changed={fname_str}");
        }
    }
}

trait FromRecord {
    fn from_record(records: &[StringRecord], headers: &[HashMap<String, usize>]) -> Self;
}

impl FromRecord for AnnotationReading {
        fn from_record(records: &[StringRecord], headers: &[HashMap<String, usize>]) -> Self {
        let text = records[0].get(headers[0]["text"]).unwrap();
        let mut data = AnnotationReading::default();
        data.buf[..text.len()].copy_from_slice(text.as_bytes());

        data
    }
}

impl FromRecord for GPSReading {
    fn from_record(records: &[StringRecord], headers: &[HashMap<String, usize>]) -> Self {
        Self {
            lat: records[0].get(headers[0]["latitude"]).unwrap().parse().unwrap(),
            lon: records[0].get(headers[0]["longitude"]).unwrap().parse().unwrap()
        }
    }
}

impl FromRecord for IMUReading {
    fn from_record(records: &[StringRecord], headers: &[HashMap<String, usize>]) -> Self {
        Self {
            accel_x: records[0].get(headers[0]["x"]).unwrap().parse().unwrap(),
            accel_y: records[0].get(headers[0]["y"]).unwrap().parse().unwrap(),
            accel_z: records[0].get(headers[0]["z"]).unwrap().parse().unwrap(),

            gyro_x: records[1].get(headers[1]["x"]).unwrap().parse().unwrap(),
            gyro_y: records[1].get(headers[1]["y"]).unwrap().parse().unwrap(),
            gyro_z: records[1].get(headers[1]["z"]).unwrap().parse().unwrap(),
        }
    }
}

fn generate_sim_data<Event: EventRecord + FromRecord>(srcs: &[&Path], dest: &Path, skip_rate: usize) {

    for src in srcs {
        println!("cargo::rerun-if-changed={}", src.to_str().unwrap());
    }
    if dest.exists() {
        let last_modified = dest.metadata().unwrap().modified().unwrap();
        let any_src_newer = srcs.iter().map(|src| {
            src.metadata().unwrap().modified().unwrap()
        }).any(|stamp| {
            stamp > last_modified
        });

        if !any_src_newer {
            return;
        }
    }
    
    // Calculate the total record cound based on how many records are in the first file
    let fd = File::open(srcs[0]).unwrap();
    let mut reader = ReaderBuilder::new().has_headers(true).from_reader(fd);
    let header = EventStreamHeader {
        count: reader.records().count()
    };

    let mut output = File::create(dest).unwrap();

    header.write_rmp(&mut output).unwrap();

    let mut readers: Vec<_> = srcs.iter().map(|src| {
        let fd = File::open(src).unwrap();
        ReaderBuilder::new().has_headers(true).from_reader(fd)
    }).collect();

    let mut last_stamp = 0.0;
    let headers: Vec<HashMap<_, _>> = readers.iter_mut().map(|reader| {
        reader.headers().unwrap().iter().enumerate().map(|x| { (x.1.to_owned(), x.0) } ).collect()
    }).collect();

    let mut all_records: Vec<_> = readers.iter_mut().map(|reader| {
        reader.records()
    }).collect();

    let mut skip_count = 0;

    loop {
        let mut next: Vec<_> = all_records.iter_mut().map(|reader| { reader.next() }).collect();

        // If any of the data files rusn out, simply quit. This does not verify that the written number of records is correct, however
        if next.iter().any(|x| { x.is_none() }) {
            break;
        }

        let next: Vec<_> = next.iter_mut().map(|x| { x.take().unwrap().unwrap() }).collect();

        let data = Event::from_record(next.as_slice(), headers.as_slice());
        //eprintln!("next={next:?} headers={headers:?}");

        let this_timecode: f64 = next[0].get(headers[0]["seconds_elapsed"]).unwrap().parse().unwrap();
        let time_delta = this_timecode - last_stamp;
        let record = StreamEvent {
            timecode: time_delta,
            data
        };

        if skip_count == 0 {
            last_stamp = this_timecode;
            record.write_rmp(&mut output).unwrap();
            skip_count = skip_rate;
        } else {
            skip_count -= 1;
        }
    }
}

fn write_sim_data() {
    let test_data_path = Path::new("test-data");
    let output_path = Path::new("target");
    let gps_input = test_data_path.join("LocationGps.csv");
    let gyro_input = test_data_path.join("GyroscopeUncalibrated.csv");
    let accel_input = test_data_path.join("AccelerometerUncalibrated.csv");
    let annotation_input = test_data_path.join("Annotation.csv");

    let gps_output = output_path.join("gps_test_data.msgpack");
    let motion_output = output_path.join("motion_test_data.msgpack");
    let annotation_output = output_path.join("annotations.msgpack");
    let unified_output = output_path.join("unified.msgpack");

    generate_sim_data::<AnnotationReading>(&[&annotation_input], &annotation_output, 0);
    generate_sim_data::<GPSReading>(&[&gps_input], &gps_output, 0);
    generate_sim_data::<IMUReading>(&[&accel_input, &gyro_input], &motion_output, 3);

    let mut unified_fd = File::create(unified_output.clone()).unwrap();

    let segments = [(StreamType::IMU, motion_output), (StreamType::GPS, gps_output), (StreamType::Annotations, annotation_output)];

    // Write out the stream index header
    rmp::encode::write_array_len(&mut unified_fd, segments.len() as u32).unwrap();

    // Then the streams
    for (stream_type, stream_path) in segments {
        let mut fd = File::open(stream_path).unwrap();
        rmp::encode::write_ext_meta(&mut unified_fd, fd.metadata().unwrap().len() as u32, stream_type.into()).unwrap();
        let mut buf = Vec::new();
        fd.read_to_end(&mut buf).unwrap();
        unified_fd.write_all(buf.as_slice()).unwrap();
    }

    let mut partitions = Reader::from_reader(File::open("partitions.csv").unwrap());
    let mut data_offset = 0x9000; // Assumes default bootloader size (0x7000) plus partition table (0x2000)
    let mut data_size = 0;
    let mut found_sim_partition = false;
    for p_desc in partitions.records().flatten() {
        let offset = parse_partition_number(p_desc.get(3).unwrap().trim());
        data_size = parse_partition_number(p_desc.get(4).unwrap().trim()).unwrap();
        data_offset = offset.unwrap_or(data_offset);
        if let Some("sim") = p_desc.get(0) {
            found_sim_partition = true;
            break;
        } else {
            data_offset += data_size;
        }
    }

    if !found_sim_partition {
        panic!("Could not find a 'sim' partition in partitions.csv!");
    }

    if unified_fd.metadata().unwrap().len() as usize >= data_size {
        // FIXME: Need to implement automatic data resampling
        panic!("Simulation data is too big! Cannot fit {:#x} bytes into a partition with a size of {data_size:#x} bytes.", unified_fd.metadata().unwrap().len());
    }

    let mut data_flash_script = File::create(output_path.join("flash-sim-data.sh")).unwrap();
    data_flash_script.write_all(format!("espflash write-bin {data_offset:#x?} {}", unified_output.to_str().unwrap()).as_bytes()).unwrap();
}

fn parse_partition_number(n: &str) -> Option<usize> {
    if n.is_empty() {
        None
    } else if let Some(hex_offset) = n.strip_prefix("0x") {
        Some(usize::from_str_radix(hex_offset, 16).unwrap())
    } else if let Some(mb_offset) = n.strip_suffix("M") {
        Some(mb_offset.parse::<usize>().unwrap() * 1024 * 1024)
    } else if let Some(kb_offset) = n.strip_suffix("K") {
        Some(kb_offset.parse::<usize>().unwrap() * 1024)
    } else {
        Some(n.parse().unwrap())
    }
}

fn linker_be_nice() {
    let args: Vec<String> = std::env::args().collect();
    if args.len() > 1 {
        let kind = &args[1];
        let what = &args[2];

        match kind.as_str() {
            "undefined-symbol" => match what.as_str() {
                "_defmt_timestamp" => {
                    eprintln!();
                    eprintln!("💡 `defmt` not found - make sure `defmt.x` is added as a linker script and you have included `use defmt_rtt as _;`");
                    eprintln!();
                }
                "_stack_start" => {
                    eprintln!();
                    eprintln!("💡 Is the linker script `linkall.x` missing?");
                    eprintln!();
                }
                "esp_wifi_preempt_enable"
                | "esp_wifi_preempt_yield_task"
                | "esp_wifi_preempt_task_create" => {
                    eprintln!();
                    eprintln!("💡 `esp-wifi` has no scheduler enabled. Make sure you have the `builtin-scheduler` feature enabled, or that you provide an external scheduler.");
                    eprintln!();
                }
                "embedded_test_linker_file_not_added_to_rustflags" => {
                    eprintln!();
                    eprintln!("💡 `embedded-test` not found - make sure `embedded-test.x` is added as a linker script for tests");
                    eprintln!();
                }
                _ => (),
            },
            // we don't have anything helpful for "missing-lib" yet
            _ => {
                std::process::exit(1);
            }
        }

        std::process::exit(0);
    }

    println!(
        "cargo:rustc-link-arg=-Wl,--error-handling-script={}",
        std::env::current_exe().unwrap().display()
    );
}
