extends Control
@onready var remnant_countdown: ProgressBar = $MarginContainer/HBoxContainer/RemnantCountdown
@onready var timeshift_countdown: ProgressBar = $MarginContainer/HBoxContainer/TimeshiftCountdown

func _ready() -> void:
	remnant_countdown.hide()
	timeshift_countdown.hide()
	SignalBus.setting_remnant_countdown_max.connect(set_remnant_countdown_max)
	SignalBus.changed_remnant_countdown_absolute.connect(change_remnant_countdown_absolute)
	SignalBus.setting_timeshift_countdown_max.connect(set_timeshift_countdown_max)
	SignalBus.changed_timeshift_countdown_absolute.connect(change_timeshift_countdown_absolute)
	SignalBus.set_anchor.connect(show_remnant_countdown)
	SignalBus.reload_anchor.connect(show_remnant_countdown)
	SignalBus.changed_time.connect(show_timeshift_countdown)
	SignalBus.player_died.connect(reset)

func reset():
	remnant_countdown.hide()
	timeshift_countdown.hide()
	
func set_remnant_countdown_max(countdown_max : float):
	remnant_countdown.max_value = countdown_max

func set_timeshift_countdown_max(countdown_max : float):
	timeshift_countdown.max_value = countdown_max

func change_remnant_countdown_absolute(countdown_new : float):
	if not remnant_countdown.visible:
		return
	if countdown_new <= 0:
		remnant_countdown.hide()
	else:
		remnant_countdown.value = countdown_new

func change_timeshift_countdown_absolute(countdown_new : float):
	if not timeshift_countdown.visible:
		return
	if countdown_new <= 0:
		timeshift_countdown.hide()
	else:
		timeshift_countdown.value = countdown_new

func show_remnant_countdown():
	remnant_countdown.value = remnant_countdown.max_value
	remnant_countdown.show()

func show_timeshift_countdown():
	if Globals.current_time == Utils.TIME.PRESENT:
		return
	timeshift_countdown.value = timeshift_countdown.max_value
	timeshift_countdown.show()
