use super::{Bucket, Entries, IndexSet, IntoIter, Iter};
use crate::util::try_simplify_range;
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::ops::{self, Bound, Index, RangeBounds};
#[repr(transparent)]
pub struct Slice<T> {
    pub(crate) entries: [Bucket<T>],
}
#[allow(unsafe_code)]
impl<T> Slice<T> {
    pub(super) fn from_slice(entries: &[Bucket<T>]) -> &Self {
        unsafe { &*(entries as *const [Bucket<T>] as *const Self) }
    }
    pub(super) fn from_boxed(entries: Box<[Bucket<T>]>) -> Box<Self> {
        unsafe { Box::from_raw(Box::into_raw(entries) as *mut Self) }
    }
    fn into_boxed(self: Box<Self>) -> Box<[Bucket<T>]> {
        unsafe { Box::from_raw(Box::into_raw(self) as *mut [Bucket<T>]) }
    }
}
impl<T> Slice<T> {
    pub(crate) fn into_entries(self: Box<Self>) -> Vec<Bucket<T>> {
        self.into_boxed().into_vec()
    }
    pub fn len(&self) -> usize {
        self.entries.len()
    }
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
    pub fn get_index(&self, index: usize) -> Option<&T> {
        self.entries.get(index).map(Bucket::key_ref)
    }
    pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Self> {
        let range = try_simplify_range(range, self.entries.len())?;
        self.entries.get(range).map(Self::from_slice)
    }
    pub fn first(&self) -> Option<&T> {
        self.entries.first().map(Bucket::key_ref)
    }
    pub fn last(&self) -> Option<&T> {
        self.entries.last().map(Bucket::key_ref)
    }
    pub fn split_at(&self, index: usize) -> (&Self, &Self) {
        let (first, second) = self.entries.split_at(index);
        (Self::from_slice(first), Self::from_slice(second))
    }
    pub fn split_first(&self) -> Option<(&T, &Self)> {
        if let [first, rest @ ..] = &self.entries {
            Some((&first.key, Self::from_slice(rest)))
        } else {
            None
        }
    }
    pub fn split_last(&self) -> Option<(&T, &Self)> {
        if let [rest @ .., last] = &self.entries {
            Some((&last.key, Self::from_slice(rest)))
        } else {
            None
        }
    }
    pub fn iter(&self) -> Iter<'_, T> {
        Iter::new(&self.entries)
    }
}
impl<'a, T> IntoIterator for &'a Slice<T> {
    type IntoIter = Iter<'a, T>;
    type Item = &'a T;
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}
impl<T> IntoIterator for Box<Slice<T>> {
    type IntoIter = IntoIter<T>;
    type Item = T;
    fn into_iter(self) -> Self::IntoIter {
        IntoIter::new(self.into_entries())
    }
}
impl<T> Default for &'_ Slice<T> {
    fn default() -> Self {
        Slice::from_slice(&[])
    }
}
impl<T> Default for Box<Slice<T>> {
    fn default() -> Self {
        Slice::from_boxed(Box::default())
    }
}
impl<T: Clone> Clone for Box<Slice<T>> {
    fn clone(&self) -> Self {
        Slice::from_boxed(self.entries.to_vec().into_boxed_slice())
    }
}
impl<T: Copy> From<&Slice<T>> for Box<Slice<T>> {
    fn from(slice: &Slice<T>) -> Self {
        Slice::from_boxed(Box::from(&slice.entries))
    }
}
impl<T: fmt::Debug> fmt::Debug for Slice<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list().entries(self).finish()
    }
}
impl<T: PartialEq> PartialEq for Slice<T> {
    fn eq(&self, other: &Self) -> bool {
        self.len() == other.len() && self.iter().eq(other)
    }
}
impl<T: Eq> Eq for Slice<T> {}
impl<T: PartialOrd> PartialOrd for Slice<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.iter().partial_cmp(other)
    }
}
impl<T: Ord> Ord for Slice<T> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.iter().cmp(other)
    }
}
impl<T: Hash> Hash for Slice<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.len().hash(state);
        for value in self {
            value.hash(state);
        }
    }
}
impl<T> Index<usize> for Slice<T> {
    type Output = T;
    fn index(&self, index: usize) -> &Self::Output {
        &self.entries[index].key
    }
}
macro_rules! impl_index {
    ($($range:ty),*) => {$(
        impl<T, S> Index<$range> for IndexSet<T, S> {
            type Output = Slice<T>;
            fn index(&self, range: $range) -> &Self::Output {
                Slice::from_slice(&self.as_entries()[range])
            }
        }
        impl<T> Index<$range> for Slice<T> {
            type Output = Self;
            fn index(&self, range: $range) -> &Self::Output {
                Slice::from_slice(&self.entries[range])
            }
        }
    )*}
}
impl_index!(
    ops::Range<usize>,
    ops::RangeFrom<usize>,
    ops::RangeFull,
    ops::RangeInclusive<usize>,
    ops::RangeTo<usize>,
    ops::RangeToInclusive<usize>,
    (Bound<usize>, Bound<usize>)
);
#[cfg(test)]
mod tests {
    use super::*;
    use alloc::vec::Vec;
    #[test]
    fn slice_index() {
        fn check(vec_slice: &[i32], set_slice: &Slice<i32>, sub_slice: &Slice<i32>) {
            assert_eq!(set_slice as *const _, sub_slice as *const _);
            itertools::assert_equal(vec_slice, set_slice);
        }
        let vec: Vec<i32> = (0..10).map(|i| i * i).collect();
        let set: IndexSet<i32> = vec.iter().cloned().collect();
        let slice = set.as_slice();
        check(&vec[..], &set[..], &slice[..]);
        for i in 0usize..10 {
            assert_eq!(vec[i], set[i]);
            assert_eq!(vec[i], slice[i]);
            check(&vec[i..], &set[i..], &slice[i..]);
            check(&vec[..i], &set[..i], &slice[..i]);
            check(&vec[..=i], &set[..=i], &slice[..=i]);
            let bounds = (Bound::Excluded(i), Bound::Unbounded);
            check(&vec[i + 1..], &set[bounds], &slice[bounds]);
            for j in i..=10 {
                check(&vec[i..j], &set[i..j], &slice[i..j]);
            }
            for j in i..10 {
                check(&vec[i..=j], &set[i..=j], &slice[i..=j]);
            }
        }
    }
}