using System;
namespace Engine.Core;
/// 
/// Represents a 3D line segment defined by two endpoints.
/// 
/// The starting point of the  segment.
/// The ending point of the  segment.
/// 
/// Initializes a new instance of the  struct with the specified endpoints.
/// 
[System.Diagnostics.DebuggerDisplay("From: {From.ToString(),nq}, To: {To.ToString(),nq}, Direction: {Direction.ToString(),nq}, Length: {Length}")]
public readonly struct Line3D(Vector3D from, Vector3D to) : IEquatable
{
    /// 
    /// The starting point of the  segment.
    /// 
    public readonly Vector3D From = from;
    /// 
    /// The ending point of the  segment.
    /// 
    public readonly Vector3D To = to;
    /// 
    /// The reversed  segment.
    /// 
    public readonly Line3D Reversed => new(To, From);
    /// 
    /// The normalized direction  of the  segment.
    /// 
    public readonly Vector3D Direction => From.FromTo(To).Normalize();
    /// 
    /// The length of the  segment.
    /// 
    public readonly float Length => From.FromTo(To).Length();
    /// 
    /// The squared length of the  segment.
    /// 
    public readonly float LengthSquared => From.FromTo(To).LengthSquared();
    public static bool operator ==(Line3D left, Line3D right) => left.From == right.From && left.To == right.To;
    public static bool operator !=(Line3D left, Line3D right) => left.From != right.From || left.To != right.To;
    /// 
    /// Linearly interpolates between the two endpoints of the  segment using parameter 't'.
    /// 
    public static Vector3D Lerp(Line3D line, float t)
        => Vector3D.Lerp(line.From, line.To, t);
    /// 
    /// Calculates the closest point on the  segment to the specified point.
    /// 
    public static Vector3D ClosestPointTo(Line3D line, Vector3D point)
    {
        Vector3D lineRelativeVector = line.From.FromTo(line.To);
        Vector3D lineDirection = lineRelativeVector.Normalized;
        Vector3D pointVector = line.From.FromTo(point);
        float dot = lineDirection.Dot(pointVector).Clamp(0f, lineRelativeVector.Magnitude);
        return lineDirection * dot;
    }
    /// 
    /// Checks if two  segments are approximately equal.
    /// 
    /// The first .
    /// The second .
    /// The epsilon range.
    ///  if the s are approximately equal; otherwise, .
    public static bool ApproximatelyEquals(Line3D left, Line3D right, float epsilon = float.Epsilon)
        => left.From.ApproximatelyEquals(right.From, epsilon) && left.To.ApproximatelyEquals(right.To, 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 Line3D line3D && this == line3D;
    public bool Equals(Line3D other) => this == other;
    /// 
    /// Generates a hash code for the .
    /// 
    /// A hash code for the .
    public override int GetHashCode() => System.HashCode.Combine(From, To);
    /// 
    /// Converts the  to its string representation.
    /// 
    /// A string representation of the .
    public override string ToString() => $"{nameof(Line3D)}({From}, {To})";
}
/// 
/// Provides extension methods for the  struct.
/// 
public static class Line3DExtensions
{
    /// 
    public static Vector3D Lerp(this Line3D line, float t) => Line3D.Lerp(line, t);
    /// 
    public static Vector3D ClosestPointTo(this Line3D line, Vector3D point) => Line3D.ClosestPointTo(line, point);
    /// 
    public static bool ApproximatelyEquals(this Line3D left, Line3D right, float epsilon = float.Epsilon) => Line3D.ApproximatelyEquals(left, right, epsilon);
}