using System;
namespace Engine.Core;
/// 
/// Represents an infinite ray in 2D space.
/// 
/// The  in 2D space where the ray starts from.
/// Normalized  indicating the ray's is direction. 
public readonly struct Ray2D(Vector2D Origin, Vector2D Direction) : IEquatable
{
    /// 
    /// The starting point of the .
    /// 
    public readonly Vector2D Origin = Origin;
    /// 
    /// The direction in which the  points. Should be a normalized vector.
    /// 
    public readonly Vector2D Direction = Direction;
    /// 
    /// Gets a  with the same origin but with the direction reversed.
    /// 
    public readonly Ray2D Reversed => new(Origin, -Direction);
    public static bool operator ==(Ray2D left, Ray2D right) => left.Origin == right.Origin && left.Direction == right.Direction;
    public static bool operator !=(Ray2D left, Ray2D right) => left.Origin != right.Origin || left.Direction != right.Direction;
    public static implicit operator Ray2D(Line2D line) => new(line.From, line.From.FromTo(line.To).Normalized);
    /// 
    /// Constructs a  from a , extending from its origin in the 's direction for a given distance.
    /// 
    /// The source .
    /// The length of the line segment to create from the .
    /// A  representing the segment of the .
    public static Line2D GetLine(Ray2D ray, float distance)
        => new(ray.Origin, ray.Origin + ray.Direction * distance);
    /// 
    /// Evaluates the point on the  at a specified distance from its origin.
    /// 
    /// The  to evaluate.
    /// The distance from the origin along the 's direction.
    /// A  representing the point at the given distance on the .
    public static Vector2D Evaluate(Ray2D ray, float distanceFromOrigin)
        => ray.Origin + ray.Direction * distanceFromOrigin;
    /// 
    /// Calculates the closest point on the  to the specified point.
    /// 
    public static Vector2D ClosestPointTo(Ray2D ray, Vector2D point)
    {
        Vector2D originToPoint = ray.Origin.FromTo(point);
        float dot = ray.Direction.Dot(originToPoint);
        return ray.Origin + ray.Direction * dot;
    }
    /// 
    /// Checks if two s are approximately equal within a specified epsilon range.
    /// 
    /// The first .
    /// The second .
    /// The epsilon range.
    ///  if the s are approximately equal; otherwise, .
    public static bool ApproximatelyEquals(Ray2D left, Ray2D right, float epsilon = float.Epsilon)
        => left.Origin.ApproximatelyEquals(right.Origin, epsilon) && left.Direction.ApproximatelyEquals(right.Direction, 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 Ray2D ray2D && this == ray2D;
    public bool Equals(Ray2D other) => this == other;
    /// 
    /// Generates a hash code for the .
    /// 
    /// A hash code for the .
    public override int GetHashCode() => System.HashCode.Combine(Origin, Direction);
    /// 
    /// Converts the  to its string representation.
    /// 
    /// A string representation of the .
    public override string ToString() => $"{nameof(Ray2D)}({Origin}, {Direction})";
}
/// 
/// Provides extension methods for the  struct.
/// 
public static class Ray2DExtensions
{
    /// 
    public static Vector2D Evaluate(this Ray2D ray, float distanceFromOrigin) => Ray2D.Evaluate(ray, distanceFromOrigin);
    /// 
    public static bool ApproximatelyEquals(this Ray2D left, Ray2D right, float epsilon = float.Epsilon) => Ray2D.ApproximatelyEquals(left, right, epsilon);
}