use crate::globals::{ACES_TABLE, DCI_P3_TABLE, REC2020_TABLE, SRGB_TABLE};
use crate::spectra::colorspace::RGBColorSpaceData;
use shared::core::geometry::Point2f;
use shared::core::spectrum::Spectrum;
use shared::core::spectrum::StandardSpectra;
use shared::spectra::DeviceStandardColorSpaces;
use shared::spectra::cie::{CIE_D65, CIE_X, CIE_Y, CIE_Z};
use shared::utils::Ptr;
use std::sync::Arc;
use std::sync::LazyLock;

pub mod colorspace;
pub mod data;
pub mod dense;
pub mod piecewise;

pub use dense::DenselySampledSpectrumBuffer;

fn get_d65_illuminant_buffer() -> &DenselySampledSpectrumBuffer {
    &CIE_D65_DATA
}

static CIE_X_DATA: LazyLock<DenselySampledSpectrumBuffer> =
    LazyLock::new(|| data::create_cie_buffer(&CIE_X));
static CIE_Y_DATA: LazyLock<DenselySampledSpectrumBuffer> =
    LazyLock::new(|| data::create_cie_buffer(&CIE_Y));
static CIE_Z_DATA: LazyLock<DenselySampledSpectrumBuffer> =
    LazyLock::new(|| data::create_cie_buffer(&CIE_Z));
static CIE_D65_DATA: LazyLock<DenselySampledSpectrumBuffer> =
    LazyLock::new(|| data::create_cie_buffer(&CIE_D65));

pub fn cie_x() -> Spectrum {
    Spectrum::Dense(CIE_X_DATA.device())
}

pub fn cie_y() -> Spectrum {
    Spectrum::Dense(CIE_Y_DATA.device())
}

pub fn cie_z() -> Spectrum {
    Spectrum::Dense(CIE_Z_DATA.device())
}

pub fn cie_d65() -> Spectrum {
    Spectrum::Dense(CIE_D65_DATA.device())
}

pub fn get_spectra_context() -> StandardSpectra {
    StandardSpectra {
        x: CIE_X_DATA.device(),
        y: CIE_Y_DATA.device(),
        z: CIE_Z_DATA.device(),
        d65: CIE_D65_DATA.device(),
    }
}

pub static SRGB: LazyLock<Arc<RGBColorSpaceData>> = LazyLock::new(|| {
    let illum = get_d65_illuminant_buffer();
    let r = Point2f::new(0.64, 0.33);
    let g = Point2f::new(0.3, 0.6);
    let b = Point2f::new(0.15, 0.06);
    let table_ptr = Ptr::from(&SRGB_TABLE.clone());

    Arc::new(RGBColorSpaceData::new(r, g, b, illum, table_ptr))
});

pub static DCI_P3: LazyLock<Arc<RGBColorSpaceData>> = LazyLock::new(|| {
    let illum = get_d65_illuminant_buffer();
    let r = Point2f::new(0.680, 0.320);
    let g = Point2f::new(0.265, 0.690);
    let b = Point2f::new(0.150, 0.060);

    let table_ptr = Ptr::from(&DCI_P3_TABLE.clone());

    Arc::new(RGBColorSpaceData::new(r, g, b, illum, table_ptr))
});

pub static REC2020: LazyLock<Arc<RGBColorSpaceData>> = LazyLock::new(|| {
    let illum = get_d65_illuminant_buffer();
    let r = Point2f::new(0.708, 0.292);
    let g = Point2f::new(0.170, 0.797);
    let b = Point2f::new(0.131, 0.046);

    let table_ptr = Ptr::from(&REC2020_TABLE.clone());

    Arc::new(RGBColorSpaceData::new(r, g, b, illum, table_ptr))
});

pub static ACES: LazyLock<Arc<RGBColorSpaceData>> = LazyLock::new(|| {
    let illum = get_d65_illuminant_buffer();
    let r = Point2f::new(0.7347, 0.2653);
    let g = Point2f::new(0.0000, 1.0000);
    let b = Point2f::new(0.0001, -0.0770);

    let table_ptr = Ptr::from(&ACES_TABLE.clone());

    Arc::new(RGBColorSpaceData::new(r, g, b, illum, table_ptr))
});

#[derive(Debug, Clone)]
pub struct StandardColorSpaces {
    pub srgb: Arc<RGBColorSpaceData>,
    pub dci_p3: Arc<RGBColorSpaceData>,
    pub rec2020: Arc<RGBColorSpaceData>,
    pub aces2065_1: Arc<RGBColorSpaceData>,
}

pub fn get_colorspace_context() -> StandardColorSpaces {
    StandardColorSpaces {
        srgb: SRGB.clone().into(),
        dci_p3: DCI_P3.clone().into(),
        rec2020: REC2020.clone().into(),
        aces2065_1: ACES.clone().into(),
    }
}

pub fn get_colorspace_device() -> DeviceStandardColorSpaces {
    DeviceStandardColorSpaces {
        srgb: Ptr::from(&SRGB.view),
        dci_p3: Ptr::from(&DCI_P3.view),
        rec2020: Ptr::from(&REC2020.view),
        aces2065_1: Ptr::from(&ACES.view),
    }
}
