using GameEngine;
using UnityEngine;


namespace Assets.Script.Game.Character
{
	public class EntityMovementVariation : MonoBehaviour
	{
		[SerializeField] private AnimationCurve m_SpeedCurve = default(AnimationCurve);
		[SerializeField] private float m_LoopTime = default(float);
		[SerializeField] private float m_MinSpeed = default(float);
		[SerializeField] private float m_MaxSpeed = default(float);
		[SerializeField] private EntityMover m_Mover = default(EntityMover);
		[SerializeField] private bool m_PingPong = default(bool);
		private float _baseSpeed;
		private float _time;





		private void Start()
		{
			ResetLoop();
		}


		private void FixedUpdate()
		{
			_time += Time.deltaTime;

			if (m_Mover == null)
			{
				return;
			}

			float time = m_PingPong
				? Mathf.PingPong(_time, m_LoopTime)/m_LoopTime
				: Mathf.Repeat(_time, m_LoopTime)/m_LoopTime;

			m_Mover.Speed = _baseSpeed+ Mathf.Lerp(m_MinSpeed, m_MaxSpeed, m_SpeedCurve.Evaluate(time));
		}


		public void ResetLoop()
		{
			if (m_Mover != null)
			{
				_baseSpeed = m_Mover.Speed;
			}

			_time = 0f;
		}
	}
}
