using System.Collections;
using UnityEngine;

public sealed class JumpDistanceMarker : MonoBehaviour
{
    [SerializeField]
    private Gradient maxRangeColor = new Gradient();
    [SerializeField]
    private Gradient actualRangeColor = new Gradient();
    [SerializeField]
    private MeshRenderer maxMeshRenderer = null;
    [SerializeField]
    private MeshRenderer actualMeshRenderer = null;
    [SerializeField]
    private Transform maxJumpRadiusTransform = null;
    [SerializeField]
    private Transform actualJumpRadiusTransform = null;
    [SerializeField]
    private float time = 1f;
    [SerializeField]
    private AnimationCurve showCurve = AnimationCurve.EaseInOut(0, 0, 1, 1);

    public void Show(Vector3 position, float maxJumpRadius = 0, float jumpRadius = 0) {
        transform.localPosition = position;
        maxJumpRadiusTransform.gameObject.SetActive(maxJumpRadius != jumpRadius);
        if (gameObject.activeSelf)
        {
            StartCoroutine(Animate(maxJumpRadiusTransform, maxMeshRenderer, time, showCurve, maxRangeColor, maxJumpRadius));
            StartCoroutine(Animate(actualJumpRadiusTransform, actualMeshRenderer, time, showCurve, actualRangeColor, jumpRadius));
        }
    }

    private IEnumerator Animate(Transform transform, MeshRenderer renderer, float time, AnimationCurve showCurve, Gradient gradient, float radius)
    {
        var timer = 0f;
        while (timer < time) {
            timer += Time.deltaTime;
            var animation = timer / time;
            transform.localScale = Vector3.one * (radius * 2f * showCurve.Evaluate(animation));
            renderer.material.color = gradient.Evaluate(animation);
            yield return null;
        }
        transform.localScale = Vector3.one * radius * 2f;
        renderer.material.color = gradient.Evaluate(1);
    }
}
