using System;
using UniRx;
using UniRx.Triggers;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

public class CornMissMeter : MonoBehaviour
{
    [SerializeField] private Image missGauge;
    private static readonly float minValue = 0.08f;
    private static readonly float totalValue = 1 - minValue;
    private float prevValue;
    private float nextValue;
    private float elapsed;
    private float duration = .3f;
    private void Start()
    {
        ResetMeter();
        this.UpdateAsObservable().Subscribe(x =>
        {
            missGauge.fillAmount = minValue + Mathf.Lerp(prevValue, nextValue, elapsed.EaseOutQuadratic()) * totalValue;
            elapsed = Mathf.Clamp01(elapsed + Time.deltaTime / duration);
        }).AddTo(this);
    }

    public void SetMeter(float value, bool animated = true)
    {
        
        nextValue = value;
        prevValue = (missGauge.fillAmount - minValue) / totalValue;
        elapsed = animated ? 0f : 1f;
    }

    public void ResetMeter()
    {
        missGauge.fillAmount = minValue;
        SetMeter(0f, false);
    }
}