use std::path::Path;
use serde::de::DeserializeOwned;
use crate::cli::cli::OutputFormat;
use crate::system::model::HistoryLine;
use crate::system::Position;

pub mod boxcount;
pub mod render;

pub fn read<T: Position>(path: &Path, format: OutputFormat) -> Vec<T> where T: DeserializeOwned {
    match format {
        OutputFormat::FullDataJson => read_json(path),
        OutputFormat::Positions => read_csv(path)
    }
}

pub fn read_json<T: Position>(path: &Path) -> Vec<T> where T: DeserializeOwned {
    serde_json::from_reader::<_, Vec<HistoryLine<T>>>(File::open(path).expect("Failed to open file"))
        .expect("Failed to read json")
        .iter()
        .map(|l| (l.position.clone()))
        .collect::<Vec<_>>()
}

pub fn read_csv<T: Position>(path: &Path) -> Vec<T> where T: DeserializeOwned {
    csv::Reader::from_path(path).expect("Failed to read positions csv").deserialize::<T>()
        .collect::<Result<Vec<T>, _>>()
        .unwrap()
}
