use crate::utils::read_float_file;
use shared::Float;
use shared::core::color::{Coeffs, RES, RGBToSpectrumTable};
use std::ops::Deref;
use std::path::Path;

pub struct RGBToSpectrumTableData {
    _z_nodes: Vec<Float>,
    _coeffs: Vec<Float>,

    pub view: RGBToSpectrumTable,
}

impl Deref for RGBToSpectrumTableData {
    type Target = RGBToSpectrumTable;
    fn deref(&self) -> &Self::Target {
        &self.view
    }
}

impl RGBToSpectrumTableData {
    pub fn new(z_nodes: Vec<Float>, coeffs: Vec<Float>) -> Self {
        assert_eq!(z_nodes.len(), RES);
        assert_eq!(coeffs.len(), RES * RES * RES * 3 * 3); // bucket*z*y*x*3(coeffs)

        let view = RGBToSpectrumTable {
            z_nodes: z_nodes.as_ptr(),
            coeffs: coeffs.as_ptr() as *const Coeffs,
        };

        Self {
            _z_nodes: z_nodes,
            _coeffs: coeffs,
            view,
        }
    }

    pub fn load(base_dir: &Path, name: &str) -> Result<Self> {
        let z_path = base_dir.join(format!("{}_znodes.dat", name));
        let c_path = base_dir.join(format!("{}_coeffs.dat", name));

        let z_nodes = read_float_file(&z_path)?;
        let coeffs = read_float_file(&c_path)?;

        Ok(Self::new(z_nodes, coeffs))
    }
}
