using System;
using System.Collections.Generic;
namespace Engine.Core;
/// 
/// Represents an Axis-Aligned Bounding Box (AABB) in 3D space.
/// 
/// The lower boundary of the .
/// The upper boundary of the .
/// 
/// Initializes a new instance of the  struct with the specified lower and upper boundaries.
/// 
[System.Diagnostics.DebuggerDisplay("LowerBoundary: {LowerBoundary.ToString(), nq}, UpperBoundary: {UpperBoundary.ToString(), nq}")]
public readonly struct AABB3D(Vector3D lowerBoundary, Vector3D upperBoundary) : IEquatable
{
    /// 
    /// The lower boundary of the .
    /// 
    public readonly Vector3D LowerBoundary = lowerBoundary;
    /// 
    /// The upper boundary of the .
    /// 
    public readonly Vector3D UpperBoundary = upperBoundary;
    /// 
    /// Gets the center point of the .
    /// 
    public readonly Vector3D Center => (LowerBoundary + UpperBoundary) * .5f;
    /// 
    /// Gets the size of the .
    /// 
    public readonly Vector3D Size => LowerBoundary.FromTo(UpperBoundary).Abs();
    /// 
    /// Gets half the size of the .
    /// 
    public readonly Vector3D SizeHalf => Size * .5f;
    public static bool operator ==(AABB3D left, AABB3D right) => left.UpperBoundary == right.UpperBoundary && left.LowerBoundary == right.LowerBoundary;
    public static bool operator !=(AABB3D left, AABB3D right) => left.UpperBoundary != right.UpperBoundary || left.LowerBoundary != right.LowerBoundary;
    public static implicit operator AABB3D(Sphere3D sphere) => new(sphere.Center - new Vector3D(sphere.Radius, sphere.Radius, sphere.Radius), sphere.Center + new Vector3D(sphere.Radius, sphere.Radius, sphere.Radius));
    /// 
    /// Creates an  from a collection of s.
    /// 
    /// The collection of s.
    /// An  that bounds all the s.
    public static AABB3D FromVectors(IEnumerable vectors)
    {
        int counter = 0;
        Vector3D lowerBoundary = new(float.MaxValue, float.MaxValue, float.MaxValue);
        Vector3D upperBoundary = new(float.MinValue, float.MinValue, float.MinValue);
        foreach (Vector3D vector in vectors)
        {
            lowerBoundary = Vector3D.Min(lowerBoundary, vector);
            upperBoundary = Vector3D.Max(upperBoundary, vector);
            counter++;
        }
        if (counter < 2)
            throw new ArgumentException($"Parameter {nameof(vectors)} must have at least 2 items.");
        return new(lowerBoundary, upperBoundary);
    }
    /// 
    /// Checks if two s are approximately equal.
    /// 
    /// The first .
    /// The second .
    /// The epsilon range.
    ///  if the s are approximately equal; otherwise, .
    public static bool ApproximatelyEquals(AABB3D left, AABB3D right, float epsilon = float.Epsilon)
        => left.LowerBoundary.ApproximatelyEquals(right.LowerBoundary, epsilon) && left.UpperBoundary.ApproximatelyEquals(right.UpperBoundary, epsilon);
    /// 
    /// Determines whether the specified object is equal to the current .
    /// 
    /// The object to compare with the current .
    ///  if the specified object is equal to the current ; otherwise, .
    public override bool Equals(object? obj) => obj is AABB3D aabb && this == aabb;
    public bool Equals(AABB3D other) => this == other;
    /// 
    /// Generates a hash code for the .
    /// 
    /// A hash code for the .
    public override int GetHashCode() => System.HashCode.Combine(LowerBoundary, UpperBoundary);
    /// 
    /// Converts the  to its string representation.
    /// 
    /// A string representation of the .
    public override string ToString() => $"{nameof(AABB3D)}({LowerBoundary}, {UpperBoundary})";
}
/// 
/// Provides extension methods for the  struct.
/// 
public static class AABB3DExtensions
{
    /// 
    public static AABB3D ToAABB3D(this IEnumerable vectors) => AABB3D.FromVectors(vectors);
    /// 
    public static bool ApproximatelyEquals(this AABB3D left, AABB3D right, float epsilon = float.Epsilon) => AABB3D.ApproximatelyEquals(left, right, epsilon);
}