using System;
using System.Text;
using UniRx;
using UnityEngine;
using UnityEngine.UI;

public class HarvestedCounterView : MonoBehaviour
{
    private static readonly float triggerThrottle = .1f;
    private static readonly int Add = Animator.StringToHash("Add");
    [SerializeField] private Animator counterAnimator;
    [SerializeField] private TextWithCountUpInt counter;
    private static readonly string hasCountFormat = "{0}";
    private static readonly float duration = .5f;
    private readonly Subject<Unit> playEffectObserve = new Subject<Unit>();

    private void Start()
    {
        playEffectObserve.AddTo(this);
        playEffectObserve
            .ThrottleFirst(TimeSpan.FromSeconds(triggerThrottle))
            .Subscribe(_ =>
            {
                counterAnimator.SetTrigger(Add);
            }).AddTo(this);
    }

    public void Initialize(int count)
    {
        counter.ChangeValue(hasCountFormat, count);
    }

    public void SetHarvestedCount(int count)
    {
        counter.CountUpAnimation(hasCountFormat, count, duration);
    }

    public void PlayEffect()
    {
        playEffectObserve.OnNext(default);
    }
}
