using Spine.Unity;
using UnityEngine;

namespace Assets.Source.Content.UI.Components
{
    public class SkeletonGraphicPlayOnEnable : MonoBehaviour
    {
        public SkeletonGraphic SkeletonGraphic;
        public string AnimationName;
        public bool Loop;
        public bool FixFirstFrame = true;

        void Reset()
        {
            SkeletonGraphic = GetComponent<SkeletonGraphic>();
        }

        void OnEnable()
        {
            SkeletonGraphic.AnimationState.SetAnimation(0, AnimationName, Loop);
            if (FixFirstFrame)
            {
                var tempColor = SkeletonGraphic.canvasRenderer.GetColor();
                SkeletonGraphic.canvasRenderer.SetColor(Color.white.SetAlpha(0));
                this.NextFixedUpdate(() => SkeletonGraphic.canvasRenderer.SetColor(tempColor));
            }
        }
    }
}
