namespace PkmnLib.Static.Utils;

/// <summary>
/// Helper methods for numeric operations.
/// </summary>
public static class NumericHelpers
{
    /// <summary>
    /// Checks if two floating-point values are approximately equal within a specified tolerance.
    /// </summary>
    public static bool IsApproximatelyEqualTo(this float value, float other, float tolerance = 0.0001f) =>
        MathF.Abs(value - other) <= tolerance;

    /// <summary>
    /// Multiplies two values. If this overflows, returns <see cref="byte.MaxValue"/>.
    /// </summary>
    public static byte MultiplyOrMax(this byte value, byte multiplier)
    {
        var result = value * multiplier;
        return result > byte.MaxValue ? byte.MaxValue : (byte)result;
    }

    public static byte AddOrMax(this byte value, byte addend)
    {
        var result = value + addend;
        return result > byte.MaxValue ? byte.MaxValue : (byte)result;
    }

    public static byte SubtractOrMin(this byte value, byte subtrahend)
    {
        var result = value - subtrahend;
        return result < 0 ? (byte)0 : (byte)result;
    }

    /// <summary>
    /// Multiplies two values. If this overflows, returns <see cref="byte.MaxValue"/>.
    /// </summary>
    public static byte MultiplyOrMax(this byte value, float multiplier)
    {
        var result = value * multiplier;
        return result > byte.MaxValue ? byte.MaxValue : (byte)result;
    }

    /// <summary>
    /// Multiplies two values. If this overflows, returns <see cref="sbyte.MaxValue"/>.
    /// </summary>
    public static sbyte MultiplyOrMax(this sbyte value, sbyte multiplier)
    {
        var result = value * multiplier;
        return result > sbyte.MaxValue ? sbyte.MaxValue : (sbyte)result;
    }

    /// <summary>
    /// Multiplies two values. If this overflows, returns <see cref="ushort.MaxValue"/>.
    /// </summary>
    public static ushort MultiplyOrMax(this ushort value, ushort multiplier)
    {
        var result = value * multiplier;
        return result > ushort.MaxValue ? ushort.MaxValue : (ushort)result;
    }

    /// <summary>
    /// Multiplies two values. If this overflows, returns <see cref="ushort.MaxValue"/>.
    /// </summary>
    public static ushort MultiplyOrMax(this ushort value, float multiplier)
    {
        var result = value * multiplier;
        return result > ushort.MaxValue ? ushort.MaxValue : (ushort)result;
    }

    /// <summary>
    /// Multiplies two values. If this overflows, returns <see cref="short.MaxValue"/>.
    /// </summary>
    public static short MultiplyOrMax(this short value, short multiplier)
    {
        var result = value * multiplier;
        return result > short.MaxValue ? short.MaxValue : (short)result;
    }

    /// <summary>
    /// Multiplies two values. If this overflows, returns <see cref="uint.MaxValue"/>.
    /// </summary>
    public static uint MultiplyOrMax(this uint value, uint multiplier)
    {
        var result = (ulong)value * multiplier;
        return result > uint.MaxValue ? uint.MaxValue : (uint)result;
    }

    /// <summary>
    /// Multiplies two values. If this overflows, returns <see cref="uint.MaxValue"/>.
    /// </summary>
    public static uint MultiplyOrMax(this uint value, float multiplier)
    {
        var result = value * multiplier;
        return result > uint.MaxValue ? uint.MaxValue : (uint)result;
    }

    /// <summary>
    /// Multiplies two values. If this overflows, returns <see cref="uint.MaxValue"/>.
    /// </summary>
    public static int MultiplyOrMax(this int value, int multiplier)
    {
        var result = (long)value * multiplier;
        return result > int.MaxValue ? int.MaxValue : (int)result;
    }

    /// <summary>
    /// Multiplies two values. If this overflows, returns <see cref="uint.MaxValue"/>.
    /// </summary>
    public static int MultiplyOrMax(this int value, float multiplier)
    {
        var result = value * multiplier;
        return result > int.MaxValue ? int.MaxValue : (int)result;
    }
}