extends Node

onready var signal_manager := get_tree().root.get_child(4).get_node("%SignalManager")
onready var levelName := get_tree().current_scene.filename

#TODO Easteregg pls
var currency: = 0 setget set_currency
var deaths: = 0 setget set_deaths
var freed_frogs := []
# TODO Rename probs
var is_dead: = false setget set_dead

func _ready() -> void:
	GlobalState.gsr.last_played_level = levelName
	SaveManager.save_default()
	signal_manager.connect("level_completed", self, "_on_level_completed")
	signal_manager.connect("player_died", self, "player_dying")

func reset() -> void:
	deaths = 0
	currency = 0
	freed_frogs = []
	# TODO Maybe not the place for this?
	if GlobalState.gsr.progress_dict.has(levelName):
		GlobalState.gsr.progress_dict[levelName].erase("savepoint")

func set_currency(value: int) -> void:
	currency = value
	signal_manager.emit_signal("currency_updated")

func set_deaths(value: int) -> void:
	deaths = value

func set_dead(value: bool) -> void:
	is_dead = value

func register_frog(number: int, freed: bool = false) -> void:
	update_global_state()
	if(!GlobalState.gsr.progress_dict[levelName]["froggies"].has(number)):
		GlobalState.gsr.progress_dict[levelName]["froggies"][number] = freed
	GlobalState.save()

func free_a_frog(number: int) -> void:
	freed_frogs.append(number)

func needs_tutorial(lesson: String) -> bool:
	if(!GlobalState.gsr.tutorial_prompts.has(lesson)):
		return false
	return GlobalState.gsr.tutorial_prompts[lesson]

func register_tutorial(lesson: String) -> void:
	if(GlobalState.gsr.tutorial_prompts.has(lesson)):
		return
	GlobalState.gsr.tutorial_prompts[lesson] = true
	GlobalState.save()

func absolved_tutorial(lesson: String) -> void:
	if(!GlobalState.gsr.tutorial_prompts.has(lesson)):
		return
	GlobalState.gsr.tutorial_prompts[lesson] = false
	GlobalState.save()

# Spends the currency when enough is available
# and returns true if so. Else it does not spend and return false.
func spend_currency(cost: int) -> bool:
	# TODO member that
	if(OS.is_debug_build()):
		return true
	if GlobalState.gsr.wallet + currency < cost:
		return false
	var remainder = currency - cost
	if remainder >= 0:
		currency = remainder
	else:
		currency = 0
		GlobalState.set_wallet(GlobalState.gsr.wallet + remainder)
	return true

func _on_level_completed():
	#if(OS.is_debug_build()):
	 #   return
	update_global_state()
	reset()

func update_global_state() -> void:
	var progress_dict : Dictionary = GlobalState.get_progress()
	var levelProgress : Dictionary = {}

	levelProgress["currency"] = currency
	levelProgress["deaths"] = deaths
	
	# TODO Doesnt account for multiple plays of same level
	if !progress_dict.has(levelName):
		progress_dict[levelName] = levelProgress
	else:
		progress_dict[levelName]["currency"] = GlobalState.get_property_value(levelName,"currency") + currency
		progress_dict[levelName]["deaths"] = GlobalState.get_property_value(levelName,"deaths") + deaths
	if !progress_dict[levelName].has("froggies"):
		progress_dict[levelName]["froggies"] = {}
	else:
		for frog_number in freed_frogs:
			if progress_dict[levelName]["froggies"].has(frog_number):
				progress_dict[levelName]["froggies"][frog_number] = true

	# TODO Wallet is independant from progress_dict because???
	GlobalState.set_wallet(GlobalState.gsr.wallet + currency)
	GlobalState.set_progress(progress_dict)

func player_dying(animation_number: int = 0) -> void:
	currency = 0
	is_dead = true
	freed_frogs = []
	deaths += 1
	update_global_state()
	deaths = 0
