extends CanvasLayer

var player_ref: CharacterBody2D = null
var enemy_ref: CharacterBody2D = null

var bar_player: ProgressBar
var bar_enemy: ProgressBar
var label_player: Label
var label_enemy: Label
var timer_label: Label

func _ready() -> void:
	# lai nebūtu “Parent node is busy … add_child/remove_child”
	call_deferred("_build_or_fix_ui")

func set_fighters(p: CharacterBody2D, e: CharacterBody2D) -> void:
	player_ref = p
	enemy_ref = e
	update_bars()

func set_time(match_time: float) -> void:
	if timer_label == null:
		return
	var time_sec: int = int(match_time)
	var mm: int = time_sec / 60
	var ss: int = time_sec % 60
	timer_label.text = "%02d:%02d" % [mm, ss]

func update_bars() -> void:
	if bar_player == null or bar_enemy == null:
		return

	if player_ref != null:
		var p_hp: int = int(player_ref.get("health"))
		var p_max: int = max(1, int(player_ref.get("max_health")))
		var p_pct: int = int(round(float(p_hp) / float(p_max) * 100.0))
		bar_player.value = clamp(p_pct, 0, 100)
		if label_player != null:
			label_player.text = "Player: %d%%" % bar_player.value

	if enemy_ref != null:
		var e_hp: int = int(enemy_ref.get("health"))
		var e_max: int = max(1, int(enemy_ref.get("max_health")))
		var e_pct: int = int(round(float(e_hp) / float(e_max) * 100.0))
		bar_enemy.value = clamp(e_pct, 0, 100)
		if label_enemy != null:
			label_enemy.text = "Enemy: %d%%" % bar_enemy.value

func get_player_hp_percent() -> int:
	if bar_player == null:
		return 0
	return int(bar_player.value)

func get_enemy_hp_percent() -> int:
	if bar_enemy == null:
		return 0
	return int(bar_enemy.value)

# -------------------------
# UI BUILD / FIX
# -------------------------
func _build_or_fix_ui() -> void:

	var vp: Vector2 = get_viewport().get_visible_rect().size

	# Timer
	timer_label = _ensure_label("TimerLabel", Vector2(0, 10), Vector2(vp.x, 40), 26, HORIZONTAL_ALIGNMENT_CENTER)

	# Player bar + text
	bar_player = _ensure_progress_bar("HealthBarPlayer", Vector2(40, 80), Vector2(420, 22))
	label_player = _ensure_label("HealthTextPlayer", Vector2(40, 105), Vector2(420, 22), 16, HORIZONTAL_ALIGNMENT_LEFT)

	# Enemy bar + text
	bar_enemy = _ensure_progress_bar("HealthBarEnemy", Vector2(vp.x - 40.0 - 420.0, 80), Vector2(420, 22))
	label_enemy = _ensure_label("HealthTextEnemy", Vector2(vp.x - 40.0 - 420.0, 105), Vector2(420, 22), 16, HORIZONTAL_ALIGNMENT_LEFT)

	# default values
	bar_player.min_value = 0
	bar_player.max_value = 100
	bar_player.value = 100
	bar_enemy.min_value = 0
	bar_enemy.max_value = 100
	bar_enemy.value = 100

	label_player.text = "Player: 100%"
	label_enemy.text = "Enemy: 100%"

func _ensure_progress_bar(n: String, pos: Vector2, size: Vector2) -> ProgressBar:
	var existing := get_node_or_null(n)
	if existing != null and existing is ProgressBar:
		var pb: ProgressBar = existing
		pb.position = pos
		pb.size = size
		return pb

	if existing != null and existing is TextureProgressBar:
		existing.visible = false

	var pb2 := ProgressBar.new()
	pb2.name = n
	pb2.position = pos
	pb2.size = size
	add_child(pb2)
	return pb2

func _ensure_label(n: String, pos: Vector2, size: Vector2, font_size: int, align: int) -> Label:
	var existing := get_node_or_null(n)
	if existing != null and existing is Label:
		var l: Label = existing
		l.position = pos
		l.size = size
		l.horizontal_alignment = align
		l.add_theme_font_size_override("font_size", font_size)
		return l

	var l2 := Label.new()
	l2.name = n
	l2.position = pos
	l2.size = size
	l2.horizontal_alignment = align
	l2.add_theme_font_size_override("font_size", font_size)
	add_child(l2)
	return l2
