extends Node

signal scene_changed
signal player_just_died
enum States{
	INTRO,
	INTRO_CUTSCENE,
	TRANSITIONING,
	MAIN_MENU,
	GAME_OVER,
	CONTROLS,
	NEW_GAME,
	GAME,
	PUSE,
	STATS,
	CUTSCENE,
}
var _SAVE_FILE_PATH = "user://data"
# Change key in release
var _ENCRYPTION_KEY = "fas3uyf076HJsiUDs24dfI9"
var is_first_run = false 
var _SCENES := {
	States.MAIN_MENU: "res://src/Menu/MainMenu.tscn",
	States.NEW_GAME: "res://src/GameWorld/GameWorld.tscn",
	States.INTRO_CUTSCENE: "res://src/CutScenes/IntroCutscene.tscn"
}


var GAME_TIMEOUT_MIN = 10

var _default_data := {
		"statistics": {
			"deaths": 0,
			"clears": 0,
			"runs": 0,
			"total_time_played": 0,
			"orbs": 0,
			"coins": 0,
		},
		"heigh_score": {
			"deaths": 0,
			"score": 0,
			"cleard": false,
			"coins": 0,
			"orbs": 0,
			"time": 0,
		},
		"achivements": {
			"a": false,
			"b": false
		}
	}

var _data := _default_data

var _run_data:= {
	"deaths": 0,
	"coins": 0,
	"orbs": 0,
	"time": 0,
	"score": 0
}

var _state = States.INTRO

func _ready() -> void:
	load_data()
	for key in _data:
		print("[%s]: %s" % [key, _data[key]])
	
func _physics_process(delta: float) -> void:
	if _state == States.GAME:
		_run_data.time += delta
		_data.statistics.total_time_played += delta

func load_data():
	var data_file = File.new()
	var e = data_file.open_encrypted_with_pass(_SAVE_FILE_PATH, File.READ, _ENCRYPTION_KEY)
	if e != 0:
		data_file.close()
		is_first_run = true
		save_data()
	else:
		var saved_data:Dictionary = data_file.get_var()
		for key in saved_data.keys():
			if _data.has(key):
				if typeof(saved_data[key]) == TYPE_DICTIONARY:
					for sub_key in saved_data[key]:
						var subdict:Dictionary = _data[key];
						if subdict.has(sub_key): _data[key][sub_key] = saved_data[key][sub_key]
				else: _data[key] = saved_data[key]
		data_file.close()
	pass
	
func save_data():
	var data_file = File.new()
	data_file.open_encrypted_with_pass(_SAVE_FILE_PATH, File.WRITE, _ENCRYPTION_KEY)
	data_file.store_var(_data)
	data_file.close()
	pass

func start_new_game(from_main_menu:bool=false, change_music:bool=true):
	for key in _run_data.keys():
		_run_data[key] = 0
	var state = States.INTRO_CUTSCENE if from_main_menu else States.NEW_GAME
	if not from_main_menu: _data.statistics.runs += 1
	save_data()
	var delay = 3 if state == States.INTRO_CUTSCENE else 1
	if change_music: AudioManager.play_music(AudioManager.Music.PreGame, delay)
	_change_scene(state, delay)
	yield(self, "scene_changed")
#	get_tree().change_scene_to(load(_SCENES[_state]))


func set_state(state:int = States.GAME):
	_state = state
	
func get_heigh_score():
	return _data.heigh_score.duplicate()
func get_statistics():
	return _data.statistics.duplicate()
func get_run_data():
	return _run_data.duplicate()
func get_state():
	return int(_state)
func go_to_main_menu():
	save_data()
	_change_scene(States.MAIN_MENU)
	yield(self, "scene_changed")
	
func _change_scene(state:int,duration:float=1):
	_state = States.TRANSITIONING
	Stage.fade_out(duration/2)
	yield(Stage, "fade_finished")
	get_tree().change_scene_to(load(_SCENES[state]))
	Stage.fade_in(duration/2)
	yield(Stage, "fade_finished")
	_state = state
	emit_signal("scene_changed")
	
func player_died():
	emit_signal("player_just_died")
	_run_data.deaths += 1
	_data.statistics.deaths += 1
	
func coin_collected():
	_run_data.coins += 10
	_data.statistics.coins += 10

func orb_collected():
	print("orb collected")
	_run_data.orbs += 1
	_data.statistics.orbs += 1