class_name GameSettingsType extends Node
## Autoloaded container for project settings, for quick runtime access.

var version: String

var settings_file: String
var audio_bus_file: String

var mouse_sensitivity_x: float
var mouse_sensitivity_y: float
var mouse_acceleration: float
var invert_pitch: bool

var enable_screen_shake: bool
var enable_head_bob: bool


func _init() -> void:
	ProjectSettings.settings_changed.connect(_read_settings)
	_read_settings()
	_load_audio_bus_override()


func _read_settings() -> void:
	version = ProjectSettings.get_setting("application/config/version")

	settings_file = ProjectSettings.get_setting("application/config/project_settings_override")
	audio_bus_file = ProjectSettings.get_setting("game/audio/buses/override_bus_layout")

	mouse_sensitivity_x = ProjectSettings.get_setting("game/config/input/mouse_sensitivity_x")
	mouse_sensitivity_y = ProjectSettings.get_setting("game/config/input/mouse_sensitivity_y")
	mouse_acceleration = ProjectSettings.get_setting("game/config/input/mouse_acceleration")
	invert_pitch = ProjectSettings.get_setting("game/config/input/invert_pitch")

	enable_screen_shake = ProjectSettings.get_setting(
		"game/config/accessibility/enable_screen_shake"
	)
	enable_head_bob = ProjectSettings.get_setting("game/config/accessibility/enable_head_bob")


func _load_audio_bus_override() -> void:
	# Load override audio bus file
	if FileAccess.file_exists(audio_bus_file):
		print_debug("Loading audio bus override from ", audio_bus_file)
		var bus_layout: AudioBusLayout = load(audio_bus_file)
		AudioServer.set_bus_layout(bus_layout)


func write() -> void:
	print_debug("Writing settings to ", settings_file)
	ProjectSettings.save_custom(settings_file)
	print_debug("Writing audio bus levels to ", audio_bus_file)
	ResourceSaver.save(AudioServer.generate_bus_layout(), audio_bus_file)
