using UnityEngine;
using UnityEngine.UI; // Add this line to use Unity's UI components

public class StaminaSystem : MonoBehaviour
{
    public float maxStamina;
    public float currentStamina;
    public float staminaRegenerationRate;

    //public Text staminaText; // Reference to the UI Text component for displaying stamina
    //public Slider staminaSlider; // Reference to the UI Slider component for visualizing stamina
    public Image staminaCircle;

    private bool isExhausted = false;

    [SerializeField]
    ParticleSystem lowStaminaVFX;

    private void Start()
    {
        maxStamina = 100f;
        staminaRegenerationRate = 0.1f;
        currentStamina = maxStamina;

        // Check if lowStaminaVFX is assigned
        if (lowStaminaVFX == null)
        {
            DevLog.LogWarning("Low stamina VFX not assigned on " + gameObject.name);
        }
    }

    private void Update()
    {
        // CompareTag avoids string allocation per frame (was gameObject.tag == "...")
        if (gameObject.CompareTag("Player") && currentStamina < 99)
            ReplenishStamina(0.1f);
        if (gameObject.CompareTag("Enemy") && currentStamina < 99)
            ReplenishStamina(0.015f);
            
        // Check if lowStaminaVFX is assigned before using it
        if (lowStaminaVFX != null)
        {
            if (currentStamina / maxStamina > 0.1 && lowStaminaVFX.isPlaying)
            {
                lowStaminaVFX.Stop();
            }
        }
        
        if (!isExhausted)
        {
            UpdateStaminaUI(); // Update UI to reflect changes in stamina
        }
    }

    public void UseStamina(float amount)
    {
        //print("stamina used in: " + gameObject.tag);
        if (!isExhausted)
        {
            
            currentStamina -= amount;
            
            //currentStamina = Mathf.Clamp(currentStamina, 0f, maxStamina);

            if (currentStamina <= 0f)
            {
                currentStamina = 0;
                isExhausted = true;
                // Perform any actions when stamina is fully depleted (e.g., disable running)
                //disable attacks
                //disable running
            }

             // Check if lowStaminaVFX is assigned before using it
            if (lowStaminaVFX != null)
            {
                if (currentStamina/maxStamina < 0.1)
                {
                    lowStaminaVFX.Play();
                }
                else if(currentStamina/maxStamina > 0.1 && lowStaminaVFX.isPlaying)
                {
                    lowStaminaVFX.Stop();
                }
            }
            
            UpdateStaminaUI(); // Update UI to reflect changes in stamina
        }
    }

    public void ReplenishStamina(float amount)
    {
        currentStamina += amount;
        
        // Add a clamp to prevent stamina from exceeding maxStamina
        currentStamina = Mathf.Clamp(currentStamina, 0f, maxStamina);

        if (currentStamina > 0f)
        {
            isExhausted = false;
        }

        UpdateStaminaUI(); // Update UI to reflect changes in stamina
    }

    private void UpdateStaminaUI()
    {
        // Update the UI components to display the current stamina
        // Add null check for staminaCircle
        if (staminaCircle != null)
        {
            staminaCircle.fillAmount = currentStamina / maxStamina;
        }
        else
        {
            // Only log this once to avoid spamming the console
            if (Time.frameCount % 300 == 0) // Log approximately every 5 seconds at 60 FPS
            {
                DevLog.LogWarning("Stamina circle reference is null on " + gameObject.name);
            }
        }
    }


}
