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

namespace FL.Battle.Components.Actions
{
    /// <summary>
    /// MoveLineAction的参数
    /// </summary>
    public struct MoveActionArgs
    {
        public Vector3 from;
        public Vector3 to;
        /// <summary>
        /// 单位:毫秒
        /// </summary>
        public int duration;
        /// <summary>
        /// 延迟，单位: 毫秒
        /// </summary>
        public int delay;
        public Ease ease;
        public Action<Vector3> onUpdate;
        public Action onComplete;
    }
    /// <summary>
    /// 直线移动
    /// Dotween.DOMove
    /// </summary>
    public class MoveLineAction : 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;
            _tween = transform.DOMove(args.to, args.duration * 0.001f);
            if (args.delay > 0)
            {
                _tween.SetDelay(args.delay * 0.001f);
            }
            _tween.SetEase(args.ease == Ease.Unset ? Ease.Linear : args.ease);
            if (args.onUpdate != null)
            {
                _tween.onUpdate += () =>
                {
                    args.onUpdate.SafeInvoke(transform.position);
                };
            }
            _tween.onComplete += () =>
            {
                var callback = args.onComplete;
                _tween = null;
                args = default;
                callback.SafeInvoke();
            };
        }
    }
}
