extends Control
class_name SettingsUI

signal back_button_pressed

@onready var slider_master_volume: HSlider = $VBoxContainer/HSliderMasterVolume
@onready var slider_music_volume: HSlider = $VBoxContainer/HSliderMusicVolume
@onready var slider_sound_effects_volume: HSlider = $VBoxContainer/HSliderSoundEffectsVolume
@onready var window_mode_option_button: OptionButton = $VBoxContainer/OptionButtonWindowMode


func _ready():
	self.hide()
	_update_ui()

## Update UI elements to reflect current settngs
func _update_ui():
	slider_master_volume.value = SettingsManager.settings.volume_master
	slider_music_volume.value = SettingsManager.settings.volume_music
	slider_sound_effects_volume.value = SettingsManager.settings.volume_sound_effects
	window_mode_option_button.select(window_mode_option_button.get_item_index(SettingsManager.settings.window_mode))

func _process(_delta):
	if not self.visible:
		return
	
	if Input.is_action_just_pressed("pause"):
		# Go back from settings
		_on_button_back_pressed()

func _on_button_back_pressed():
	self.hide()
	back_button_pressed.emit()


func _on_h_slider_master_volume_value_changed(value):
	SettingsManager.settings.volume_master = int(value)

func _on_h_slider_music_volume_value_changed(value):
	SettingsManager.settings.volume_music = int(value)

func _on_h_slider_sound_effects_volume_value_changed(value):
	SettingsManager.settings.volume_sound_effects = int(value)

func _on_option_button_window_mode_item_selected(index):
	var id: int = window_mode_option_button.get_item_id(index)
	var mode: DisplayServer.WindowMode = (id as DisplayServer.WindowMode)
	(SettingsManager.settings as Settings).window_mode = mode
