extends Resource
class_name Savegame

var version: int = 1

signal savegame_changed

## Dictionary storing level_id and bool if level completed
var level_dict: Dictionary = {}:
	get:
		return level_dict
	set(value):
		level_dict = value
		savegame_changed.emit()

func to_dict():
	var data = {
		"version" : version,
	}
	for level_id in level_dict.keys():
		data[level_id] = (level_dict[level_id])
		
	return data

func apply_dict(data):
	if (data.version != version):
		print("Saved savegame version is ", data.version, ", but game uses version ", version, ".")
	
	print("Stored data.keys = ", len(data.keys()))
	var not_level_keys: Array = [ 
		"version",
	]
	
	for key in (data as Dictionary).keys():
		if not_level_keys.has(key):
			continue
			
		var level_id_int: int = int(key)
		var level_status: bool = bool(data[key])
		level_dict[level_id_int] = level_status
	
func level_status_changed():
	savegame_changed.emit()
