using DG.Tweening;
using System;
using UnityEngine;
using XGame.Framework.Interfaces;

namespace FL.Battle.Components.Actions
{
    /// <summary>
    /// 抛物线移动
    /// 中心点位置固定的贝塞尔曲线
    /// </summary>
    internal class MoveParabolaAction : IAction
    {
        public MoveActionArgs args;
        private Tween _tween;
        void IReference.Clear()
        {
            args = default;
            _tween?.Kill();
            _tween = null;
        }

        public void Start(Transform transform)
        {
            transform.position = args.from;
            var totalTime = args.duration * 0.001f;
            var center = Vector3.Lerp(args.from, args.to, 0.5f);
            // z轴向相机偏移
            center.z -= 8;
            _tween = DOTween.To(() => 0, (t) =>
            {
                var tNormal = t / totalTime;
                var pos = args.from.BezierPoint(center, args.to, tNormal);
                transform.position = pos;
                args.onUpdate.SafeInvoke(pos);
            }, totalTime, totalTime);
            if (args.delay > 0)
            {
                _tween.SetDelay(args.delay * 0.001f);
            }
            _tween.SetEase(args.ease == Ease.Unset ? Ease.OutQuad : args.ease);
            _tween.onComplete += () =>
            {
                var callback = args.onComplete;
                _tween = null;
                args = default;
                callback.SafeInvoke();
            };


        }
    }
}
