extends Node


signal levelup
signal start


var maxspeed = 260
var speed = 100
var score = 0
var active_biom
var gamerunning = false

var cached_save

func reset_game():
	speed = 100
	score = 0
	active_biom = "n"
	gamerunning = false
	_ready()
	$"/root/Transition".fade_out()
	await $"/root/Transition".finish
	get_tree().reload_current_scene()


var data : FileAccess
func load_save() -> Dictionary:
	
	if cached_save is Dictionary:
		return cached_save
	# wenn kein save vorhanden ist, erstelle eins
	if (not FileAccess.file_exists("user://save.json")) or (FileAccess.open("user://save.json", FileAccess.READ_WRITE).get_as_text() == ""):
		init_save()

	data = FileAccess.open("user://save.json", FileAccess.READ)
	var content_text: Dictionary = JSON.parse_string(data.get_as_text())

	if content_text:
		print("loaded: ",content_text)
		return content_text
	else:
		print("Failed to parse JSON")
		return {}


func write_save():
	var sdata : FileAccess
	sdata = FileAccess.open("user://save.json",FileAccess.WRITE)
	var content = JSON.stringify(
			{
				"hiscore":$/root/Game/Gui/ScoreContainer.get_score(),
				"color":$/root/Game/Screen/Duck.get_duck_color()
			}
		)
	print("saved: ",content)
	sdata.store_string(content)

func init_save():
	data = FileAccess.open("user://save.json", FileAccess.WRITE)
	data.store_string(FileAccess.open("res://code/first_save.json", FileAccess.READ).get_as_text())
	data.close()
