use std::sync::Arc;

use crate::Pid;
use crate::jiffy::Jiffies;
use crate::random::Randomizer;
use crate::topology::Topology;

pub(crate) struct Latency {
    topology: Arc<Topology>,
    randomizer: Randomizer,
}

impl Latency {
    pub(crate) fn new(randomizer: Randomizer, topology: Arc<Topology>) -> Self {
        Self {
            randomizer,
            topology,
        }
    }

    pub(crate) fn random_latency(&mut self, source: Pid, target: Pid) -> Jiffies {
        let distribution = self.topology.get_distribution(source, target);
        Jiffies(self.randomizer.random_usize(distribution))
    }
}
