use bnum::BInt;
use typenum::Unsigned;
use std::marker::PhantomData;

#[derive(Clone,Copy,Debug,Hash)]
pub struct Fixed<const CHUNKS:usize,Frac>{
	pub(crate)bits:BInt<{CHUNKS}>,
	pub(crate)frac:PhantomData<Frac>,
}

impl<const CHUNKS:usize,Frac:Unsigned> Fixed<CHUNKS,Frac>{
	pub const MAX:Self=Self{bits:BInt::<CHUNKS>::MAX,frac:PhantomData};
	pub const MIN:Self=Self{bits:BInt::<CHUNKS>::MIN,frac:PhantomData};
	pub const ZERO:Self=Self{bits:BInt::<CHUNKS>::ZERO,frac:PhantomData};
	pub const ONE:Self=Self{bits:BInt::<CHUNKS>::ONE.shl(Frac::U32),frac:PhantomData};
	pub const TWO:Self=Self{bits:BInt::<CHUNKS>::TWO.shl(Frac::U32),frac:PhantomData};
	pub const HALF:Self=Self{bits:BInt::<CHUNKS>::ONE.shl(Frac::U32-1),frac:PhantomData};
	pub const NEG_ONE:Self=Self{bits:BInt::<CHUNKS>::NEG_ONE.shl(Frac::U32),frac:PhantomData};
	pub const NEG_TWO:Self=Self{bits:BInt::<CHUNKS>::NEG_TWO.shl(Frac::U32),frac:PhantomData};
	pub const NEG_HALF:Self=Self{bits:BInt::<CHUNKS>::NEG_ONE.shl(Frac::U32-1),frac:PhantomData};
}

impl<const CHUNKS:usize,Frac:Unsigned,T> From<T> for Fixed<CHUNKS,Frac>
	where
		BInt<CHUNKS>:From<T>
{
	fn from(value:T)->Self{
		Self{
			bits:BInt::<{CHUNKS}>::from(value)<<Frac::U32,
			frac:PhantomData,
		}
	}
}

impl<const CHUNKS:usize,Frac> PartialEq for Fixed<CHUNKS,Frac>{
	fn eq(&self,other:&Self)->bool{
		self.bits.eq(&other.bits)
	}
}
impl<const CHUNKS:usize,Frac> Eq for Fixed<CHUNKS,Frac>{}

impl<const CHUNKS:usize,Frac> PartialOrd for Fixed<CHUNKS,Frac>{
	fn partial_cmp(&self,other:&Self)->Option<std::cmp::Ordering>{
		self.bits.partial_cmp(&other.bits)
	}
}
impl<const CHUNKS:usize,Frac> Ord for Fixed<CHUNKS,Frac>{
	fn cmp(&self,other:&Self)->std::cmp::Ordering{
		self.bits.cmp(&other.bits)
	}
}

impl<const CHUNKS:usize,Frac> std::ops::Neg for Fixed<CHUNKS,Frac>{
	type Output=Self;
	fn neg(self)->Self{
		Self{
			bits:self.bits.neg(),
			frac:PhantomData,
		}
	}
}

macro_rules! impl_operator {
	( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
		impl<const CHUNKS:usize,Frac> core::ops::$trait for $struct<CHUNKS,Frac>{
			type Output = $output;

			fn $method(self, other: Self) -> Self::Output {
				Self {
					bits:self.bits.$method(other.bits),
					frac:PhantomData,
				}
			}
		}
		impl<const CHUNKS:usize,Frac:Unsigned,U> core::ops::$trait<U> for $struct<CHUNKS,Frac>
			where
				BInt::<CHUNKS>:From<U>,
		{
			type Output = $output;

			fn $method(self, other: U) -> Self::Output {
				Self {
					bits:self.bits.$method(BInt::<CHUNKS>::from(other)<<Frac::U32),
					frac:PhantomData,
				}
			}
		}
	};
}
macro_rules! impl_assign_operator {
	( $struct: ident, $trait: ident, $method: ident ) => {
		impl<const CHUNKS:usize,Frac> core::ops::$trait for $struct<CHUNKS,Frac>{
			fn $method(&mut self, other: Self) {
				self.bits.$method(other.bits);
			}
		}
		impl<const CHUNKS:usize,Frac:Unsigned,U> core::ops::$trait<U> for $struct<CHUNKS,Frac>
		where
				BInt::<CHUNKS>:From<U>,
		{
			fn $method(&mut self, other: U) {
				self.bits.$method(BInt::<CHUNKS>::from(other)<<Frac::U32);
			}
		}
	};
}

