extends MarginContainer
## Menu allowing the user to adjust game configuration.

const SETTINGS_GROUP := "Settings"
const SETTINGS_FILE := "override.cfg"

@onready var control_binding_list: VBoxContainer = %ControlBindingList


func _ready() -> void:
	populate_control_bindings()


func _get_settings_elements() -> Array[Setting]:
	var elements: Array[Setting] = []
	elements.assign(get_tree().get_nodes_in_group(SETTINGS_GROUP))
	return elements


func populate_control_bindings() -> void:
	InputMap.get
	for action: StringName in InputMap.get_actions():
		if not action.begins_with("ui_"):
			control_binding_list.add_child(ControlBinding.create(action))


## Close menu without applying settings.
func cancel() -> void:
	queue_free()


## Apply all settings.
func apply() -> void:
	for setting: Setting in _get_settings_elements():
		setting.apply()


## Write all applied settings to disk.
func save_settings() -> void:
	ProjectSettings.save_custom(SETTINGS_FILE)


## Apply settings and close menu.
func accept() -> void:
	apply()
	save_settings()
	queue_free()
