extends Node

onready var signalManager := get_tree().root.get_child(3).get_node("%SignalManager")
onready var levelName := get_tree().get_current_scene().get_name()

#TODO Easteregg pls
var currency: = 0 setget set_currency
var deaths: = 0 setget set_deaths
var kills: = 0 setget set_kills
var frees: = 0 setget set_frees
# TODO Rename probs
var is_dead: = false setget set_dead

func _ready() -> void:
    signalManager.connect("level_completed", self, "_on_level_completed")
    signalManager.connect("player_died", self, "player_dying")

func reset() -> void:
    deaths = 0
    kills = 0
    currency = 0
    frees = 0
    # 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
    signalManager.emit_signal("currency_updated")

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

func set_kills(value: int) -> void:
    kills = value
    signalManager.emit_signal("kills_updated")

func set_frees(value: int) -> void:
    frees = value
    signalManager.emit_signal("frees_updated")

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

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

func absolved_tutorial(action_path: String) -> void:
    if(!GlobalState.gsr.tutorial_prompts.has(action_path)):
        return
    GlobalState.gsr.tutorial_prompts[action_path] = 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:
    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():
    update_global_state()
    reset()

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

    levelProgress["currency"] = currency
    levelProgress["kills"] = kills
    levelProgress["deaths"] = deaths
    levelProgress["frees"] = frees
    
    # 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]["kills"] = GlobalState.get_property_value(levelName,"kills") + kills
        progress_dict[levelName]["deaths"] = GlobalState.get_property_value(levelName,"deaths") + deaths
        progress_dict[levelName]["frees"] = GlobalState.get_property_value(levelName,"frees") + frees

    # 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
    kills = 0
    frees = 0
    is_dead = true
    deaths += 1
    update_global_state()
    deaths = 0
