extends Control
@onready var back: Button = $CenterContainer/VBoxContainer2/Back
# Video Settings
@onready var fullscreen: CheckBox = $CenterContainer/VBoxContainer2/HBoxContainer2/Fullscreen
# Audio Settings
@onready var master_volume: HSlider = $"CenterContainer/VBoxContainer2/HBoxContainer/Volume Slider/MasterVolume"
@onready var music_volume: HSlider = $"CenterContainer/VBoxContainer2/HBoxContainer/Volume Slider/MusicVolume"
@onready var sfx_volume: HSlider = $"CenterContainer/VBoxContainer2/HBoxContainer/Volume Slider/SFXVolume"
@onready var master_volume_mute: CheckBox = $"CenterContainer/VBoxContainer2/HBoxContainer/Volume Mute/VBoxContainer/MasterVolumeMute"
@onready var music_volume_mute: CheckBox = $"CenterContainer/VBoxContainer2/HBoxContainer/Volume Mute/VBoxContainer/MusicVolumeMute"
@onready var sfx_volume_mute: CheckBox =$"CenterContainer/VBoxContainer2/HBoxContainer/Volume Mute/VBoxContainer/SFXVolumeMute"

# saving
func _ready():
	var video_settings = ConfigFileHandler.load_video_settings()
	fullscreen.button_pressed = video_settings.fullscreen
	var audio_settings = ConfigFileHandler.load_audio_settings()
	master_volume.value = min(audio_settings.master_volume, 1.0) * 100
	music_volume.value = min(audio_settings.music_volume, 1.0) * 100
	sfx_volume.value = min(audio_settings.sfx_volume, 1.0) * 100
	master_volume_mute.button_pressed = audio_settings.master_volume_mute
	music_volume_mute.button_pressed = audio_settings.music_volume_mute
	sfx_volume_mute.button_pressed = audio_settings.sfx_volume_mute
	visible = false

func _on_fullscreen_toggled(toggled_on: bool) -> void:
	ConfigFileHandler.save_video_setting("fullscreen", toggled_on)
	GlobalSettings.change_fullscreen(fullscreen.button_pressed)

func _on_master_volume_drag_ended(value_changed: bool) -> void:
	if value_changed:
		ConfigFileHandler.save_audio_setting("master_volume", master_volume.value/100)
		GlobalSettings.change_master_volume()
		
func _on_music_volume_drag_ended(value_changed: bool) -> void:
	if value_changed:
		ConfigFileHandler.save_audio_setting("music_volume", music_volume.value/100)
		GlobalSettings.change_music_volume()
		
func _on_sfx_volume_drag_ended(value_changed: bool) -> void:
	if value_changed:
		ConfigFileHandler.save_audio_setting("sfx_volume", sfx_volume.value/100)
		GlobalSettings.change_sfx_volume()
		
func _on_master_volume_mute_toggled(toggled_on: bool) -> void:
	ConfigFileHandler.save_audio_setting("master_volume_mute", toggled_on)
	GlobalSettings.mute_master(master_volume_mute.button_pressed)

func _on_music_volume_mute_toggled(toggled_on: bool) -> void:
	ConfigFileHandler.save_audio_setting("music_volume_mute", toggled_on)
	GlobalSettings.mute_music(music_volume_mute.button_pressed)

func _on_sfx_volume_mute_toggled(toggled_on: bool) -> void:
	ConfigFileHandler.save_audio_setting("sfx_volume_mute", toggled_on)
	GlobalSettings.mute_sfx(sfx_volume_mute.button_pressed)
# Settings
func _process(delta: float) -> void:
	check_Esc()
func check_Esc():
	if Input.is_action_just_pressed("back"):
		visible = false
func _on_back_pressed() -> void:
	visible = false
