﻿/*
MIT License

Copyright(c) 2019 Mitchel Thompson
www.angryarugula.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Arugula
{
    public static class Mathemagic
    {

        public static Vector3 RandomVector3(Vector3 min, Vector3 max)
        {
            return new Vector3(Random.Range(min.x, max.x), Random.Range(min.y, max.y), Random.Range(min.z, max.z));
        }

        public static Vector3 GetHeading(this Quaternion q)
        {
            Vector3 fwd = q * Vector3.forward;
            q = Quaternion.AngleAxis(-q.eulerAngles.z, fwd) * q;
            Vector3 right = q * Vector3.right;
            q = Quaternion.AngleAxis(-q.eulerAngles.x, right) * q;

            return q * Vector3.forward;

        }

        public static Vector3 GetHeading(this Vector3 fwd)
        {
            return Quaternion.LookRotation(fwd).GetHeading();
        }

        public static Vector3 GetHeading(this Vector3 fwd, Vector3 up)
        {
            return Quaternion.LookRotation(fwd, up).GetHeading();
        }

        public static Vector3 GetFlattened(this Vector3 v)
        {
            v.y = 0;
            return v;
        }

        public static Vector3 ClosestPointOnSegment(this Vector3 p, Vector3 a, Vector3 b)
        {
            var v1 = p - a;
            var v2 = (b - a).normalized;

            var d = Vector3.Distance(a, b);
            var t = Vector3.Dot(v2, v1);

            if (t <= 0)
                return a;

            if (t >= d)
                return b;

            Vector3 v = v2 * t;

            Vector3 c = a + v;

            return c;
        }

        public static Vector3 ClosestPointOnRay(this Vector3 p, Vector3 a, Vector3 dir)
        {
            var v1 = p - a;
            var v2 = (dir - a).normalized;

            var d = Vector3.Distance(a, dir);
            var t = Vector3.Dot(v2, v1);

            if (t <= 0)
                return a;

            Vector3 v = v2 * t;

            Vector3 c = a + v;

            return c;
        }

        public static Vector3 ClosestPointOnLine(this Vector3 p, Vector3 a, Vector3 dir)
        {
            var v1 = p - a;
            var v2 = (dir - a).normalized;

            var d = Vector3.Distance(a, dir);
            var t = Vector3.Dot(v2, v1);

            Vector3 v = v2 * t;

            Vector3 c = a + v;

            return c;
        }

        public static float Snap(this float value, float multiple = 1)
        {
            if (multiple <= 0) return value;
            return Mathf.Round(value / multiple) * multiple;
        }

        public static float SnapAngle(this float value, float increment)
        {
            return Mathf.DeltaAngle(0, value).Snap(increment);
        }

        public static Vector3 Snap(this Vector3 vec, float x = 1, float y = 1, float z = 1)
        {
            return new Vector3(vec.x.Snap(x), vec.y.Snap(y), vec.z.Snap(z));
        }

        public static float Clamp(this float f, float min = 0, float max = 1) => Mathf.Clamp(f, min, max);
        public static float Floor(this float f) => Mathf.Floor(f);
        public static int FloorToInt(this float f) => Mathf.FloorToInt(f);
        public static float Ceil(this float f) => Mathf.Ceil(f);
        public static int CeilToInt(this float f) => Mathf.CeilToInt(f);
    }
}
