extends CanvasLayer

@onready var _oxygen_bar: ProgressBar = %OxygenBar
@onready var _health_bar: ProgressBar = %HealthBar
@onready var _hunger_bar: ProgressBar = %HungerBar


func _ready() -> void:
	_style_bar(_oxygen_bar, Color(0.31, 0.76, 0.97))
	_style_bar(_health_bar, Color(0.91, 0.30, 0.24))
	_style_bar(_hunger_bar, Color(0.95, 0.61, 0.07))


func _style_bar(bar: ProgressBar, color: Color) -> void:
	var style := StyleBoxFlat.new()
	style.bg_color = color
	style.corner_radius_top_left = 4
	style.corner_radius_top_right = 4
	style.corner_radius_bottom_left = 4
	style.corner_radius_bottom_right = 4
	bar.add_theme_stylebox_override("fill", style)

	var bg_style := StyleBoxFlat.new()
	bg_style.bg_color = Color(0.1, 0.1, 0.1, 0.6)
	bg_style.corner_radius_top_left = 4
	bg_style.corner_radius_top_right = 4
	bg_style.corner_radius_bottom_left = 4
	bg_style.corner_radius_bottom_right = 4
	bar.add_theme_stylebox_override("background", bg_style)


func connect_to_dolphin(dolphin: CharacterBody3D) -> void:
	dolphin.stats_changed.connect(_on_stats_changed)


func _on_stats_changed(oxygen: float, hp: float, hunger: float) -> void:
	_oxygen_bar.value = oxygen
	_health_bar.value = hp
	_hunger_bar.value = hunger
