using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// Manages the health bar UI for the player.
/// Attach to the health bar fill image in the UI.
/// </summary>
public class HealthBarController : MonoBehaviour
{
    [Header("Health Bar")]
    [Tooltip("The fill image that shows current health")]
    public Image healthBarFill;
    
    [Tooltip("Background image behind the health bar")]
    public Image healthBarBackground;
    
    [Tooltip("Text showing current HP numbers")]
    public Text healthText;

    [Header("Colors")]
    [Tooltip("Color when health is high")]
    public Color healthyColor = Color.green;
    
    [Tooltip("Color when health is medium")]
    public Color mediumColor = Color.yellow;
    
    [Tooltip("Color when health is low")]
    public Color lowColor = Color.red;

    [Header("Damage Effect")]
    [Tooltip("Flash white when taking damage")]
    public bool flashOnDamage = true;
    
    [Tooltip("Duration of damage flash")]
    public float flashDuration = 0.2f;

    private float currentHealthPercent;
    private float flashTimer;
    private bool isFlashing;

    void Start()
    {
        // Setup colors
        if (healthBarFill != null)
        {
            healthBarFill.color = healthyColor;
        }
        
        // Set initial text
        UpdateHealthText(100);
    }

    void Update()
    {
        HandleDamageFlash();
    }

    /// <summary>
    /// Update the health bar based on health percentage (0 to 1).
    /// </summary>
    public void UpdateHealthBar(float healthPercent)
    {
        currentHealthPercent = Mathf.Clamp01(healthPercent);
        
        if (healthBarFill != null)
        {
            healthBarFill.fillAmount = currentHealthPercent;
            
            // Update color based on health
            if (!isFlashing)
            {
                if (currentHealthPercent > 0.5f)
                {
                    healthBarFill.color = Color.Lerp(lowColor, healthyColor, (currentHealthPercent - 0.5f) * 2f);
                }
                else
                {
                    healthBarFill.color = Color.Lerp(lowColor, mediumColor, currentHealthPercent * 2f);
                }
            }
        }

        // Update text
        if (healthText != null)
        {
            int currentHP = Mathf.RoundToInt(currentHealthPercent * 100);
            UpdateHealthText(currentHP);
        }
    }

    void UpdateHealthText(int hp)
    {
        if (healthText != null)
        {
            healthText.text = $"HP: {hp}";
        }
    }

    /// <summary>
    /// Trigger a damage flash effect.
    /// </summary>
    public void TriggerDamageFlash()
    {
        if (flashOnDamage)
        {
            isFlashing = true;
            flashTimer = flashDuration;
            
            if (healthBarFill != null)
            {
                healthBarFill.color = Color.white;
            }
        }
    }

    void HandleDamageFlash()
    {
        if (isFlashing)
        {
            flashTimer -= Time.deltaTime;
            
            if (flashTimer <= 0)
            {
                isFlashing = false;
                UpdateHealthBar(currentHealthPercent);
            }
        }
    }
}