use core::fmt::{self, Debug};

use Entry::*;

use crate::collections::btree_map as map;
use crate::collections::btree_map::{DefaultTuning, Tuning};

/// A view into a single entry in a set, which may either be vacant or occupied.
///
/// This `enum` is constructed from the [`entry`] method on [`BTreeSet`].
///
/// [`BTreeSet`]: super::BTreeSet
/// [`entry`]: super::BTreeSet::entry
///
/// # Examples
///
/// ```
///
/// use pstd::collections::btree_set::BTreeSet;
///
/// let mut set = BTreeSet::new();
/// set.extend(["a", "b", "c"]);
/// assert_eq!(set.len(), 3);
///
/// // Existing value (insert)
/// let entry = set.entry("a");
/// let _raw_o = entry.insert();
/// assert_eq!(set.len(), 3);
/// // Nonexistent value (insert)
/// set.entry("d").insert();
///
/// // Existing value (or_insert)
/// set.entry("b").or_insert();
/// // Nonexistent value (or_insert)
/// set.entry("e").or_insert();
///
/// println!("Our BTreeSet: {:?}", set);
/// assert!(set.iter().eq(&["a", "b", "c", "d", "e"]));
/// ```
pub enum Entry<'a, T, A: Tuning = DefaultTuning> {
    /// An occupied entry.
    ///
    /// # Examples
    ///
    /// ```
    ///
    /// use pstd::collections::btree_set::{Entry, BTreeSet};
    ///
    /// let mut set = BTreeSet::from(["a", "b"]);
    ///
    /// match set.entry("a") {
    ///     Entry::Vacant(_) => unreachable!(),
    ///     Entry::Occupied(_) => { }
    /// }
    /// ```
    Occupied(OccupiedEntry<'a, T, A>),

    /// A vacant entry.
    ///
    /// # Examples
    ///
    /// ```
    ///
    /// use pstd::collections::btree_set::{Entry, BTreeSet};
    ///
    /// let mut set = BTreeSet::new();
    ///
    /// match set.entry("a") {
    ///     Entry::Occupied(_) => unreachable!(),
    ///     Entry::Vacant(_) => { }
    /// }
    /// ```
    Vacant(VacantEntry<'a, T, A>),
}

impl<T: Debug + Ord, A: Tuning> Debug for Entry<'_, T, A> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Vacant(ref v) => f.debug_tuple("Entry").field(v).finish(),
            Occupied(ref o) => f.debug_tuple("Entry").field(o).finish(),
        }
    }
}

/// A view into an occupied entry in a `BTreeSet`.
/// It is part of the [`Entry`] enum.
///
/// # Examples
///
/// ```
/// use pstd::collections::btree_set::{Entry, BTreeSet};
///
/// let mut set = BTreeSet::new();
/// set.extend(["a", "b", "c"]);
///
/// let _entry_o = set.entry("a").insert();
/// assert_eq!(set.len(), 3);
///
/// // Existing key
/// match set.entry("a") {
///     Entry::Vacant(_) => unreachable!(),
///     Entry::Occupied(view) => {
///         assert_eq!(view.get(), &"a");
///     }
/// }
///
/// assert_eq!(set.len(), 3);
///
/// // Existing key (take)
/// match set.entry("c") {
///     Entry::Vacant(_) => unreachable!(),
///     Entry::Occupied(view) => {
///         assert_eq!(view.remove(), "c");
///     }
/// }
/// assert_eq!(set.get(&"c"), None);
/// assert_eq!(set.len(), 2);
/// ```
pub struct OccupiedEntry<'a, T, A: Tuning = DefaultTuning> {
    pub(super) inner: map::OccupiedEntry<'a, T, (), A>,
}

impl<T: Debug + Ord, A: Tuning> Debug for OccupiedEntry<'_, T, A> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("OccupiedEntry")
            .field("value", self.get())
            .finish()
    }
}

/// A view into a vacant entry in a `BTreeSet`.
/// It is part of the [`Entry`] enum.
///
/// # Examples
///
/// ```
/// use pstd::collections::btree_set::{Entry, BTreeSet};
///
/// let mut set = BTreeSet::<&str>::new();
///
/// let entry_v = match set.entry("a") {
///     Entry::Vacant(view) => view,
///     Entry::Occupied(_) => unreachable!(),
/// };
/// entry_v.insert();
/// assert!(set.contains("a") && set.len() == 1);
///
/// // Nonexistent key (insert)
/// match set.entry("b") {
///     Entry::Vacant(view) => view.insert(),
///     Entry::Occupied(_) => unreachable!(),
/// }
/// assert!(set.contains("b") && set.len() == 2);
/// ```
pub struct VacantEntry<'a, T, A: Tuning = DefaultTuning> {
    pub(super) inner: map::VacantEntry<'a, T, (), A>,
}

impl<T: Debug + Ord, A: Tuning> Debug for VacantEntry<'_, T, A> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("VacantEntry").field(self.get()).finish()
    }
}

