﻿/*
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.
*/

//#define TWEENER_DEBUG

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Random = UnityEngine.Random;

namespace Arugula.Animation
{

    public static class Defaults
    {
        public const float duration = 1;
        public const Easing easing = Easing.easeInOutSine;
        public const LoopMode loop = LoopMode.None;
    }

    public enum Easing
    {
        linear,
        easeInBack,
        easeInBounce,
        easeInOutBack,
        easeInOutSine,
        easeInSine,
        easeInQuadratic,
        easeInElastic,
        easeOutBack,
        easeOutBounce,
        easeOutSine,
        easeOutQuadratic,
        easeOutElastic
    }

    public enum LoopMode
    {
        None,
        Repeat,
        PingPong
    }

    public interface ITween
    {
        void Update(float deltaTime);
        void Finish(bool complete = true);
        float Duration { get; set; }
        float Delay { get; set; }
        float Time { get; set; }
        float NormalizedTime { get; set; }
        LoopMode Loop { get; set; }
        UnityEngine.Object Context { get; set; }
        bool Paused { get; set; }
        bool Active { get; }
        bool Finished { get; }
        int LoopCount { get; }
        event Action OnStarted;
        event Action OnUpdated;
        event Action<int> OnLooped;
        event Action<bool> OnFinished;
        event Action OnContextLost;

    }
    public interface ITween<T> : ITween
    {
        T Origin { get; set; }
        T Target { get; set; }
        T Current { get; }
        event Action<T> OnUpdatedValue;
    }

    [Serializable]
    public class TweenData
    {
        public float duration = Defaults.duration;
        public float delay;
        public Easing easing = Defaults.easing;
        public AnimationCurve curve = null;
        public LoopMode loop = Defaults.loop;
        public Space space = Space.Self;

        public bool UsesCustomCurve => curve != null && curve.keys.Length >= 2;
    }

    //[Serializable]  //TODO:  Can't serialize natively this until Unity 2020 sad face.
    public class TweenData<T> : TweenData
    {
        public T target;
    }


    public class Tween : ITween
    {
        public float Duration
        {
            get { return duration; }
            set
            {
                if (value <= 0)
                    return;

                if (duration != value)
                {
                    if (Time > 0)
                    {
                        float p = Time / (Delay + Duration);
                        Time = Mathf.Lerp(0, Delay + value, p);
                    }
                }

                duration = value;
            }

        }
        public float Time
        {
            get { return time; }
            set
            {
                time = value;
                if (Finished && time < Delay + Duration)
                    Finished = false;

                Update(0);
            }
        }
        public float NormalizedTime
        {
            get { return time / (Delay + Duration); }
            set
            {
                Time = Mathf.Lerp(0, Delay + Duration, Mathf.Clamp01(value));
            }
        }
        public LoopMode Loop { get; set; }
        public float Delay { get; set; }
        public UnityEngine.Object Context
        {
            get
            {
                return context;
            }
            set
            {
                context = value;
                useContext = context != null;
            }
        }

        public bool Paused { get; set; }
        public bool Active { get; private set; }
        public bool Finished { get; private set; }
        public int LoopCount { get; private set; }

        public event Action OnStarted = delegate { };
        public event Action OnUpdated = delegate { };
        public event Action<bool> OnFinished = delegate { };
        public event Action OnContextLost = delegate { };
        public event Action<int> OnLooped = delegate { };

        protected AnimationCurve curve;
        protected bool useContext;
        protected UnityEngine.Object context;
        protected float duration;
        protected float time;
        private int direction = 1;

        public Tween(float duration, float delay, AnimationCurve curve, LoopMode loop, UnityEngine.Object context)
        {
            Duration = duration;
            Delay = delay;
            this.curve = curve;
            Loop = loop;
            Context = context;
        }

        public Tween(TweenData data, UnityEngine.Object context) : this(
            data.duration, 
            data.delay,
            (data.curve == null || data.curve.keys.Length >= 2) ? data.curve : Tweener.GetCurve(data.easing),
            data.loop, 
            context)
        {
            
        }

        public virtual void UpdateCurrent(float p)
        {
            //IE: for vector3 tweens
            //Current = Vector3.LerpUnclamped(Origin, Target, curve.Evaluate(p));
        }

