use crate::{
    buffers::{FetchRowMember, Indicator},
    handles::{CData, CDataMut, DataType, HasDataType},
    parameter::{CElement, OutputParameter},
};
use odbc_sys::{CDataType, Date, Numeric, Time, Timestamp};
use std::{
    ffi::c_void,
    ptr::{null, null_mut},
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Ord, PartialOrd)]
pub struct Bit(pub u8);
impl Bit {
                                    pub fn from_bool(boolean: bool) -> Self {
        if boolean { Bit(1) } else { Bit(0) }
    }
        pub fn as_bool(self) -> bool {
        match self.0 {
            0 => false,
            1 => true,
            _ => panic!("Invalid boolean representation in Bit."),
        }
    }
}
pub unsafe trait Pod: Default + Copy + CElement + CDataMut + Send + 'static {
        const C_DATA_TYPE: CDataType;
}
macro_rules! impl_pod {
    ($t:ident, $c_data_type:expr) => {
        unsafe impl CData for $t {
            fn cdata_type(&self) -> CDataType {
                $c_data_type
            }
            fn indicator_ptr(&self) -> *const isize {
                                null()
            }
            fn value_ptr(&self) -> *const c_void {
                self as *const $t as *const c_void
            }
            fn buffer_length(&self) -> isize {
                0
            }
        }
        unsafe impl CDataMut for $t {
                        fn mut_indicator_ptr(&mut self) -> *mut isize {
                null_mut()
            }
                        fn mut_value_ptr(&mut self) -> *mut c_void {
                self as *mut $t as *mut c_void
            }
        }
        unsafe impl CElement for $t {
                        fn assert_completness(&self) {}
        }
        unsafe impl Pod for $t {
            const C_DATA_TYPE: CDataType = $c_data_type;
        }
        unsafe impl FetchRowMember for $t {
            fn indicator(&self) -> Option<Indicator> {
                None
            }
        }
    };
}
impl_pod!(f64, CDataType::Double);
impl_pod!(f32, CDataType::Float);
impl_pod!(Date, CDataType::TypeDate);
impl_pod!(Timestamp, CDataType::TypeTimestamp);
impl_pod!(Time, CDataType::TypeTime);
impl_pod!(Numeric, CDataType::Numeric);
impl_pod!(i16, CDataType::SShort);
impl_pod!(u16, CDataType::UShort);
impl_pod!(i32, CDataType::SLong);
impl_pod!(u32, CDataType::ULong);
impl_pod!(i8, CDataType::STinyInt);
impl_pod!(u8, CDataType::UTinyInt);
impl_pod!(Bit, CDataType::Bit);
impl_pod!(i64, CDataType::SBigInt);
impl_pod!(u64, CDataType::UBigInt);
macro_rules! impl_input_fixed_sized {
    ($t:ident, $data_type:expr) => {
        impl HasDataType for $t {
            fn data_type(&self) -> DataType {
                $data_type
            }
        }
        unsafe impl OutputParameter for $t {}
    };
}
impl_input_fixed_sized!(f64, DataType::Double);
impl_input_fixed_sized!(f32, DataType::Real);
impl_input_fixed_sized!(Date, DataType::Date);
impl_input_fixed_sized!(i16, DataType::SmallInt);
impl_input_fixed_sized!(i32, DataType::Integer);
impl_input_fixed_sized!(i8, DataType::TinyInt);
impl_input_fixed_sized!(Bit, DataType::Bit);
impl_input_fixed_sized!(i64, DataType::BigInt);
#[cfg(test)]
mod tests {
    use super::Bit;
        #[test]
    #[should_panic(expected = "Invalid boolean representation in Bit.")]
    fn invalid_bit() {
        let bit = Bit(2);
        bit.as_bool();
    }
}