using System;
using UnityEngine;

namespace Utility.CTween
{
    public class CustomTweener
    {
        public AnimationCurve AnimationCurve = CustomTweenManager.AnimationCurveLibrary.Line;
        public Action OnCompleteAction;
        public Action OnUpdateAction; 
        public Action<object> OnUpdateAndValueAction;
        public Action OnDestroyAction;

        public float Duration;


        protected bool _isKilled;
        protected bool _isPause;
        protected bool _isComplete;
        protected bool _isPlaying;
        protected float _tempTimer;
        
        
        public bool IsLoop;
        public bool IsPlaying
        {
            get => _isPlaying;
        }

        protected float _curTime;

        /// <summary>
        /// 完成后是否自动销毁
        /// </summary>
        public bool AutoDispose = true;

        public void Pause()
        {
            _isPlaying = false;
            _isPause = true;
        }
        
        public void Play()
        {
            _isPlaying = true;
            _isPause = false;
        }

        public void Start()
        {
            _isPause = true;
        }

        public virtual void Update()
        {
        }

        /// <summary>
        /// 被杀死后补间将会被回收
        /// </summary>
        public async void Kill(bool isComplete = false)
        {
            if (_isKilled)
            {
                return;
            }

            if (isComplete)
            {
                OnCompleteAction?.Invoke();
            }

            _isKilled = true;
            Dispose();
        }

        public virtual void Dispose()
        {
            _tempTimer = 0;
            _curTime = 0;
            Duration = 0;

            _isPause = false;
            _isComplete = false;

            IsLoop = false;
            AutoDispose = true;
            OnCompleteAction = null;
            OnUpdateAction = null;
            OnUpdateAndValueAction = null;
        }
    }

    public delegate T CustomGetter<out T>();


    public delegate void CustomSetter<in T>(T pNewValue);
}