use sciforge::hub::domain::common::constants::R_GAS;

pub struct ErosionParams {
    pub windspeedms: f64,
    pub atmosphericdensity: f64,
    pub particlediameterm: f64,
    pub particledensity: f64,
}

impl ErosionParams {
    pub fn winderosionratemmyr(&self) -> f64 {
        let threshold = winderosionthresholdvelocity(self.particlediameterm, self.particledensity);
        if self.windspeedms > threshold {
            let excess = self.windspeedms - threshold;
            0.01 * excess * excess * self.atmosphericdensity / self.particledensity
        } else {
            0.0
        }
    }

    pub fn sedimentyieldtkm2yr(&self) -> f64 {
        self.winderosionratemmyr() * self.particledensity / 1000.0
    }
}

pub fn chemicalweatheringrate(temperaturek: f64, mediumdensity: f64) -> f64 {
    let ea = 40000.0;
    let a = 1e4;
    a * (-ea / (R_GAS * temperaturek)).exp() * mediumdensity.powf(0.65)
}

pub fn thermalstresserosion(temprangek: f64, thermalexpansion: f64) -> f64 {
    1e-6 * temprangek * thermalexpansion
}

pub fn winderosionthresholdvelocity(particlediameterm: f64, particledensity: f64) -> f64 {
    let airdensity = *crate::ONEBARDENSITY;
    let g = *crate::SURFACEGRAVITY;
    0.1 * ((particledensity - airdensity) * g * particlediameterm / airdensity).sqrt()
}

pub fn sublimationerosionrate(temperaturek: f64, vaporpressurepa: f64) -> f64 {
    let factor = (-3000.0 / temperaturek).exp();
    1e-8 * vaporpressurepa * factor
}
