extends Control
@onready var options_menu: Control = $"."

@onready var volume_slider: HSlider = $Volume
@onready var music: HSlider = $Music
@onready var sfx_vol: HSlider = $SFX_vol
@onready var mute_checkbox: CheckButton = $Mute
@onready var resolution_dropdown: OptionButton = $Resolution
@onready var fullscreen_checkbox: CheckButton = $Fullscreen
@onready var vol_percentage: Label = $Volume/vol_percentage
@onready var music_percentage: Label = $Music/music_percentage
@onready var sfx_percentage: Label = $SFX_vol/sfx_percentage


func _ready() -> void:
	# Update the UI with the loaded settings when the options menu is ready
	update_ui()

func update_ui() -> void:
	# Update UI elements to match the loaded settings
	if Save.game_data.has("volume"):
		volume_slider.value = Save.game_data["volume"]
	if Save.game_data.has("music"):
		music.value = Save.game_data["music"]
	if Save.game_data.has("sfx"):
		sfx_vol.value = Save.game_data["sfx"]
	if Save.game_data.has("mute"):
		mute_checkbox.button_pressed = Save.game_data["mute"]
	if Save.game_data.has("resolution"):
		resolution_dropdown.selected = Save.game_data["resolution"]
	if Save.game_data.has("fullscreen"):
		fullscreen_checkbox.button_pressed = Save.game_data["fullscreen"]

func _on_back_pressed() -> void:
	options_menu.visible = false

func _on_volume_value_changed(value: float) -> void:
	GlobalSettings.volume_value_changed(value)
	Save.game_data["volume"] = value  # Update the save data
	Save.save_data()  # Save the updated data
	update_volume_percentage_label(value)
	
func update_volume_percentage_label(slider_value: float) -> void:
	var percentage = int(slider_value)
	vol_percentage.text = str(percentage) + "%"
	
func _on_music_value_changed(value: float) -> void:
	GlobalSettings.music_value_changed(value)
	Save.game_data["music"] = value  # Update the save data
	Save.save_data()  # Save the updated data
	update_music_percentage_label(value)
	
func update_music_percentage_label(slider_value: float) -> void:	
	var percentage = int(slider_value)
	music_percentage.text = str(percentage) + "%"
	
func _on_sfx_vol_value_changed(value: float) -> void:
	GlobalSettings.sfx_value_changed(value)
	Save.game_data["sfx"] = value  # Update the save data
	Save.save_data()  # Save the updated data	
	update_sfx_percentage_label(value)

func update_sfx_percentage_label(slider_value: float) -> void:	
	var percentage = int(slider_value)
	sfx_percentage.text = str(percentage) + "%"
	
func _on_mute_toggled(toggled_on: bool) -> void:
	GlobalSettings.mute_toggled(toggled_on)
	Save.game_data["mute"] = toggled_on  # Update the save data
	Save.save_data()  # Save the updated data

func _on_resolution_item_selected(index: int) -> void:
	GlobalSettings.resolution_item_selected(index)
	Save.game_data["resolution"] = index  # Update the save data
	Save.save_data()  # Save the updated data

func _on_fullscreen_toggled(toggled_on: bool) -> void:
	GlobalSettings.toggle_fullscreen(toggled_on)
	Save.game_data["fullscreen"] = toggled_on  # Update the save data
	Save.save_data()  # Save the updated data
