use std::ops::Add;

use derive_more::Deref;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use vecdb::{CheckedSub, Formattable, Pco, PrintableIndex, VecIndex};

use crate::TypeIndex;

#[derive(
    Debug,
    Default,
    Clone,
    Copy,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Deref,
    Serialize,
    Deserialize,
    Pco,
    JsonSchema,
)]
pub struct EmptyAddrIndex(TypeIndex);

impl From<TypeIndex> for EmptyAddrIndex {
    #[inline]
    fn from(value: TypeIndex) -> Self {
        Self(value)
    }
}

impl From<usize> for EmptyAddrIndex {
    #[inline]
    fn from(value: usize) -> Self {
        Self(TypeIndex::from(value))
    }
}
impl From<u32> for EmptyAddrIndex {
    #[inline]
    fn from(value: u32) -> Self {
        Self(TypeIndex::from(value))
    }
}

impl From<EmptyAddrIndex> for usize {
    #[inline]
    fn from(value: EmptyAddrIndex) -> Self {
        usize::from(value.0)
    }
}

impl Add<usize> for EmptyAddrIndex {
    type Output = Self;
    fn add(self, rhs: usize) -> Self::Output {
        Self(self.0 + rhs)
    }
}

impl CheckedSub<EmptyAddrIndex> for EmptyAddrIndex {
    fn checked_sub(self, rhs: Self) -> Option<Self> {
        self.0.checked_sub(rhs.0).map(Self)
    }
}

impl PrintableIndex for EmptyAddrIndex {
    fn to_string() -> &'static str {
        "empty_addr_index"
    }

    fn to_possible_strings() -> &'static [&'static str] {
        &["emptyaddr", "empty_addr_index"]
    }
}

impl VecIndex for EmptyAddrIndex {
    const INITIAL_CAPACITY: usize = 1_800_000_000;
}

impl std::fmt::Display for EmptyAddrIndex {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl Formattable for EmptyAddrIndex {
    #[inline(always)]
    fn write_to(&self, buf: &mut Vec<u8>) {
        self.0.write_to(buf);
    }
}
