using UnityEngine;
using UnityEngine.Events;

namespace WS
{
    [ObjectSystem]
    public class AnimationComponentAwakeSystem : AwakeSystem<AnimationComponent, Animation>
    {
        public override void Awake(AnimationComponent self, Animation a)
        {
            self.Awake(a);
        }
    }

    [ObjectSystem]
    public class AnimationComponentUpdateSystem : UpdateSystem<AnimationComponent>
    {
        public override void Update(AnimationComponent self)
        {
            self.Update();
        }
    }

    ///<summary>判断动画播放完成</summary>
    public class AnimationComponent : WSComponent
    {
        ///<summary>动画组件</summary>
        private Animation Anim;
        ///<summary>回调</summary>
        private UnityAction OnCallback;
        ///<summary>播放</summary>
        private bool isplay;

        public void Awake(Animation anim)
        {
            Anim = anim;
        }

        /// <summary>播放动画</summary>
        /// <param name="name">名称</param>
        /// <param name="action">回调</param>
        public void PlayAnim(string name, UnityAction action)
        {
            Anim.Play(name);
            OnCallback = action;
            isplay = true;
        }

        public void Update()
        {
            if (Anim != null)
            {
                if (isplay)
                {
                    if (!Anim.isPlaying)
                    {
                        OnCallback?.Invoke();
                        isplay = false;
                    }
                }
            }
        }

        public override void Dispose()
        {
            base.Dispose();
            Anim = null;
            OnCallback = null;
        }
    }
}