using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerLifeUI : MonoBehaviour {

    public Player player;

    public Sprite slotFull;
    public Sprite slotEmpty;

    public Image [] slots;

    int slotsActive = 0;

    public void OnEnable() {
        UpdateSlotsAmount();
    }

    int healthBuf;

    public void Update() {
        if(player != null) {
            healthBuf = Mathf.Clamp(Mathf.CeilToInt(player.stats.health), 0, slots.Length);
            
            for(int i = 0; i < slotsActive; i++) {
                slots[i].sprite = slotEmpty;
            }

            for(int i = 0; i < healthBuf; i++) {
                slots[i].sprite = slotFull;
            }
        }
    }

    void UpdateSlotsAmount() {
        EntityStats stats = player.stats;
        slotsActive = Mathf.Clamp((int)stats.maxHealth, 0, slots.Length);

        for(int i = 0; i < slots.Length; i++) {
            slots[i].gameObject.SetActive(false);
        }

        for(int i = 0; i < slotsActive; i++) {
            slots[i].gameObject.SetActive(true);
        }
    }
}
