extends Control

@export_group("Node References")
@export var lose_panel : PanelContainer
@export var lose_label : Label
@export var reset_label : Label
@export var start_level : PanelContainer
@export var level_title : Label
@export var clock_progress : TextureProgressBar
@export var clock_label : Label
@export var volume_slider : HSlider
@export var level_name : Label
@export var pause_menu : MarginContainer

var title_fade : float = 2.5
var sound = AudioServer.get_bus_index("Master")

# Called when the node enters the scene tree for the first time.
func _ready():
	lose_panel.visible = false
	volume_slider.value = db_to_linear(AudioServer.get_bus_volume_db(sound))
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	title_fade -= delta
	start_level.modulate.a = clamp(title_fade,0,1)
	pass

func show_lose_screen(lose_text : String, reset_timer : float):
	lose_panel.visible = true
	if lose_text != "":
		lose_label.text = lose_text
	reset_label.text = "Resetting in: " + str(round(reset_timer*10)/10)

func hide_lose_screen():
	lose_panel.visible = false
	clock_progress.value = 0
	clock_label.text = str(0)
	pass

func show_level_title(title : String):
	level_title.text = title
	level_name.text = title
	title_fade = 2

func onTick(tick : int):
	clock_progress.value = tick
	if(tick == 11):
		clock_label.text = str(10)
	else:
		clock_label.text = str(tick)

func open_pause():
	pause_menu.show()

func close_pause():
	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
	pause_menu.hide()
	get_parent().process_mode = Node.PROCESS_MODE_INHERIT
	

func goto_menu():
	get_tree().change_scene_to_file(GameManager.menu_scene_path)


func _on_h_slider_value_changed(value: float) -> void:
	AudioServer.set_bus_volume_db(sound, linear_to_db(value))
	
