use super::Evolver;

impl Evolver {
    /// Changes evolution parameter.
    pub fn par(&mut self, param: &str, valstr: &str) -> Result<(), String> {
        match param {
            "batches" => {
                match valstr.parse::<usize>() {
                    Ok(batches) => {
                        if batches > 0 {
                            self.parameters.batches = batches;
                            Ok(())
                        } else {
                            Err(String::from("Number of batches must be positive"))
                        }
                    },
                    Err(e) => Err(format!("Cannot parse value: {}", e.to_string()))
                }
            },

            "replace_ratio" => {
                match valstr.parse::<f64>() {
                    Ok(replace_ratio) => {
                        if (replace_ratio > 0.0) && (replace_ratio < 1.0) {
                            self.parameters.replace_ratio = replace_ratio;
                            Ok(())
                        } else {
                            Err(String::from("Replace ratio must be in (0; 1)"))
                        }
                    },
                    Err(e) => Err(format!("Cannot parse value: {}", e.to_string()))
                }
            },

            "par_ratio" => {
                match valstr.parse::<f64>() {
                    Ok(par_ratio) => {
                        if (par_ratio > 0.0) && (par_ratio <= 1.0) {
                            self.parameters.par_ratio = par_ratio;
                            Ok(())
                        } else {
                            Err(String::from("Parents ratio must be in (0; 1]"))
                        }
                    },
                    Err(e) => Err(format!("Cannot parse value: {}", e.to_string()))
                }
            },

            "parents" => {
                match valstr.parse::<usize>() {
                    Ok(parents) => {
                        if parents > 0 {
                            self.parameters.parents = parents;
                            Ok(())
                        } else {
                            Err(String::from("Number of parents must be positive"))
                        }
                    },
                    Err(e) => Err(format!("Cannot parse value: {}", e.to_string()))
                }
            },

            "par_sw_prob" => {
                match valstr.parse::<f64>() {
                    Ok(par_sw_prob) => {
                        if (par_sw_prob >= 0.0) && (par_sw_prob <= 1.0) {
                            self.parameters.par_sw_prob = par_sw_prob;
                            Ok(())
                        } else {
                            Err(String::from("Parent switch probability must be in [0; 1]"))
                        }
                    },
                    Err(e) => Err(format!("Cannot parse value: {}", e.to_string()))
                }
            },

            "mutat_prob" => {
                match valstr.parse::<f64>() {
                    Ok(mutat_prob) => {
                        if (mutat_prob >= 0.0) && (mutat_prob <= 1.0) {
                            self.parameters.mutat_prob = mutat_prob;
                            Ok(())
                        } else {
                            Err(String::from("Mutation probability must be in [0; 1]"))
                        }
                    },
                    Err(e) => Err(format!("Cannot parse value: {}", e.to_string()))
                }
            },

            _ => {
                Err(String::from("Unknown parameter"))
            }
        }
    }

}
