using UnityEngine;
using Utility.CTween;

public class CustomVectorTween : CustomTweener
{
    public CustomGetter<Vector2> Getter;
    public CustomSetter<Vector2> Setter;
    public Vector2 EndValue;
    private Vector2 StartValue;


    private GameObject _target;


    public CustomVectorTween CustomInit(CustomGetter<Vector2> getter, CustomSetter<Vector2> setter, Vector2 endValue, float duration, GameObject target)
    {
        _target = target;
        _tempTimer = 0;
        _curTime = 0;
        Duration = 0;

        _isPause = false;
        _isKilled = false;
        _isComplete = false;

        Duration = duration;
        Getter = getter;
        Setter = setter;
        EndValue = endValue;
        if (Getter != null)
        {
            StartValue = Getter();
        }

        Update();
        return this;
    }


    public override void Update()
    {
        if (_target == null)
        {
            Kill();
            return;
        }

        if (_isPause || _isKilled || _isComplete || Setter == null)
        {
            return;
        }

        _isPlaying = true;
        _curTime += Time.deltaTime;
        float rate = 1 / Duration; //相对速率

        _tempTimer += Time.deltaTime * rate;
        _tempTimer = Mathf.Clamp(_tempTimer, 0, 1);
        float v = AnimationCurve.Evaluate(_tempTimer);

        Vector2 pNewValue = Vector2.Lerp(StartValue, EndValue, v);


        Setter?.Invoke(pNewValue);
        OnUpdateAction?.Invoke();


        if (_curTime >= Duration)
        {
            if (IsLoop)
            {
                _tempTimer = 0;
                _curTime = 0;
                if (Getter != null)
                {
                    StartValue = Getter();
                }
            }
            else
            {
                OnCompleteAction?.Invoke();
                _isPlaying = false;
                _isComplete = true;
                if (AutoDispose)
                {
                    Kill();
                }
            }
        }
    }


    // public void ReStart(Vector2 endValue, float duration)
    // {
    //     _tempTimer = 0;
    //     _curTime = 0;
    //     Duration = 0;
    //
    //     _isPause = false;
    //     _isKilled = false; 
    //     _isComplete = false;
    //
    //     EndValue = endValue;
    //     Duration = duration;
    //     CustomInit(Getter, Setter, EndValue, Duration,_target);
    // }

    // /// <summary>
    // /// 被杀死后补间将会被回收
    // /// </summary>
    // public async void Kill(bool isComplete = false)
    // {
    //     if (isComplete)
    //     {
    //         OnCompleteAction?.Invoke();
    //     }
    //
    //     _isKilled = true;
    //     Update();
    //     Dispose();
    // }

    public override void Dispose()
    {
      
        if (_target != null)
        {
            Setter?.Invoke(EndValue);
        }

        base.Dispose();
        _target = null;
        Getter = null;
        Setter = null;
        CustomTweenManager.VectorRecycle(this);
    }
}