use sciforge::hub::prelude::constants::elements::{atomic_mass, electronegativity};

#[derive(Debug, Clone)]
pub struct PbrMaterial {
    pub albedo: [f32; 4],
    pub roughness: f32,
    pub metallic: f32,
    pub normalstrength: f32,
    pub emissive: [f32; 3],
}

fn metallicfromiron(fewtfrac: f64) -> f32 {
    let mfe = atomic_mass(26);
    let mo = atomic_mass(8);
    let fefractioninfeo = mfe / (mfe + mo);
    let purefe = fewtfrac * fefractioninfeo;
    let en = electronegativity(26).unwrap_or(1.83);
    let metallicscale = (1.0 - en / 4.0).max(0.0);
    (purefe * metallicscale).clamp(0.0, 1.0) as f32
}

impl PbrMaterial {
    pub fn methanecloud() -> Self {
        Self {
            albedo: [0.65, 0.72, 0.82, 0.7],
            roughness: 0.2,
            metallic: 0.0,
            normalstrength: 0.3,
            emissive: [0.0; 3],
        }
    }

    pub fn h2scloud() -> Self {
        Self {
            albedo: [0.45, 0.50, 0.55, 0.8],
            roughness: 0.4,
            metallic: 0.0,
            normalstrength: 0.5,
            emissive: [0.0; 3],
        }
    }

    pub fn deepatmosphere() -> Self {
        Self {
            albedo: [0.02, 0.05, 0.15, 1.0],
            roughness: 0.1,
            metallic: 0.01,
            normalstrength: 0.2,
            emissive: [0.0; 3],
        }
    }

    pub fn hazelayer() -> Self {
        Self {
            albedo: [0.55, 0.60, 0.75, 0.3],
            roughness: 0.1,
            metallic: 0.0,
            normalstrength: 0.1,
            emissive: [0.0; 3],
        }
    }

    pub fn corerock() -> Self {
        Self {
            albedo: [0.25, 0.20, 0.15, 1.0],
            roughness: 0.95,
            metallic: metallicfromiron(0.15),
            normalstrength: 1.0,
            emissive: [0.0; 3],
        }
    }

    pub fn superionicice() -> Self {
        Self {
            albedo: [0.40, 0.55, 0.70, 0.9],
            roughness: 0.05,
            metallic: 0.3,
            normalstrength: 0.4,
            emissive: [0.01, 0.02, 0.05],
        }
    }

    pub fn stormregion() -> Self {
        Self {
            albedo: [0.10, 0.12, 0.20, 1.0],
            roughness: 0.3,
            metallic: 0.0,
            normalstrength: 0.8,
            emissive: [0.0; 3],
        }
    }
}
