extends Node2D

var totalCoinsAvailable

var timer = Timer.new()
var timeAvailable := 0
var level="res://Scenes/game.tscn"
var levelTwo = "res://Scenes/level2.tscn"
var startingCoins = 0

var levels = ["res://Scenes/game.tscn", "res://Scenes/level2.tscn", "res://Scenes/level3.tscn"]
var timers = [30,15,9]
var currentLevel = 0

signal destroySignal(body)
signal levelChangeSignal(level)
signal playerTakesDamageSignal(health)
signal playerDiesSignal
signal bulletDamageEnemySignal(body)
signal countDownSignal(timeRemaining)
signal coinCountSignal(coinsRemaining, startingCoins)

var player:CharacterStats
var enemy:CharacterStats

var playerHealth:int

var enemiesDict = {}

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	player = load("res://Scripts/rscs/playerStats.tres")
	enemy = load("res://Scripts/rscs/slimeStats.tres")
	
	playerHealth = player.starting_health
	timeAvailable = timers[currentLevel]
	
	add_child(timer)
	timer.wait_time = 1
	timer.one_shot = false 
	timer.connect("timeout", secondCounter)
	timer.start()

func secondCounter() -> void:
	timeAvailable -=1
	countDownSignal.emit(timeAvailable)
	if timeAvailable <= 0:
		print("LOSE")
		#reload the current scene
		levelChangeSignal.emit(levels[currentLevel])
func reset() -> void:
	startingCoins = 0
	timeAvailable = timers[currentLevel]
	playerHealth = player.starting_health
	
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass


func _on_areatrigger(effect, body) -> void:
	print("Game controller knows about trigger")
	if effect == "destroy":
		print("Destroy this thing")
		body.queue_free()
	if effect == "lavaBurn":
		if body is Player:
			playerHealth -= 10
			print("Player health is now "+str(playerHealth))
			

func totalCoinCount(totalCoins):
	if startingCoins == 0:
		startingCoins = totalCoins
	totalCoinsAvailable = totalCoins
func coinCollected(body, coin):
	if not body is Crate:
		destroySignal.emit(coin)
		totalCoinsAvailable -=1
		coinCountSignal.emit(totalCoinsAvailable, startingCoins)
		if totalCoinsAvailable <= 0:
			currentLevel +=1
			if currentLevel >= levels.size():
				currentLevel = 0
			levelChangeSignal.emit(levels[currentLevel])
		
func playerAttacked(body, slime):
	print("GC knows player attacked")
	playerHealth -= enemiesDict[slime].damage
	if playerHealth <=0:
		playerDiesSignal.emit()
	else:
		print("Received damage...")
		playerTakesDamageSignal.emit(playerHealth)

func playerDead():
	levelChangeSignal.emit(levels[currentLevel]) 

func bulletDamage(area, bullet):
	enemiesDict[area].health -= player.rangeDamage
	if enemiesDict[area].health <=0:
		destroySignal.emit(area)
		enemiesDict.erase(area)
	else:
		bulletDamageEnemySignal.emit(area)
	
func addEnemyToLevel(slime):
	var randHealth:int = randi() % 20
	var randDamage:int = randi() % 10
	var enemyStat = {
		"health":enemy.starting_health + randHealth, 
		"damage":enemy.meleeDamage + randDamage
	}
	enemiesDict[slime]=enemyStat
