use crate::Quota;
use crate::refill_rate::RefillRate;

#[derive(Clone, Copy, Debug)]
pub struct QuotaPolicy {
    capacity: f64,
    refill_rate_ns: f64,
    refill_interval_ns: u64,
}

impl QuotaPolicy {
    #[inline]
    pub fn new() -> QuotaPolicy {
        QuotaPolicy {
            capacity: 0.0,
            refill_rate_ns: 0.0,
            refill_interval_ns: 0,
        }
    }

    #[inline]
    pub fn set_capacity(self, capacity: f64) -> Self {
        Self {
            capacity,
            ..self
        }
    }

    #[inline]
    pub fn set_refill_rate(self, refill_rate: RefillRate) -> Self {
        Self {
            refill_rate_ns: refill_rate.0,
            ..self
        }
    }

    #[inline]
    pub fn set_refill_interval(self, interval: std::time::Duration) -> Self {
        Self {
            refill_interval_ns: interval.as_nanos() as u64,
            ..self
        }
    }

    #[inline]
    pub fn tick(&self, dt_ns: u64, quota: &Quota) {
        let refill_amount = self.refill_rate_ns * dt_ns as f64;
        quota.fill(refill_amount, self.capacity);
    }
}

#[cfg(test)]
mod tests;