use rand::{SeedableRng, rngs::StdRng};
use serde::{Deserialize, Serialize};

use crate::changed_state;

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct PlaylistRng {
    initial_seed: u64,
    current_cycle: u64,
}

impl PlaylistRng {
    pub fn new() -> Self {
        Self {
            initial_seed: rand::random::<u64>(),
            current_cycle: 0,
        }
    }

    pub fn current_cycle(&self) -> u64 {
        self.current_cycle
    }

    pub fn set_cycle(&mut self, cycle: u64) {
        self.current_cycle = cycle;
    }

    pub fn current_rng(&self) -> StdRng {
        StdRng::seed_from_u64(self.initial_seed + self.current_cycle)
    }

    pub fn increment(&mut self) -> StdRng {
        self.current_cycle += 1;
        changed_state();
        self.current_rng()
    }

    pub fn find_cycle_rng(&self, cycle: u64) -> StdRng {
        StdRng::seed_from_u64(self.initial_seed + cycle)
    }
}
