use crate::{
    utils::{AffinePoint, WeierstrassAffinePoint, WeierstrassPoint},
    CryptoRuntime,
};
use fluentbase_types::CryptoAPI;

/// The number of limbs in `Bn254AffinePoint`.
pub const N: usize = 16;

/// A point on the Bn254 curve.
#[derive(Copy, Clone)]
#[repr(align(4))]
pub struct Bn254Point(pub WeierstrassPoint<N>);

impl WeierstrassAffinePoint<N> for Bn254Point {
    fn infinity() -> Self {
        Self(WeierstrassPoint::Infinity)
    }

    fn is_infinity(&self) -> bool {
        matches!(self.0, WeierstrassPoint::Infinity)
    }
}

impl AffinePoint<N> for Bn254Point {
    const GENERATOR: [u32; N] = [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0];

    #[allow(deprecated)]
    /// The generator has been taken from py_pairing python library by the Ethereum Foundation:
    ///
    /// <https://github.com/ethereum/py_pairing/blob/5f609da/py_ecc/bn128/bn128_field_elements.py>
    const GENERATOR_T: Self = Self(WeierstrassPoint::Affine(Self::GENERATOR));

    fn new(limbs: [u32; N]) -> Self {
        Self(WeierstrassPoint::Affine(limbs))
    }

    fn identity() -> Self {
        Self::infinity()
    }

    fn is_identity(&self) -> bool {
        self.is_infinity()
    }

    fn limbs_ref(&self) -> &[u32; N] {
        match &self.0 {
            WeierstrassPoint::Infinity => panic!("Infinity point has no limbs"),
            WeierstrassPoint::Affine(limbs) => limbs,
        }
    }

    fn limbs_mut(&mut self) -> &mut [u32; N] {
        match &mut self.0 {
            WeierstrassPoint::Infinity => panic!("Infinity point has no limbs"),
            WeierstrassPoint::Affine(limbs) => limbs,
        }
    }

    fn add_assign(&mut self, other: &Self) {
        let a: [u8; 64] = bytemuck::cast(*self.limbs_mut());
        let b: [u8; 64] = bytemuck::cast(*other.limbs_ref());
        CryptoRuntime::bn254_add(a, b);
    }

    fn complete_add_assign(&mut self, other: &Self) {
        self.weierstrass_add_assign(other);
    }

    fn double(&mut self) {
        let _a = self.limbs_mut();
        unimplemented!()
    }
}