        public void Update(float deltaTime)
        {
            if (Paused || Finished)
                return;

            if (useContext && Context == null)
            {
                OnContextLost();
                Finish(false);
                return;
            }

            time += deltaTime * direction;

            if (Time >= Delay)
            {
                if (!Active)
                {
                    OnStarted();
                    Active = true;
                }

                float p = Mathf.Clamp01((Time - Delay) / Duration);
                UpdateCurrent(p);
                OnUpdated();

                if (p == 1)
                {
                    switch (Loop)
                    {
                        case LoopMode.None:
                            Finish();
                            break;
                        case LoopMode.Repeat:
                            Time = 0;
                            Active = false;
                            LoopCount++;
                            OnLooped(LoopCount);
                            break;
                        case LoopMode.PingPong:
                            if (direction == 1)
                                time = duration;
                            else
                                time = 0;
                            direction *= -1;
                            LoopCount++;
                            OnLooped(LoopCount);
                            break;
                    }
                }
            }
            else
            {
                Active = false;
                UpdateCurrent(0);

                if (direction == -1 && Time <= 0)
                {
                    Time = 0;
                    direction = 1;
                }
            }
        }

        public virtual void Finish(bool complete = true)
        {
            if (complete && Loop == LoopMode.None && NormalizedTime < 1)
                UpdateCurrent(1);

            Active = false;
            Finished = true;
            OnFinished(complete);
        }

        public override string ToString()
        {
            return string.Format("[{0}] {1}/{2}s {3}", GetType(), (Time - Delay).ToString("f2"), Duration.ToString("f2"), Context);
        }
    }

    public class FloatTween : Tween, ITween<float>
    {
        public FloatTween(float origin, float target, float duration, float delay, AnimationCurve curve, LoopMode loop, UnityEngine.Object context) : base(duration, delay, curve, loop, context)
        {
            Origin = origin;
            Target = target;
            Current = origin;
        }

        public FloatTween(float origin, float target, TweenData data, UnityEngine.Object context) : base(data, context)
        {
            Origin = origin;
            Target = target;
            Current = origin;
        }

        public float Origin { get; set; }
        public float Target { get; set; }
        public float Current { get; private set; }

        public event Action<float> OnUpdatedValue = delegate { };

        public override void UpdateCurrent(float p)
        {
            Current = Mathf.LerpUnclamped(Origin, Target, curve.Evaluate(p));
            OnUpdatedValue(Current);
        }
    }

    public class Vector3Tween : Tween, ITween<Vector3>
    {
        public Vector3Tween(Vector3 origin, Vector3 target, float duration, float delay, AnimationCurve curve, LoopMode loop, UnityEngine.Object context) : base(duration, delay, curve, loop, context)
        {
            Origin = origin;
            Target = target;
            Current = origin;
        }

        public Vector3Tween(Vector3 origin, Vector3 target, TweenData data, UnityEngine.Object context) : base(data, context)
        {
            Origin = origin;
            Target = target;
            Current = origin;
        }

        public Vector3 Origin { get; set; }
        public Vector3 Target { get; set; }
        public Vector3 Current { get; private set; }

        public event Action<Vector3> OnUpdatedValue = delegate { };

        public override void UpdateCurrent(float p)
        {
            Current = Vector3.LerpUnclamped(Origin, Target, curve.Evaluate(p));
            OnUpdatedValue(Current);
        }
    }

    public class ColorTween : Tween, ITween<Color>
    {
        public ColorTween(Color origin, Color target, float duration, float delay, AnimationCurve curve, LoopMode loop, UnityEngine.Object context) : base(duration, delay, curve, loop, context)
        {
            Origin = origin;
            Target = target;
            Current = origin;
        }

        public ColorTween(Color origin, Color target, TweenData data, UnityEngine.Object context) : base(data, context)
        {
            Origin = origin;
            Target = target;
            Current = origin;
        }

        public Color Origin { get; set; }
        public Color Target { get; set; }
        public Color Current { get; private set; }

        public event Action<Color> OnUpdatedValue = delegate { };

        public override void UpdateCurrent(float p)
        {
            Current = Color.LerpUnclamped(Origin, Target, curve.Evaluate(p));
            OnUpdatedValue(Current);
        }
    }

    public class Vector3Punch : Tween, ITween<Vector3>
    {
        public Vector3Punch(Vector3 origin, Vector3 amount, float duration, float delay, AnimationCurve curve, LoopMode loop, UnityEngine.Object context) : base(duration, delay, curve, loop, context)
        {
            Origin = origin;
            Target = amount;
            Current = origin + new Vector3(Random.Range(-Target.x, Target.x), Random.Range(-Target.y, Target.y), Random.Range(-Target.z, Target.z));
        }

