using System;
using UnityEngine;

public class HeartAnimation : MonoBehaviour
{
    [SerializeField] private GameObject bigHeart;
    [SerializeField] private GameObject smallHeart;
    private float animationDelay = .3f;
    private float animationDuration = .5f;
    private float heartToMeterDuration = .4f;

    public void GetHeart(Transform target, Action callback = null)
    {
        smallHeart.SetActive(false);
        this.CallWaitForSeconds(animationDelay, () =>
        {
            bigHeart.SetActive(true);
            this.CallWaitForSeconds(animationDuration, () =>
            {
                var cacheTransform = bigHeart.transform;
                var beginPos = cacheTransform.position;
                cacheTransform.parent = null;
                this.CallLerp(heartToMeterDuration, f =>
                {
                    cacheTransform.position = Vector3.Lerp(beginPos, target.position, f.EaseInQuadratic());
                }, () =>
                {
                    smallHeart.SetActive(true);
                    this.CallWaitForSeconds(.5f, () =>
                    {
                        callback?.Invoke();
                        Destroy(bigHeart);
                        Destroy(this);
                    });
                });
            });
        });
    }
}