impl<'a, T: Ord, A: Tuning> Entry<'a, T, A> {
    /// Sets the value of the entry, and returns an `OccupiedEntry`.
    ///
    /// # Examples
    ///
    /// ```
    ///
    /// use pstd::collections::BTreeSet;
    ///
    /// let mut set = BTreeSet::new();
    /// let entry = set.entry("horseyland").insert();
    ///
    /// assert_eq!(entry.get(), &"horseyland");
    /// ```
    #[inline]
    pub fn insert(self) -> OccupiedEntry<'a, T, A> {
        match self {
            Occupied(entry) => entry,
            Vacant(entry) => entry.insert_entry(),
        }
    }

    /// Ensures a value is in the entry by inserting if it was vacant.
    ///
    /// # Examples
    ///
    /// ```
    ///
    /// use pstd::collections::BTreeSet;
    ///
    /// let mut set = BTreeSet::new();
    ///
    /// // nonexistent key
    /// set.entry("poneyland").or_insert();
    /// assert!(set.contains("poneyland"));
    ///
    /// // existing key
    /// set.entry("poneyland").or_insert();
    /// assert!(set.contains("poneyland"));
    /// assert_eq!(set.len(), 1);
    /// ```
    #[inline]
    pub fn or_insert(self) {
        if let Vacant(entry) = self {
            entry.insert();
        }
    }

    /// Returns a reference to this entry's value.
    ///
    /// # Examples
    ///
    /// ```
    ///
    /// use pstd::collections::BTreeSet;
    ///
    /// let mut set = BTreeSet::new();
    /// set.entry("poneyland").or_insert();
    ///
    /// // existing key
    /// assert_eq!(set.entry("poneyland").get(), &"poneyland");
    /// // nonexistent key
    /// assert_eq!(set.entry("horseland").get(), &"horseland");
    /// ```
    #[inline]
    pub fn get(&self) -> &T {
        match *self {
            Occupied(ref entry) => entry.get(),
            Vacant(ref entry) => entry.get(),
        }
    }
}

impl<'a, T: Ord, A: Tuning> OccupiedEntry<'a, T, A> {
    /// Gets a reference to the value in the entry.
    ///
    /// # Examples
    ///
    /// ```
    ///
    /// use pstd::collections::btree_set::{Entry, BTreeSet};
    ///
    /// let mut set = BTreeSet::new();
    /// set.entry("poneyland").or_insert();
    ///
    /// match set.entry("poneyland") {
    ///     Entry::Vacant(_) => panic!(),
    ///     Entry::Occupied(entry) => assert_eq!(entry.get(), &"poneyland"),
    /// }
    /// ```
    #[inline]
    pub fn get(&self) -> &T {
        self.inner.key()
    }

    /// Takes the value out of the entry, and returns it.
    ///
    /// # Examples
    ///
    /// ```
    ///
    /// use pstd::collections::BTreeSet;
    /// use pstd::collections::btree_set::Entry;
    ///
    /// let mut set = BTreeSet::new();
    /// set.entry("poneyland").or_insert();
    ///
    /// if let Entry::Occupied(o) = set.entry("poneyland") {
    ///     assert_eq!(o.remove(), "poneyland");
    /// }
    ///
    /// assert_eq!(set.contains("poneyland"), false);
    /// ```
    #[inline]
    pub fn remove(self) -> T {
        self.inner.remove_entry().0
    }
}

impl<'a, T: Ord, A: Tuning> VacantEntry<'a, T, A> {
    /// Gets a reference to the value that would be used when inserting
    /// through the `VacantEntry`.
    ///
    /// # Examples
    ///
    /// ```
    ///
    /// use pstd::collections::BTreeSet;
    ///
    /// let mut set = BTreeSet::new();
    /// assert_eq!(set.entry("poneyland").get(), &"poneyland");
    /// ```
    #[inline]
    pub fn get(&self) -> &T {
        self.inner.key()
    }

    /// Take ownership of the value.
    ///
    /// # Examples
    ///
    /// ```
    ///
    /// use pstd::collections::btree_set::{Entry, BTreeSet};
    ///
    /// let mut set = BTreeSet::new();
    ///
    /// match set.entry("poneyland") {
    ///     Entry::Occupied(_) => panic!(),
    ///     Entry::Vacant(v) => assert_eq!(v.into_value(), "poneyland"),
    /// }
    /// ```
    #[inline]
    pub fn into_value(self) -> T {
        self.inner.into_key()
    }

    /// Sets the value of the entry with the VacantEntry's value.
    ///
    /// # Examples
    ///
    /// ```
    ///
    /// use pstd::collections::BTreeSet;
    /// use pstd::collections::btree_set::Entry;
    ///
    /// let mut set = BTreeSet::new();
    ///
    /// if let Entry::Vacant(o) = set.entry("poneyland") {
    ///     o.insert();
    /// }
    /// assert!(set.contains("poneyland"));
    /// ```
    #[inline]
    pub fn insert(self) {
        self.inner.insert(());
    }

    #[inline]
    fn insert_entry(self) -> OccupiedEntry<'a, T, A> {
        OccupiedEntry {
            inner: self.inner.insert_entry(()),
        }
    }
}
