extends Node

const SAVE_PATH: String = "user://saves/"
const SAVE_FILENAME: String = "saved_game.dat"
const SAVE_FILE: String = SAVE_PATH + SAVE_FILENAME

var persistent_data: Dictionary[StringName, Variant] = {
	&"spawn_id": &"",
	&"sequence_index": 0,
	&"chapter_index": 0,
	&"level": &"",
	&"level_data": {

	}
}

func save_game() -> void:
	DirAccess.make_dir_recursive_absolute(SAVE_PATH)

	var file := FileAccess.open(SAVE_FILE, FileAccess.WRITE)

	# Store the variables.
	file.store_var(persistent_data)


func load_game() -> bool:
	if not save_exists():
		return false

	var file := FileAccess.open(SAVE_FILE, FileAccess.READ)

	# Load the variables.
	var loaded_data: Variant = file.get_var()

	if loaded_data is Dictionary:
		persistent_data = loaded_data
		return true

	return false


func delete_save() -> void:
	DirAccess.remove_absolute(SAVE_FILE)


func save_exists() -> bool:
	return FileAccess.file_exists(SAVE_FILE)
