use rand::{
  rngs::{SmallRng, StdRng},
  Rng, SeedableRng,
};

/// URL safe symbols.
///
/// An array of characters which can be safely used in urls.
/// Alphabet default for Rand. Is alphabet by default for Rand
///
/// # Example
///
/// ```
/// let id = Rand::Rand!(10, &Rand::alphabet::NID_SAFE);
/// ```
pub const NID_SAFE: [char; 64] = [
  '_', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
  'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
];

///
#[derive(Debug)]
pub enum Rand {
  ///Safe
  Safe,
  ///
  UnSafe,
}
impl Rand {
  ///
  pub fn random_type<T>(&self) -> T
  where
    rand::distributions::Standard: rand::prelude::Distribution<T>,
  {
    match &self {
      Rand::Safe => StdRng::from_entropy().gen::<T>(),
      Rand::UnSafe => SmallRng::from_entropy().gen::<T>(),
    }
  }
  ///
  pub fn random_bool(&self) -> bool {
    match &self {
      Rand::Safe => StdRng::from_entropy().gen_range(1u8..=2) == 1,
      Rand::UnSafe => SmallRng::from_entropy().gen_range(1u8..=2) == 1,
    }
  }
  ///
  pub fn rgb_range(&self, min: u8, max: u8) -> (u8, u8, u8) {
    match &self {
      Rand::Safe => {
        let mut rng = StdRng::from_entropy();
        (
          rng.gen_range(min..max),
          rng.gen_range(min..max),
          rng.gen_range(min..max),
        )
      }
      Rand::UnSafe => {
        let mut rng = SmallRng::from_entropy();
        (
          rng.gen_range(min..max),
          rng.gen_range(min..max),
          rng.gen_range(min..max),
        )
      }
    }
  }
  ///
  pub fn random_range<T, R>(&self, range: R) -> T
  where
    T: rand::distributions::uniform::SampleUniform,
    R: rand::distributions::uniform::SampleRange<T>,
  {
    match &self {
      Rand::Safe => StdRng::from_entropy().gen_range(range),
      Rand::UnSafe => SmallRng::from_entropy().gen_range(range),
    }
  }
  ///
  pub fn random_rng(&self, step: usize) -> Vec<u8> {
    match &self {
      Rand::Safe => {
        let mut rng = StdRng::from_entropy();
        let mut result = vec![0u8; step];
        rng.fill(&mut result[..]);
        result
      }
      Rand::UnSafe => {
        let mut rng = SmallRng::from_entropy();
        let mut result = vec![0u8; step];
        rng.fill(&mut result[..]);
        result
      }
    }
  }
  /// Nanoid
  pub fn nanoid_format(&self, alphabet: &[char], size: usize) -> String {
    assert!(
      alphabet.len() <= u8::max_value() as usize,
      "The alphabet cannot be longer than a `u8` (to comply with the `random` function)"
    );
    let mask = alphabet.len().next_power_of_two() - 1;
    let step: usize = 8 * size / 5;
    // Assert that the masking does not truncate the alphabet. (See #9)
    debug_assert!(alphabet.len() <= mask + 1);
    let mut id = String::with_capacity(size);
    loop {
      let bytes = self.random_rng(step);
      for &byte in &bytes {
        let byte = byte as usize & mask;

        if alphabet.len() > byte {
          id.push(alphabet[byte]);

          if id.len() == size {
            return id;
          }
        }
      }
    }
  }
}
