extends CanvasLayer

@export var _combo_timer: Timer
@export var _combo_label: RichTextLabel

var _id: int
var _last_combo_time: float

func _process(_delta: float) -> void:
	var blend := Color(0.2, 0.2, 0.2, (Time.get_ticks_msec() - _last_combo_time) / 3000)
	var color := Color.GOLD.blend(blend)
	_combo_label.add_theme_color_override("default_color", color)
	_combo_label.self_modulate = Color(1, 1, 1,
		clamp(1 - (Time.get_ticks_msec() - _last_combo_time) / 3000, 0, 1))


func _ready() -> void:
	_id = multiplayer.get_unique_id()
	GameState.fetch().player_data[_id].water_combo_update.connect(
		func(current: int):
			if current == 0:
				_combo_label.hide()
			else:
				_combo_label.show()
				_combo_label.text = "+" + str(current) + "!"
				_last_combo_time = Time.get_ticks_msec()
			_combo_timer.start()
	)


func _on_combo_timer_timeout() -> void:
	GameState.fetch().player_data[_id].reset_water_combo()
	_combo_label.hide()
