class_name DamageDisplay
extends Control

## DamageDisplay - Shows player damage counters

# Display settings
const MAX_DAMAGE: int = 7
const MARKER_SIZE: float = 30.0
const MARKER_SPACING: float = 5.0

# UI elements
var markers: Array[ColorRect] = []
var damage_label: Label

# Current damage
var current_damage: int = 0

func _ready() -> void:
	_create_display()

func _create_display() -> void:
	# Container for markers
	var marker_container = HBoxContainer.new()
	add_child(marker_container)
	marker_container.add_theme_constant_override("separation", int(MARKER_SPACING))

	# Create damage markers
	for i in range(MAX_DAMAGE):
		var marker = ColorRect.new()
		marker.custom_minimum_size = Vector2(MARKER_SIZE, MARKER_SIZE)
		marker.color = Color(0.3, 0.3, 0.3)  # Empty state
		marker_container.add_child(marker)
		markers.append(marker)

	# Damage count label
	damage_label = Label.new()
	damage_label.text = "0 / 7"
	damage_label.position = Vector2(0, MARKER_SIZE + 5)
	damage_label.add_theme_font_size_override("font_size", 14)
	add_child(damage_label)

## Update damage display
func set_damage(amount: int) -> void:
	current_damage = clampi(amount, 0, MAX_DAMAGE)

	# Update markers
	for i in range(MAX_DAMAGE):
		if i < current_damage:
			markers[i].color = Color(0.8, 0.2, 0.2)  # Damage taken
		else:
			markers[i].color = Color(0.3, 0.3, 0.3)  # Empty

	# Update label
	damage_label.text = str(current_damage) + " / " + str(MAX_DAMAGE)

	# Flash on damage change
	if current_damage > 0:
		_flash_latest()

func _flash_latest() -> void:
	if current_damage <= 0 or current_damage > MAX_DAMAGE:
		return

	var marker = markers[current_damage - 1]
	var tween = create_tween()
	tween.tween_property(marker, "color", Color(1.0, 0.5, 0.5), 0.1)
	tween.tween_property(marker, "color", Color(0.8, 0.2, 0.2), 0.2)
