extern crate rand;

use rand::prelude::*;

use crate::base;

use super::Evolver;

impl Evolver {
    /// Runs given boolnet with given hex-string input to obtain output formatted as hex-string.
    pub fn run(&mut self, bn_ind: usize, inp_hex: &str) -> Result<String, String> {
        if bn_ind >= self.population.len() {
            return Err(format!("Boolnet index is larger than {}", self.population.len() - 1));
        }

        let inp_bytes = base::hex2bytes(inp_hex)?;

        if (inp_bytes.len() << 3) != self.bitran.inp_size() {
            return Err(format!("Input size in bits {} is wrong for this bitransform, must be {}", inp_bytes.len() << 3, self.bitran.inp_size()));
        }

        let mut outp_bytes = vec![0u8; self.bitran.outp_size() >> 3];

        let mut rng = thread_rng();

        self.population[bn_ind].run_single(&mut rng, &inp_bytes, &mut outp_bytes);

        base::bytes2hex(&outp_bytes)
    }

}
