use crate::core::image::Image;
use crate::core::material::CreateMaterial;
use crate::spectra::get_colorspace_context;
use crate::utils::{Arena, FileLoc, TextureParameterDictionary, Upload};
use shared::bxdfs::HairBxDF;
use shared::core::bsdf::BSDF;
use shared::core::bssrdf::BSSRDF;
use shared::core::material::{Material, MaterialEvalContext, MaterialTrait};
use shared::core::spectrum::Spectrum;
use shared::core::texture::{GPUFloatTexture, SpectrumType, TextureEvaluator};
use shared::materials::complex::*;
use shared::spectra::RGBUnboundedSpectrum;
use shared::spectra::SampledWavelengths;
use shared::textures::SpectrumConstantTexture;
use shared::utils::Ptr;
use std::collections::HashMap;
use std::fmt::Error;
use std::sync::Arc;

impl CreateMaterial for HairMaterial {
    fn create(
        parameters: &TextureParameterDictionary,
        normal_map: Option<Arc<Image>>,
        _named_materials: &HashMap<String, Material>,
        loc: &FileLoc,
        arena: &mut Arena,
    ) -> Result<Material, Error> {
        let sigma_a = parameters.get_spectrum_texture_or_null("sigma_a", SpectrumType::Unbounded);
        let reflectance = parameters
            .get_spectrum_texture_or_null("reflectance", SpectrumType::Albedo)
            .or_else(|| parameters.get_spectrum_texture_or_null("color", SpectrumType::Albedo));
        let eumelanin = parameters.get_float_texture_or_null("eumelanin");
        let pheomelanin = parameters.get_float_texture_or_null("pheomelanin");
        let has_melanin = eumelanin.is_some() || pheomelanin.is_some();

        // Default distribution if nothing is spceified
        let sigma_a = if sigma_a.is_none() && !reflectance.is_none() && !has_melanin {
            let stdcs = get_colorspace_context();
            let default_rgb = HairBxDF::sigma_a_from_concentration(1.3, 0.0, stdcs);
            Some(Arc::new(SpectrumConstantTexture::new(
                Spectrum::RGBUnbounded(RGBUnboundedSpectrum::new(
                    reflectance.to_rgb(),
                    default_rgb,
                )),
            )))
        } else {
            sigma_a
        };

        let eta = parameters.get_flot_texture("eta", 1.55);
        let beta_m = parameters.get_float_texture("beta_m", 0.3);
        let beta_n = parameters.get_float_texture("beta_n", 0.3);
        let alpha = parameters.get_float_texture("alpha", 2.);
        let material = HairMaterial::new(
            sigma_a.upload(arena),
            reflectance.upload(arena),
            eumelanin.upload(arena),
            pheomelanin.upload(arena),
            eta.upload(arena),
            beta_m.upload(arena),
            beta_n.upload(arena),
            alpha.upload(arena),
        );

        Ok(Material::Hair(material))
    }
}

impl MaterialTrait for MeasuredMaterial {
    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 {
        todo!()
    }
    fn get_normal_map(&self) -> *const Image {
        todo!()
    }
    fn get_displacement(&self) -> Ptr<GPUFloatTexture> {
        todo!()
    }
    fn has_subsurface_scattering(&self) -> bool {
        todo!()
    }
}

impl CreateMaterial for SubsurfaceMaterial {
    fn create(
        _parameters: &TextureParameterDictionary,
        _normal_map: Option<Arc<Image>>,
        _named_materials: &HashMap<String, Material>,
        _loc: &FileLoc,
        _arena: &mut Arena,
    ) -> Result<Material, Error> {
        todo!()
    }
}
