#[derive(Debug, Clone)]
pub enum UniformValue {
    Float(f32),
    Vec3([f32; 3]),
    Vec4([f32; 4]),
    Mat4([f32; 16]),
    Int(i32),
}

#[derive(Debug, Clone)]
pub struct ShaderUniform {
    pub name: &'static str,
    pub value: UniformValue,
}

pub struct ShaderEndpoint {
    pub name: &'static str,
    pub uniforms: Vec<ShaderUniform>,
}

impl ShaderEndpoint {
    pub fn surface() -> Self {
        Self {
            name: "mercury_surface_pbr",
            uniforms: vec![
                ShaderUniform {
                    name: "u_sun_direction",
                    value: UniformValue::Vec3([0.0, 1.0, 0.0]),
                },
                ShaderUniform {
                    name: "u_camera_pos",
                    value: UniformValue::Vec3([0.0, 0.0, 0.0]),
                },
                ShaderUniform {
                    name: "u_planet_radius",
                    value: UniformValue::Float(2_439_700.0),
                },
                ShaderUniform {
                    name: "u_solar_flux",
                    value: UniformValue::Float(crate::SOLAR_CONSTANT_MEAN as f32),
                },
                ShaderUniform {
                    name: "u_surface_temp_k",
                    value: UniformValue::Float(crate::MEAN_SURFACE_TEMP_K as f32),
                },
                ShaderUniform {
                    name: "u_thermal_overlay",
                    value: UniformValue::Int(1),
                },
                ShaderUniform {
                    name: "u_normal_mapping",
                    value: UniformValue::Int(1),
                },
            ],
        }
    }

    pub fn exosphere() -> Self {
        Self {
            name: "mercury_exosphere_glow",
            uniforms: vec![
                ShaderUniform {
                    name: "u_sodium_brightness",
                    value: UniformValue::Float(0.0),
                },
                ShaderUniform {
                    name: "u_sodium_color",
                    value: UniformValue::Vec3([1.0, 0.82, 0.0]),
                },
                ShaderUniform {
                    name: "u_sun_distance_au",
                    value: UniformValue::Float(0.387),
                },
                ShaderUniform {
                    name: "u_phase_angle_rad",
                    value: UniformValue::Float(0.0),
                },
            ],
        }
    }

    pub fn nightside() -> Self {
        Self {
            name: "mercury_nightside_thermal",
            uniforms: vec![
                ShaderUniform {
                    name: "u_surface_temp_k",
                    value: UniformValue::Float(crate::NIGHTSIDE_TEMP_K as f32),
                },
                ShaderUniform {
                    name: "u_thermal_emissivity",
                    value: UniformValue::Float(crate::SURFACE_EMISSIVITY as f32),
                },
                ShaderUniform {
                    name: "u_thermal_color",
                    value: UniformValue::Vec3([0.6, 0.15, 0.02]),
                },
            ],
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShaderPass {
    Depth,
    Color,
    Shadow,
    Thermal,
}

pub struct ShaderConfig {
    pub id: &'static str,
    pub has_atmosphere: bool,
    pub has_ocean: bool,
    pub has_clouds: bool,
    pub thermal_overlay: bool,
    pub normal_mapping: bool,
}

impl ShaderConfig {
    pub fn mercury_surface() -> Self {
        Self {
            id: "mercury_surface",
            has_atmosphere: false,
            has_ocean: false,
            has_clouds: false,
            thermal_overlay: true,
            normal_mapping: true,
        }
    }

    pub fn mercury_nightside() -> Self {
        Self {
            id: "mercury_nightside",
            has_atmosphere: false,
            has_ocean: false,
            has_clouds: false,
            thermal_overlay: true,
            normal_mapping: false,
        }
    }

    pub fn active_features(&self) -> Vec<&'static str> {
        let mut f = vec!["pbr_base"];
        if self.normal_mapping {
            f.push("normal_map");
        }
        if self.thermal_overlay {
            f.push("thermal_ir");
        }
        f
    }
}

pub fn shader_id() -> &'static str {
    "mercury_default"
}
