using System;
using System.Collections.Generic;
using VisiMath;

namespace VisiMath.Geometry
{
    public interface IIntersectionInfo
    {
        /// <summary>
        /// Distance from the origin
        /// </summary>
        float Distance { get; set; }
        /// <summary>
        /// Geometrical object with intersection
        /// </summary>
        IGeometry GeoObject { get; set; }
        /// <summary>
        /// Intersection point
        /// </summary>
        Vector3 IntersectionPoint { get; set; }
    }


    /// <summary>
    /// Structure for holding intersection info
    /// </summary>
    public struct IntersectionInfo : VisiMath.Geometry.IIntersectionInfo
    {
        /// <summary>
        /// Distance from the origin
        /// </summary>
        public float Distance { get; set; }
        /// <summary>
        /// Geometrical object with intersection
        /// </summary>
        public IGeometry GeoObject { get; set; }
        /// <summary>
        /// Intersection point
        /// </summary>
        public Vector3 IntersectionPoint { get; set; }

        /// <summary>
        /// creates new starting intersection info
        /// </summary>
        /// <returns></returns>
        public static IntersectionInfo CreateNew()
        {
            IntersectionInfo rval = new IntersectionInfo();
            rval.Distance = float.MaxValue;
            rval.GeoObject = null;
            rval.IntersectionPoint = Vector3.Invalid;
            return rval;
        }
    }

    /// <summary>
    /// List of intersections
    /// </summary>
    public class IntersectionList
    {
        /// <summary>
        /// List of intersections
        /// </summary>
        public List<IIntersectionInfo> Intersections;

        /// <summary>
        /// Index of the closest intersection
        /// </summary>
        public int Closest;

        /// <summary>
        /// Adds another intersection and checks if it's closer then previous values
        /// </summary>
        /// <param name="info"></param>
        public void Add(IIntersectionInfo info)
        {
            if (Intersections.Count > 0)
            {
                if (Intersections[Closest].Distance < info.Distance)
                    Closest = Intersections.Count;
            }
            Intersections.Add(info);
        }
    }
}
