use std::{
    sync::atomic::{AtomicU64, Ordering},
    time::{Duration, SystemTime},
};

use crate::utils::time_utils::time_to_millis;

#[derive(Debug)]
pub(crate) struct ActivityTimeCell {
    millis_ts: AtomicU64,
}

impl ActivityTimeCell {
    pub fn get(&self) -> SystemTime {
        return SystemTime::UNIX_EPOCH + Duration::from_millis(self.millis_ts.load(Ordering::Relaxed));
    }

    pub fn set(&self, time: SystemTime) {
        self.millis_ts.store(time_to_millis(time), Ordering::Relaxed);
    }
}

impl From<SystemTime> for ActivityTimeCell {
    fn from(time: SystemTime) -> Self {
        Self {
            millis_ts: AtomicU64::new(time_to_millis(time)),
        }
    }
}

#[cfg(test)]
impl PartialEq for ActivityTimeCell {
    fn eq(&self, other: &Self) -> bool {
        return self.millis_ts.load(Ordering::Relaxed) == other.millis_ts.load(Ordering::Relaxed);
    }
}