        public override void UpdateCurrent(float p)
        {
            float intensity = curve.Evaluate(1f - p);

            Vector3 offset = Current - Origin;
            offset.x = Random.Range(-Target.x, Target.x) * intensity;
            offset.y = Random.Range(-Target.y, Target.y) * intensity;
            offset.z = Random.Range(-Target.z, Target.z) * intensity;
            //offset.x = Random.Range(0.1f,1) * Target.x * intensity * -Mathf.Sign(offset.x);
            //offset.y = Random.Range(0.1f, 1) * Target.y * intensity * -Mathf.Sign(offset.y);
            //offset.z = Random.Range(0.1f, 1) * Target.z * intensity * -Mathf.Sign(offset.z);

            Current = Origin + offset;

            OnUpdatedValue(Current);
        }

        public Vector3 Origin { get; set; }
        public Vector3 Target { get; set; }
        public Vector3 Current { get; private set; }

        public event Action<Vector3> OnUpdatedValue = delegate { };
    }

    public class QuaternionTween : Tween, ITween<Quaternion>
    {
        public QuaternionTween(Quaternion origin, Quaternion target, float duration, float delay, AnimationCurve curve, LoopMode loop, UnityEngine.Object context) : base(duration, delay, curve, loop, context)
        {
            Origin = origin;
            Target = target;
            Current = origin;
        }

        public Quaternion Origin { get; set; }
        public Quaternion Target { get; set; }
        public Quaternion Current { get; private set; }

        public event Action<Quaternion> OnUpdatedValue = delegate { };

        public override void UpdateCurrent(float p)
        {
            Current = Quaternion.SlerpUnclamped(Origin, Target, curve.Evaluate(p));
            OnUpdatedValue(Current);
        }
    }

    public class TransformPoseTween : Tween, ITween<TransformPose>
    {
        public TransformPoseTween(TransformPose origin, TransformPose target, float duration, float delay, AnimationCurve curve, LoopMode loop, UnityEngine.Object context) : base(duration, delay, curve, loop, context)
        {
            Origin = origin;
            Target = target;
            Current = origin;
        }

        public TransformPoseTween(TransformPose origin, TransformPose target, float duration, float delay, Easing easing, LoopMode loop, UnityEngine.Object context) : base(duration, delay, Tweener.GetCurve(easing), loop, context)
        {
            Origin = origin;
            Target = target;
            Current = origin;
        }

        public TransformPose Origin { get; set; }
        public TransformPose Target { get; set; }
        public TransformPose Current { get; private set; }

        public event Action<TransformPose> OnUpdatedValue = delegate { };

        public override void UpdateCurrent(float p)
        {
            Current = TransformPose.LerpUnclamped(Origin, Target, curve.Evaluate(p));
            OnUpdatedValue(Current);
        }
    }

    public class Tweener : MonoBehaviour
    {
        static Queue<ITween> tweenQueue = new Queue<ITween>();
        static List<ITween> tweens = new List<ITween>();
        static Queue<ITween> finishedTweens = new Queue<ITween>();

        static Dictionary<string, AnimationCurve> curveCache = new Dictionary<string, AnimationCurve>();

        static Tweener inst;
        static Tweener()
        {
            if (inst == null)
            {
                GameObject go = new GameObject("Tweener", typeof(Tweener));
                go.hideFlags = HideFlags.HideInHierarchy;
                DontDestroyOnLoad(go);
                inst = go.GetComponent<Tweener>();
            }
        }

        public static AnimationCurve GetCurve(string name)
        {
            if (curveCache.ContainsKey(name))
                return curveCache[name];

            curveCache.Add(name, Resources.Load<TweenCurve>(name).curve);
            return curveCache[name];
        }

        public static AnimationCurve GetCurve(Easing easing)
        {
            return GetCurve(easing.ToString());
        }

        public static void Enqueue(ITween tween)
        {
            tweenQueue.Enqueue(tween);
            tween.OnFinished += (complete) => finishedTweens.Enqueue(tween);
            tween.OnContextLost += () => finishedTweens.Enqueue(tween);
        }

#if TWEENER_DEBUG
        private void OnGUI()
        {
            foreach (var t in tweens)
                GUILayout.Label(t.ToString());
        }
#endif

        private void Update()
        {
            while (tweenQueue.Count > 0)
                tweens.Add(tweenQueue.Dequeue());

            foreach (var t in tweens)
                t.Update(Time.deltaTime);

            while (finishedTweens.Count > 0)
                tweens.Remove(finishedTweens.Dequeue());
        }

    }
}
