use crate::Float;
use crate::bxdfs::{
    CoatedConductorBxDF, CoatedDiffuseBxDF, ConductorBxDF, DielectricBxDF, DiffuseBxDF, HairBxDF,
};
use crate::core::bsdf::BSDF;
use crate::core::bssrdf::BSSRDF;
use crate::core::bxdf::BxDF;
use crate::core::image::Image;
use crate::core::material::{Material, MaterialEvalContext, MaterialTrait};
use crate::core::scattering::TrowbridgeReitzDistribution;
use crate::core::spectrum::{Spectrum, SpectrumTrait};
use crate::core::texture::{GPUFloatTexture, GPUSpectrumTexture, TextureEvaluator};
use crate::spectra::{SampledSpectrum, SampledWavelengths};
use crate::utils::Ptr;
use crate::utils::math::clamp;

#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct DiffuseMaterial {
    pub normal_map: Ptr<Image>,
    pub displacement: Ptr<GPUFloatTexture>,
    pub reflectance: Ptr<GPUSpectrumTexture>,
}

impl MaterialTrait for DiffuseMaterial {
    fn get_bsdf<T: TextureEvaluator>(
        &self,
        tex_eval: &T,
        ctx: &MaterialEvalContext,
        lambda: &SampledWavelengths,
    ) -> BSDF {
        let r = tex_eval.evaluate_spectrum(&self.reflectance, ctx, lambda);
        let bxdf = BxDF::Diffuse(DiffuseBxDF::new(r));
        BSDF::new(ctx.ns, ctx.dpdus, Ptr::from(&bxdf))
    }

    fn get_bssrdf<T>(
        &self,
        _tex_eval: &T,
        _ctx: &MaterialEvalContext,
        _lambda: &SampledWavelengths,
    ) -> Option<BSSRDF> {
        todo!()
    }

    fn can_evaluate_textures(&self, tex_eval: &dyn TextureEvaluator) -> bool {
        tex_eval.can_evaluate(&[], &[self.reflectance])
    }

    fn get_normal_map(&self) -> Option<&Image> {
        Some(&*self.normal_map)
    }

    fn get_displacement(&self) -> Ptr<GPUFloatTexture> {
        self.displacement
    }

    fn has_subsurface_scattering(&self) -> bool {
        false
    }
}

#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct DiffuseTransmissionMaterial {
    pub image: Ptr<Image>,
    pub displacement: Ptr<GPUFloatTexture>,
    pub reflectance: Ptr<GPUFloatTexture>,
    pub transmittance: Ptr<GPUFloatTexture>,
    pub scale: Float,
}

impl MaterialTrait for DiffuseTransmissionMaterial {
    fn get_bsdf<T: TextureEvaluator>(
        &self,
        _tex_eval: &T,
        _ctx: &MaterialEvalContext,
        _lambda: &SampledWavelengths,
    ) -> BSDF {
        todo!()
    }
    fn get_bssrdf<T>(
        &self,
        _tex_eval: &T,
        _ctx: &MaterialEvalContext,
        _lambda: &SampledWavelengths,
    ) -> Option<BSSRDF> {
        todo!()
    }

    fn can_evaluate_textures(&self, tex_eval: &dyn TextureEvaluator) -> bool {
        tex_eval.can_evaluate(&[self.reflectance, self.transmittance], &[])
    }

    fn get_normal_map(&self) -> Option<&Image> {
        Some(&*self.image)
    }

    fn get_displacement(&self) -> Ptr<GPUFloatTexture> {
        self.displacement
    }

    fn has_subsurface_scattering(&self) -> bool {
        false
    }
}
