using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class ExpBar : MonoBehaviour
{
    private Image _valueBar;
    private TMPro.TextMeshProUGUI _levelText; 
    
    
    private void Awake()
    {
        _valueBar = transform.Find("Stack/ImageStack/ValueBar").GetComponent<Image>();
        _levelText = transform.Find("Stack/Lv_Icon/Lv_BackGround/Lv_Text").GetComponent<TextMeshProUGUI>();
        
        RectTransform fillRect = _valueBar.rectTransform;
        fillRect.anchorMax = new Vector2(0, 1);
        _levelText.text = 1.ToString();
    }

    public void UpdateValue(int level, float currentExp, float maxExp)
    {
        if (_valueBar == null) return;

        float expRatio = 0f;
        if (maxExp > 0) expRatio = currentExp / maxExp;
        RectTransform fillRect = _valueBar.rectTransform;
        fillRect.anchorMax = new Vector2(expRatio, 1);
        
        if (_levelText != null)
            _levelText.text = level.ToString();
    }
}
