using KitsuneCafe.Sys.Attributes;
using R3;
using UnityEngine;

namespace KitsuneCafe.Entities
{
    public class DummyAnimator : MonoBehaviour
    {
        [SerializeField]
        private Health health;

        [SerializeField]
        private Animator animator;

        [SerializeField, AnimatorParam("animator")]
        private int hitParam;

        [SerializeField, AnimatorParam("animator")]
        private int deathParam;

        private void OnValidate()
        {
            if (animator == null)
            {
                animator = GetComponent<Animator>();
            }

            if (health == null)
            {
                health = GetComponent<Health>();
            }
        }


        private void Start()
        {
            Observable.FromEventHandler<HealthChanged>(
                e => health.HealthChanged += e,
                e => health.HealthChanged -= e
            )
                .Select(ev => ev.e)
                .Subscribe(e =>
                {
                    if (e.Current > 0)
                    {
                        animator.SetBool(hitParam, true);
                    }
                    else
                    {
                        animator.SetBool(deathParam, true);
                    }
                })
                .AddTo(this);
        }
    }
}
