using System;
using System.Collections;
using UnityEngine;

namespace Intrepid.Gameplay.Entities.Player.PDA.UI
{
    [Serializable]
    [CreateAssetMenu(fileName = "New Linear Animation", menuName = "Intrepid/Animation/Linear Animation")]
    public class LinearAnimation : ScriptableObject
    {
        [SerializeField] private float timeToNextPosition = .25f;
        [SerializeField] private int frames = 60;
        
        public IEnumerator Animate(
            float start,
            float final,
            Func<float, float> deltaIncrement,
            Action<float> updateValue,
            Action<bool> animationRunning)
        {
            animationRunning.Invoke(true);
            var dInc = Mathf.Abs(final - start) / frames;
            var tInc = timeToNextPosition / frames;
            var v = start;
            for (var i = 0; i < frames; i++)
            {
                v += deltaIncrement.Invoke(dInc);
                updateValue.Invoke(v);
                yield return new WaitForSeconds(tInc);
            }
            updateValue.Invoke(final);
            animationRunning.Invoke(false);
        }
    }
}