using System;
using nkast.Aether.Physics2D.Common;

namespace CtrEditor.Simulacion.Fluids.Components
{
    /// <summary>
    /// Utilidades para operaciones con Vector2 de nkast.Aether.Physics2D.Common
    /// </summary>
    public static class VectorUtils
    {
        /// <summary>
        /// Calcula la distancia entre dos vectores
        /// </summary>
        public static float DistanceBetween(Vector2 v1, Vector2 v2)
        {
            float result;
            Vector2 v1Ref = v1;
            Vector2 v2Ref = v2;
            Vector2.Distance(ref v1Ref, ref v2Ref, out result);
            return result;
        }
        
        /// <summary>
        /// Calcula el producto punto entre dos vectores
        /// </summary>
        public static float DotProduct(Vector2 v1, Vector2 v2)
        {
            float result;
            Vector2 v1Ref = v1;
            Vector2 v2Ref = v2;
            Vector2.Dot(ref v1Ref, ref v2Ref, out result);
            return result;
        }
        
        /// <summary>
        /// Normaliza un vector y retorna el resultado
        /// </summary>
        public static Vector2 NormalizeVector(Vector2 v)
        {
            Vector2 result;
            float length = v.Length();
            
            if (length < 1e-6f)
                return new Vector2(0, 0);
                
            Vector2.Divide(ref v, length, out result);
            return result;
        }
        
        /// <summary>
        /// Calcula la reflexión de un vector respecto a una normal
        /// </summary>
        public static Vector2 Reflect(Vector2 vector, Vector2 normal)
        {
            Vector2 normalized = NormalizeVector(normal);
            float dot;
            Vector2 vectorRef = vector;
            Vector2 normalizedRef = normalized;
            Vector2.Dot(ref vectorRef, ref normalizedRef, out dot);
            
            Vector2 result;
            Vector2.Multiply(ref normalizedRef, 2f * dot, out result);
            Vector2 vectorRef2 = vector;
            Vector2 resultRef = result;
            Vector2.Subtract(ref vectorRef2, ref resultRef, out result);
            
            return result;
        }
    }
}
