extends Control
class_name ResourceBars
## Resource bars (Health, Stamina, etc.) displayed above the action bar

# Health bar references
@onready var health_bg: ColorRect = $VBoxContainer/HealthBar/Background
@onready var health_fill: ColorRect = $VBoxContainer/HealthBar/Background/Fill
@onready var health_text: Label = $VBoxContainer/HealthBar/HealthText

# Future: Stamina bar references
# @onready var stamina_bg: ColorRect = $VBoxContainer/StaminaBar/Background
# @onready var stamina_fill: ColorRect = $VBoxContainer/StaminaBar/Background/Fill

# Player reference
var player: Character = null

# Smoothing
var target_health_width: float = 0.0
var current_health_width: float = 0.0
const HEALTH_LERP_SPEED: float = 10.0

# Colors
const COLOR_HEALTH_HIGH = Color(0.0, 0.8, 0.0, 1.0)    # Green (>60%)
const COLOR_HEALTH_MID = Color(1.0, 0.8, 0.0, 1.0)     # Yellow (30-60%)
const COLOR_HEALTH_LOW = Color(0.8, 0.0, 0.0, 1.0)     # Red (<30%)

func _ready():
	# Initialize bars
	if health_fill:
		current_health_width = health_fill.size.x
		target_health_width = health_fill.size.x

	print("[ResourceBars] Resource bars initialized")

func _process(delta):
	# Smooth health bar animation
	if health_fill and abs(current_health_width - target_health_width) > 0.1:
		current_health_width = lerp(current_health_width, target_health_width, HEALTH_LERP_SPEED * delta)
		health_fill.size.x = current_health_width

## Set player reference
func set_player(p: Character):
	player = p
	if player:
		update_health(player.current_health, player.max_health)

## Update health bar display
func update_health(current: float, maximum: float):
	if not health_fill or not health_bg or not health_text:
		return

	var health_percent = current / maximum if maximum > 0 else 0.0
	health_percent = clamp(health_percent, 0.0, 1.0)

	# Update fill width
	var max_width = health_bg.size.x
	target_health_width = max_width * health_percent

	# Update color based on health percentage
	if health_percent > 0.6:
		health_fill.color = COLOR_HEALTH_HIGH
	elif health_percent > 0.3:
		health_fill.color = COLOR_HEALTH_MID
	else:
		health_fill.color = COLOR_HEALTH_LOW

	# Update text
	health_text.text = "%d / %d HP" % [int(current), int(maximum)]

## Update stamina bar (for future use)
func update_stamina(current: float, maximum: float):
	# TODO: Implement when stamina system is added
	pass
