use std::collections::HashMap;
use std::hash::{BuildHasher, Hash};

use crate::dice::{CollectionBuilder, LengthRange};
use crate::prelude::*;

/// [`HashMap`] builder for [`dice::collection`].
///
/// [`dice::collection`]: dice::collection()
pub struct HashMapBuilder<S>
where
    S: BuildHasher + Clone,
{
    hasher: S,
}

impl<S> HashMapBuilder<S>
where
    S: BuildHasher + Clone,
{
    /// Creates a builder that uses the given [`BuildHasher`] for constructing a [`HashMap`].
    pub fn with_hasher(hasher: S) -> Self {
        HashMapBuilder { hasher }
    }
}

impl<K, V, S> CollectionBuilder<(K, V), HashMap<K, V, S>> for HashMapBuilder<S>
where
    K: Eq + Hash,
    S: BuildHasher + Clone,
{
    fn build(&self, elems: impl ExactSizeIterator<Item = (K, V)>) -> HashMap<K, V, S> {
        let hasher = self.hasher.clone();
        let mut map = HashMap::with_capacity_and_hasher(elems.len(), hasher);
        map.extend(elems);
        map
    }
}

/// Generates a [`HashMap`] that uses a default pseudorandom [`BuildHasher`] and contains keys of
/// type `K` with values of type `V`.
///
/// The range specifies the number of tries to generate key-value entries with distinct keys.
///
/// # Panics
///
/// Panics if the range is empty.
///
/// # Hasher
///
/// [`HashMap`] requires a [`BuildHasher`] which determines the order of elements.
/// The default is [`RandomState`] and it has no deterministic constructor.
///
/// If deterministic behavior is required, you can use [`Prng`] as [`BuildHasher`]. Otherwise,
/// you can construct [`RandomState`] yourself.
///
/// ```
/// use std::collections::hash_map::RandomState;
///
/// use dicetest::prelude::*;
/// use dicetest::{Prng, Limit};
///
/// let mut prng = Prng::from_seed(0x5EED.into());
/// let limit = Limit::default();
/// let mut fate = Fate::new(&mut prng, limit);
///
/// let elem_die = dice::zip().two(dice::u8(..), dice::char());
///
/// let hasher = fate.fork_prng();
/// let deterministic_map_die = dice::hash_map(hasher, &elem_die, ..);
///
/// let hasher = RandomState::new(); // Warning: not deterministic!
/// let non_deterministic_map_die = dice::hash_map(hasher, &elem_die, ..);
/// ```
///
/// [`Prng`]: crate::Prng
/// [`RandomState`]: std::collections::hash_map::RandomState
///
/// # Examples
///
/// ```
/// use dicetest::prelude::*;
/// use dicetest::{Prng, Limit};
///
/// let mut prng = Prng::from_seed(0x5EED.into());
/// let limit = Limit::default();
/// let mut fate = Fate::new(&mut prng, limit);
///
/// let elem_die = dice::zip().two(dice::u8(..), dice::char());
///
/// let hasher = fate.fork_prng();
/// let map = fate.with_limit(100.into()).roll(dice::hash_map(hasher, &elem_die, ..));
/// assert!(map.len() <= 100);
///
/// let hasher = fate.fork_prng();
/// let map = fate.roll(dice::hash_map(hasher, &elem_die, ..=73));
/// assert!(map.len() <= 73);
///
/// let hasher = fate.fork_prng();
/// let map = fate.roll(dice::hash_map(hasher, &elem_die, 17..));
/// assert!(map.len() >= 17);
///
/// let hasher = fate.fork_prng();
/// let map = fate.roll(dice::hash_map(hasher, &elem_die, 42));
/// assert!(map.len() <= 42);
/// ```
pub fn hash_map<K, V, S>(
    hasher: S,
    elem_die: impl Die<(K, V)>,
    tries_range: impl LengthRange,
) -> impl Die<HashMap<K, V, S>>
where
    K: Eq + Hash,
    S: BuildHasher + Clone,
{
    dice::collection(HashMapBuilder::with_hasher(hasher), elem_die, tries_range)
}

/// Similar to [`dice::hash_map`] but each element is generated using only a random part of
/// [`Limit`].
///
/// If you want to generate a [`HashMap`] that contains other collections, then you should
/// consider using this generator for the outer [`HashMap`]. That way the overall length is
/// bounded by [`Limit`] (and not the square of [`Limit`]).
///
/// [`Limit`]: crate::Limit
/// [`dice::hash_map`]: dice::hash_map()
///
/// # Panics
///
/// Panics if the range is empty.
///
/// # Examples
///
/// ```
/// use dicetest::prelude::*;
/// use dicetest::{Prng, Limit};
///
/// let mut prng = Prng::from_seed(0x5EED.into());
/// let limit = Limit::default();
/// let mut fate = Fate::new(&mut prng, limit);
///
/// let elem_die = dice::char();
/// let vec_die = dice::zip().two(dice::u8(..), dice::vec(elem_die, ..));
/// let hasher = fate.fork_prng();
/// let map_of_vecs_die = dice::outer_hash_map(hasher, vec_die, ..);
///
/// let map_of_vecs = fate.roll(map_of_vecs_die);
/// assert!(map_of_vecs.values().flatten().count() <= 100);
/// ```
pub fn outer_hash_map<K, V, S>(
    hasher: S,
    elem_die: impl Die<(K, V)>,
    tries_range: impl LengthRange,
) -> impl Die<HashMap<K, V, S>>
where
    K: Eq + Hash,
    S: BuildHasher + Clone,
{
    dice::outer_collection(HashMapBuilder::with_hasher(hasher), elem_die, tries_range)
}
