// Copyright (C) 2020-2025 glam-det authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::AbsDiffEq;
use core::{cell, f32, f64};
#[cfg(feature = "num-complex")]
use num_complex::Complex;

/// Equality comparisons between two numbers using both the absolute difference and
/// relative based comparisons.
pub trait RelativeEq<Rhs = Self>: AbsDiffEq<Rhs>
where
    Rhs: ?Sized,
{
    /// The default relative tolerance for testing values that are far-apart.
    ///
    /// This is used when no `max_relative` value is supplied to the [`relative_eq`] macro.
    fn default_max_relative() -> Self::Epsilon;

    /// A test for equality that uses a relative comparison if the values are far apart.
    fn relative_eq(&self, other: &Rhs, epsilon: Self::Epsilon, max_relative: Self::Epsilon)
        -> bool;

    /// The inverse of [`RelativeEq::relative_eq`].
    fn relative_ne(
        &self,
        other: &Rhs,
        epsilon: Self::Epsilon,
        max_relative: Self::Epsilon,
    ) -> bool {
        !Self::relative_eq(self, other, epsilon, max_relative)
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Base implementations
///////////////////////////////////////////////////////////////////////////////////////////////////

// Implementation based on: [Comparing Floating Point Numbers, 2012 Edition]
// (https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/)

impl RelativeEq for f32 {
    #[inline]
    fn default_max_relative() -> f32 {
        f32::EPSILON
    }

    #[inline]
    #[allow(unused_imports)]
    fn relative_eq(&self, other: &f32, epsilon: f32, max_relative: f32) -> bool {
        // Handle same infinities
        if self == other {
            return true;
        }

        // Handle remaining infinities
        if f32::is_infinite(*self) || f32::is_infinite(*other) {
            return false;
        }

        #[cfg(all(feature = "std", not(feature = "libm_force")))]
        let abs_diff = f32::abs(self - other);
        #[cfg(any(
            feature = "libm_force",
            all(feature = "libm_fallback", not(feature = "std"))
        ))]
        let abs_diff = libm::fabsf(self - other);

        // For when the numbers are really close together
        if abs_diff <= epsilon {
            return true;
        }

        #[cfg(all(feature = "std", not(feature = "libm_force")))]
        let abs_self = f32::abs(*self);
        #[cfg(any(
            feature = "libm_force",
            all(feature = "libm_fallback", not(feature = "std"))
        ))]
        let abs_self = libm::fabsf(*self);
        #[cfg(all(feature = "std", not(feature = "libm_force")))]
        let abs_other = f32::abs(*other);
        #[cfg(any(
            feature = "libm_force",
            all(feature = "libm_fallback", not(feature = "std"))
        ))]
        let abs_other = libm::fabsf(*other);

        let largest = if abs_other > abs_self {
            abs_other
        } else {
            abs_self
        };

        // Use a relative difference comparison
        abs_diff <= largest * max_relative
    }
}

impl RelativeEq for f64 {
    #[inline]
    fn default_max_relative() -> f64 {
        f64::EPSILON
    }

    #[inline]
    #[allow(unused_imports)]
    fn relative_eq(&self, other: &f64, epsilon: f64, max_relative: f64) -> bool {
        // Handle same infinities
        if self == other {
            return true;
        }

        // Handle remaining infinities
        if f64::is_infinite(*self) || f64::is_infinite(*other) {
            return false;
        }

        #[cfg(all(feature = "std", not(feature = "libm_force")))]
        let abs_diff = f64::abs(self - other);
        #[cfg(any(
            feature = "libm_force",
            all(feature = "libm_fallback", not(feature = "std"))
        ))]
        let abs_diff = libm::fabs(self - other);

        // For when the numbers are really close together
        if abs_diff <= epsilon {
            return true;
        }

        #[cfg(all(feature = "std", not(feature = "libm_force")))]
        let abs_self = f64::abs(*self);
        #[cfg(any(
            feature = "libm_force",
            all(feature = "libm_fallback", not(feature = "std"))
        ))]
        let abs_self = libm::fabs(*self);
        #[cfg(all(feature = "std", not(feature = "libm_force")))]
        let abs_other = f64::abs(*other);
        #[cfg(any(
            feature = "libm_force",
            all(feature = "libm_fallback", not(feature = "std"))
        ))]
        let abs_other = libm::fabs(*other);

        let largest = if abs_other > abs_self {
            abs_other
        } else {
            abs_self
        };

        // Use a relative difference comparison
        abs_diff <= largest * max_relative
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Derived implementations
///////////////////////////////////////////////////////////////////////////////////////////////////

impl<'a, T: RelativeEq + ?Sized> RelativeEq for &'a T {
    #[inline]
    fn default_max_relative() -> T::Epsilon {
        T::default_max_relative()
    }

    #[inline]
    fn relative_eq(&self, other: &&'a T, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool {
        T::relative_eq(*self, *other, epsilon, max_relative)
    }
}

impl<'a, T: RelativeEq + ?Sized> RelativeEq for &'a mut T {
    #[inline]
    fn default_max_relative() -> T::Epsilon {
        T::default_max_relative()
    }

    #[inline]
    fn relative_eq(
        &self,
        other: &&'a mut T,
        epsilon: T::Epsilon,
        max_relative: T::Epsilon,
    ) -> bool {
        T::relative_eq(*self, *other, epsilon, max_relative)
    }
}

impl<T: RelativeEq + Copy> RelativeEq for cell::Cell<T> {
    #[inline]
    fn default_max_relative() -> T::Epsilon {
        T::default_max_relative()
    }

    #[inline]
    fn relative_eq(
        &self,
        other: &cell::Cell<T>,
        epsilon: T::Epsilon,
        max_relative: T::Epsilon,
    ) -> bool {
        T::relative_eq(&self.get(), &other.get(), epsilon, max_relative)
    }
}

impl<T: RelativeEq + ?Sized> RelativeEq for cell::RefCell<T> {
    #[inline]
    fn default_max_relative() -> T::Epsilon {
        T::default_max_relative()
    }

    #[inline]
    fn relative_eq(
        &self,
        other: &cell::RefCell<T>,
        epsilon: T::Epsilon,
        max_relative: T::Epsilon,
    ) -> bool {
        T::relative_eq(&self.borrow(), &other.borrow(), epsilon, max_relative)
    }
}

impl<A, B> RelativeEq<[B]> for [A]
where
    A: RelativeEq<B>,
    A::Epsilon: Clone,
{
    #[inline]
    fn default_max_relative() -> A::Epsilon {
        A::default_max_relative()
    }

    #[inline]
    fn relative_eq(&self, other: &[B], epsilon: A::Epsilon, max_relative: A::Epsilon) -> bool {
        self.len() == other.len()
            && Iterator::zip(self.iter(), other)
                .all(|(x, y)| A::relative_eq(x, y, epsilon.clone(), max_relative.clone()))
    }
}

#[cfg(feature = "num-complex")]
impl<T: RelativeEq> RelativeEq for Complex<T>
where
    T::Epsilon: Clone,
{
    #[inline]
    fn default_max_relative() -> T::Epsilon {
        T::default_max_relative()
    }

    #[inline]
    fn relative_eq(
        &self,
        other: &Complex<T>,
        epsilon: T::Epsilon,
        max_relative: T::Epsilon,
    ) -> bool {
        T::relative_eq(&self.re, &other.re, epsilon.clone(), max_relative.clone())
            && T::relative_eq(&self.im, &other.im, epsilon, max_relative)
    }
}
