extends Node2D

var bullet = preload("res://Scenes/bullet.tscn")
var bulletArray = []
var totalAllowedBullets = 7

var current_level_container: Node2D
var current_level: Node2D
var triggers: Node2D
var coins: Node2D
var enemies: Node2D
var player: Player
var ui: Control

func _ready() -> void:
	print("SCENE MANAGER READY")
	current_level_container = $"../CurrentLevel"
	current_level = current_level_container.get_child(0)

	triggers = current_level.get_node("Triggers")
	coins = current_level.get_node("Coins")
	enemies = current_level.get_node_or_null("Enemies")

	player = get_parent().get_node("Player")
	ui = current_level.get_node_or_null("CanvasLayer/UI")
	
	move_player_to_spawn()
	apply_camera_limits()
	
	Gamecontroller.reset()
	
	for obj in triggers.get_children():
		obj.areatrigger.connect(Gamecontroller._on_areatrigger)
		
	if enemies:
		for obj in enemies.get_children():
			if obj is Slime:
				obj.playerDamageSignal.connect(Gamecontroller.playerAttacked)
				Gamecontroller.addEnemyToLevel(obj)
				Gamecontroller.bulletDamageEnemySignal.connect(obj.takeDamage)
				
	updateCoins()
	
	Gamecontroller.destroySignal.connect(destroyItem)
	Gamecontroller.levelChangeSignal.connect(changeScene)
	Gamecontroller.playerTakesDamageSignal.connect(player.playerTakesDamage)
	if ui:
		Gamecontroller.playerTakesDamageSignal.connect(ui.healthUpdate)
		Gamecontroller.countDownSignal.connect(ui.timerUpdate)
		Gamecontroller.coinCountSignal.connect(ui.coinsUpdate)
	Gamecontroller.playerDiesSignal.connect(player.playerDies)
	player.deathAnimationCompleteSignal.connect(Gamecontroller.playerDead)
	
func apply_camera_limits() -> void:
	var bounds = current_level.get_node_or_null("CameraBounds")

	if bounds == null:
		print("No CameraBounds found in level")
		return

	var top_left = bounds.get_node("TopLeft")
	var bottom_right = bounds.get_node("BottomRight")

	player.set_camera_limits(
		int(top_left.global_position.x),
		int(top_left.global_position.y),
		int(bottom_right.global_position.x),
		int(bottom_right.global_position.y)
	)
	
func move_player_to_spawn() -> void:
	var spawn = current_level.get_node_or_null("PlayerSpawn")
	print("Trying to move player to spawn")


	if spawn == null:
		print("No PlayerSpawn found in level")
		return

	player.global_position = spawn.global_position
	print("Player moved to spawn: ", spawn.global_position)
	
	
#call this any time the coins change 
func updateCoins() -> void:
	var coinCount = 0
	for obj in coins.get_children():
		#connect coin collected to game controller
		if not obj.coinCollectedSignal.is_connected(Gamecontroller.coinCollected):
			obj.coinCollectedSignal.connect(Gamecontroller.coinCollected)
		#connect tree_exited signal for updates
		if not obj.tree_exited.is_connected(updateCoins):
			obj.tree_exited.connect(updateCoins)
		coinCount +=1
	#send information to the game controller
	Gamecontroller.totalCoinCount(coinCount)
	
#makes bullets
func bulletFactory():
	var mybullet
	if bulletArray.size() < totalAllowedBullets:
		#make a new bullet
		mybullet = bullet.instantiate()
		mybullet.bulletDamageSignal.connect(Gamecontroller.bulletDamage)
		owner.add_child(mybullet)
	else:
		#recycle bullet
		mybullet = bulletArray.pop_back()
		
	bulletArray.push_front(mybullet)
	return mybullet
	
#order desk
func makeBullet(position, speed):
	print("Scenemanger orders a bullet")
	#ask the factory for a bullet
	var mybullet = bulletFactory()
	#set the speed for the bullet
	mybullet.setSpeed(speed)
	#position the bullet
	mybullet.transform = position
	
func destroyItem(body):
	body.queue_free()
func changeScene(level):
	get_tree().call_deferred("change_scene_to_file", level)
	
