extends Control

var resolutions = [
	Vector2i(640, 360),
	Vector2i(800, 450),
	Vector2i(1152, 648),
	Vector2i(1280, 720),
	Vector2i(1366, 768),
	Vector2i(1600, 900),
	Vector2i(1920, 1080),
	Vector2i(2560, 1440),
	Vector2i(3840, 2160)
]

# Adjust these paths as needed to match your scene
@onready var resolution_button: OptionButton = $"TabContainer/Screen & Video/VBoxContainer/HBoxContainer/OptionButton"
@onready var display_mode: OptionButton = $"TabContainer/Screen & Video/VBoxContainer/HBoxContainer2/OptionButton"

@onready var master_volume: HSlider = $"TabContainer/Music & Sound/VBoxContainer/HBoxContainer/HSlider"
@onready var music_volume: HSlider = $"TabContainer/Music & Sound/VBoxContainer/HBoxContainer2/HSlider"
@onready var sfx_volume: HSlider = $"TabContainer/Music & Sound/VBoxContainer/HBoxContainer3/HSlider"



func _ready():
	# Populate resolution selector
	for res in resolutions:
		resolution_button.add_item("%d x %d" % [res.x, res.y])

	# Populate display mode selector
	display_mode.clear()
	display_mode.add_item("Windowed")
	display_mode.add_item("Fullscreen")
	display_mode.add_item("Borderless")

	# Connect signals (editor: also connect in "Node" panel for best reliability)
	resolution_button.connect("item_selected", Callable(self, "_on_resolution_button_item_selected"))
	display_mode.connect("item_selected", Callable(self, "_on_display_mode_selected"))

	# Set defaults (optional)
	display_mode.select(0)
	resolution_button.select(2)
	resolution_button.disabled = false
	load_settings()
	
func _on_display_mode_selected(index):
	var mode_name = display_mode.get_item_text(index)
	match mode_name:
		"Windowed":
			DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
			resolution_button.disabled = false
			# Reapply current resolution
			_set_screen_size(resolutions[resolution_button.selected])
		"Fullscreen":
			DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
			resolution_button.disabled = true
		"Borderless":
			DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_EXCLUSIVE_FULLSCREEN)
			resolution_button.disabled = false
			# Optional: some platforms need actual borderless flag set
			# DisplayServer.window_set_flag(DisplayServer.WINDOW_FLAG_BORDERLESS, true)
			_set_screen_size(resolutions[resolution_button.selected])

func _on_resolution_button_item_selected(index):
	# Only acts if mode is not Fullscreen
	if display_mode.get_selected_id() != 1:
		_set_screen_size(resolutions[index])

func _set_screen_size(size: Vector2i):
	# Only relevant for windowed/borderless
	if display_mode.get_selected_id() != 1: # 1 is "Fullscreen"
		DisplayServer.window_set_size(size)
		# Optionally center or reposition window if desired

func save_settings():
	var config = ConfigFile.new()
		
	# Save resolution details
	var res = resolutions[resolution_button.selected]
	config.set_value("screen", "width", res.x)
	config.set_value("screen", "height", res.y)
		
	# Save display mode
	config.set_value("screen", "mode", display_mode.get_selected_id())  # 0=Windowed, 1=Fullscreen, 2=Borderless
		
	# Save music module volumes
	config.set_value("audio", "master_volume", master_volume.value)
	config.set_value("audio", "music_volume", music_volume.value)
	config.set_value("audio", "sfx_volume", sfx_volume.value)
		
	var err = config.save("user://settings.cfg")

func load_settings():
	var config = ConfigFile.new()
	var err = config.load("user://settings.cfg")
	
	if err == OK:
		# Load resolution
		var width = config.get_value("screen", "width", -1)
		var height = config.get_value("screen", "height", -1)
		if width != -1 and height != -1:
			for i in range(resolutions.size()):
				if resolutions[i].x == width and resolutions[i].y == height:
					resolution_button.select(i)
					break
		
		# Load display mode and apply it
		var mode = config.get_value("screen", "mode", 0)
		display_mode.select(mode)
		_on_display_mode_selected(mode) # apply display mode and resolution
		
		# Load audio volumes
		master_volume.value = config.get_value("audio", "master_volume", master_volume.value)
		music_volume.value = config.get_value("audio", "music_volume", music_volume.value)
		sfx_volume.value = config.get_value("audio", "sfx_volume", sfx_volume.value)
	else:
		print("No previous settings found or failed to load.")

func _on_button_pressed() -> void:
	save_settings()
	get_tree().change_scene_to_file("res://Game/Menu/main.tscn")
