using System.Runtime.CompilerServices;
using Unity.Mathematics;
using UnityEngine;

namespace RebootKit.Engine.Extensions {
    public static class Vector3Ex {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static Vector3 With(this Vector3 vec, float? x = null, float? y = null, float? z = null) {
            return new Vector3(x ?? vec.x,
                               y ?? vec.y,
                               z ?? vec.z);
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static bool IsZero(this Vector3 vec, float epsilon = float.Epsilon) {
            return math.abs(vec.x) < epsilon && math.abs(vec.y) < epsilon && math.abs(vec.z) < epsilon;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static bool IsNonZero(this Vector3 vec, float epsilon = float.Epsilon) {
            return !vec.IsZero(epsilon);
        }
    }

    public static class float3Ex {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static float3 With(this float3 vec, float? x = null, float? y = null, float? z = null) {
            return new float3(x ?? vec.x,
                               y ?? vec.y,
                               z ?? vec.z);
        }
        
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static bool IsZero(this float3 vec, float epsilon = float.Epsilon) {
            return math.abs(vec.x) < epsilon && math.abs(vec.y) < epsilon && math.abs(vec.z) < epsilon;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static bool IsNonZero(this float3 vec, float epsilon = float.Epsilon) {
            return !vec.IsZero(epsilon);
        }
        
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static bool IsInRange(this float3 vec, float3 pos, float range) {
            float dstSquared = math.distancesq(vec, pos);
            float rangeSquared = range * range;
            return dstSquared <= rangeSquared;
        }
    }
}