use crate::utils::read_float_file;
use shared::spectra::PiecewiseLinearSpectrum;

pub trait ReadFromFile: Sized {
    fn read(filepath: &str) -> Option<Self>;
}

impl ReadFromFile for PiecewiseLinearSpectrum {
    fn read(filepath: &str) -> Option<Self> {
        let vals = read_float_file(filepath).ok()?;
        if vals.is_empty() || vals.len() % 2 != 0 {
            return None;
        }
        for pair in vals.chunks(2).collect::<Vec<_>>().windows(2) {
            if pair[1][0] <= pair[0][0] {
                return None;
            }
        }
        Some(Self::from_interleaved(&vals, false))
    }
}
