use std::fs::File;
use std::io::{self, Read};

use crate::chunker::{chunk_data, ChunkingConfig};
use crate::utils::{Chunk, DGGSCell};

pub type Config = ChunkingConfig;

/// Ingests stream.
pub fn ingest_stream(source: &str, config: &Config) -> io::Result<Vec<Chunk>> {
    let mut f = File::open(source)?;
    let mut buf = Vec::new();
    f.read_to_end(&mut buf)?;
    Ok(chunk_data(&buf, config.chunk_size))
}

/// Maps dggs cell.
pub fn map_dggs_cell(_chunk: &Chunk) -> DGGSCell {
    DGGSCell { id: 0 }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    /// Tests that ingest stream reads and chunks file.
    #[test]
    fn ingest_stream_reads_and_chunks_file() {
        let mut file = NamedTempFile::new().expect("temp file");
        file.write_all(b"abcdefghijkl").expect("write");
        let cfg = Config { chunk_size: 5 };

        let chunks =
            ingest_stream(file.path().to_str().expect("utf8 path"), &cfg).expect("ingest stream");

        assert_eq!(chunks.len(), 3);
        assert_eq!(chunks[0].data, b"abcde");
        assert_eq!(chunks[1].data, b"fghij");
        assert_eq!(chunks[2].data, b"kl");
    }

    /// Tests that map dggs cell returns default cell.
    #[test]
    fn map_dggs_cell_returns_default_cell() {
        let chunk = Chunk {
            data: vec![1, 2, 3],
        };
        let cell = map_dggs_cell(&chunk);
        assert_eq!(cell.id, 0);
    }
}
