extends CanvasLayer

var sfx_preview = preload("res://assets/music&sfx/sfx/hit2.wav")

func _ready():
	# Music Slider
	$VBoxContainer/HBoxContainer/HSlider.min_value = 0.0
	$VBoxContainer/HBoxContainer/HSlider.max_value = 1.0
	$VBoxContainer/HBoxContainer/HSlider.step = 0.01
	# Aktuellen Bus-Wert als Startwert setzen
	var bus_idx = AudioServer.get_bus_index("Music")
	$VBoxContainer/HBoxContainer/HSlider.value = db_to_linear(AudioServer.get_bus_volume_db(bus_idx))
	
	 # SFX Slider
	$VBoxContainer/HBoxContainer3/HSlider.min_value = 0.0
	$VBoxContainer/HBoxContainer3/HSlider.max_value = 1.0
	$VBoxContainer/HBoxContainer3/HSlider.step = 0.01
	# Aktuellen Bus-Wert als Startwert setzen
	var sfx_idx = AudioServer.get_bus_index("SFX")
	$VBoxContainer/HBoxContainer3/HSlider.value = db_to_linear(AudioServer.get_bus_volume_db(sfx_idx))

func _on_back_button_pressed() -> void:
	get_tree().change_scene_to_file("res://scenes/mainmenu.tscn")

func _on_check_box_toggled(toggled_on: bool) -> void:
		if toggled_on:
			DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
		else:
			DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)

func _on_h_slider_value_changed(value: float) -> void:
	var bus_idx = AudioServer.get_bus_index("Music")
	AudioServer.set_bus_volume_db(bus_idx, linear_to_db(value))


func _on_h2_slider_value_changed(value: float) -> void:
	var bus_idx = AudioServer.get_bus_index("SFX")
	AudioServer.set_bus_volume_db(bus_idx, linear_to_db(value))
	
	# Vorschau-Sound abspielen
	var preview = AudioStreamPlayer.new()
	add_child(preview)
	preview.stream = sfx_preview
	preview.bus = "SFX"
	preview.play()
	await preview.finished
	preview.queue_free()