// Impl arithmetic pperators
impl_assign_operator!( Fixed, AddAssign, add_assign );
impl_operator!( Fixed, Add, add, Self );
impl_assign_operator!( Fixed, SubAssign, sub_assign );
impl_operator!( Fixed, Sub, sub, Self );
impl_assign_operator!( Fixed, MulAssign, mul_assign );
impl_operator!( Fixed, Mul, mul, Self );
impl_assign_operator!( Fixed, DivAssign, div_assign );
impl_operator!( Fixed, Div, div, Self );
impl_assign_operator!( Fixed, RemAssign, rem_assign );
impl_operator!( Fixed, Rem, rem, Self );

// Impl bitwise operators
impl_assign_operator!( Fixed, BitAndAssign, bitand_assign );
impl_operator!( Fixed, BitAnd, bitand, Self );
impl_assign_operator!( Fixed, BitOrAssign, bitor_assign );
impl_operator!( Fixed, BitOr, bitor, Self );
impl_assign_operator!( Fixed, BitXorAssign, bitxor_assign );
impl_operator!( Fixed, BitXor, bitxor, Self );

macro_rules! impl_shift_operator {
	( $struct: ident, $trait: ident, $method: ident, $output: ty ) => {
		impl<const CHUNKS:usize,Frac> core::ops::$trait<u32> for $struct<CHUNKS,Frac>{
			type Output = $output;

			fn $method(self, other: u32) -> Self::Output {
				Self {
					bits:self.bits.$method(other),
					frac:PhantomData,
				}
			}
		}
	};
}
macro_rules! impl_shift_assign_operator {
	( $struct: ident, $trait: ident, $method: ident ) => {
		impl<const CHUNKS:usize,Frac> core::ops::$trait<u32> for $struct<CHUNKS,Frac>{
			fn $method(&mut self, other: u32) {
				self.bits.$method(other);
			}
		}
	};
}
impl_shift_assign_operator!( Fixed, ShlAssign, shl_assign );
impl_shift_operator!( Fixed, Shl, shl, Self );
impl_shift_assign_operator!( Fixed, ShrAssign, shr_assign );
impl_shift_operator!( Fixed, Shr, shr, Self );

impl<const CHUNKS:usize,Frac:Unsigned> Fixed<CHUNKS,Frac>{
	pub fn sqrt_unchecked(self)->Self{
		//find pow2 more powerful than self
		let mut pow2=if Self::ONE<self{
			let mut pow2=Self::TWO;//1<self is so bake one iteration
			while pow2<=self{
				pow2<<=1;
			}
			pow2
		}else if Self::ONE==self{
			return Self::ONE;
		}else if Self::ZERO<self{
			let mut pow2=Self::ONE;
			while self<=pow2{
				pow2>>=1;
			}
			pow2
		}else{//either 0==self or self is negative
			return Self::ZERO;
		};
		let mut result=pow2;
		while pow2!=Self::ZERO{
			pow2>>=1;
			let new_result=result+pow2;
			if new_result*new_result<=self{
				result=new_result;
			}
		}
		result
	}
	pub fn sqrt(self)->Self{
		if self<Self::ZERO{
			panic!("Square root less than zero")
		}else{
			self.sqrt_unchecked()
		}
	}
	pub fn sqrt_checked(self)->Option<Self>{
		if self<Self::ZERO{
			None
		}else{
			Some(self.sqrt_unchecked())
		}
	}
}