extends Node2D
class_name Level

signal level_exited
signal level_ready

var _player:Player
var _camera:CameraGame

var is_active_level := false

export var game_state = GameState.States.GAME

export var level_entry_point := Vector2.ZERO
export var level_exit_point := Vector2.ZERO

var level_coords :=Vector2.ZERO
export(Vector2) var prev_level = null
export(Vector2) var next_level = null

var _first_time_init = true


export var camer_limits := {
	"top": 0,
	"right": 400,
	"bottom": 400,
	"left": 0,
}

func _ready():
	pass



func init_level_config(prev_level_:Vector2, level_coords_:Vector2):
	print("Initializing [%s]: prev = %s | current = %s" % [self.name, prev_level_, level_coords_]) 
	self.prev_level = prev_level_
	self.level_coords = level_coords_
	self.next_level = Vector2(level_coords.x, level_coords.y + 1)
	

func _set_active(activated:bool, is_entering:bool):
	print("Setting active [%s]: active = %s " % [self.name, activated]) 
	is_active_level = activated
	if activated:
		if _first_time_init: _set_wrap_zones()
		_set_player_position(is_entering)
		emit_signal("level_ready")

func set_player(player:Player):
	print("Setting player [%s]" % [self.name]) 
	_player = player
	_camera = _player.get_camera()
	_set_camera_limits()
	add_child(_player)
	
	
func remove_player():
	print("Removing player [%s]" % [self.name]) 
	remove_child(_player)
	_camera = null
	_player = null

func _set_camera_limits():
	_camera.limit_top = camer_limits.top
	_camera.limit_right = camer_limits.right
	_camera.limit_bottom = camer_limits.bottom
	_camera.limit_left = camer_limits.left

func _set_player_position(is_entering:bool):
	var warps = _get_all_warps()
	var found_spawn_point := false
	var position = Vector2.ZERO
	
	for warp in warps:
		if warp.to_level_coord == prev_level:
			position = warp.get_checkpoint_position()
			found_spawn_point = true
			print("Found point warp.position.x = %s player spawn.x = %s" % [warp.position.x, _player.position.x])
	if not found_spawn_point:
		position = level_entry_point if is_entering else level_exit_point
	
	_player.face_player(is_entering)
	_player.respawn_position = position
	_player.set_position(position)

func _set_wrap_zones():
	_first_time_init = false
	var warps = _get_all_warps()
	for warp in warps:
		warp.connect("player_entered", self, "_on_WarpZone_player_entered")
		var has_custom_level = warp.to_level_coord != warp.NON_SPECIFIED_LEVEL
		if not has_custom_level:
			if not warp.is_exit: warp.to_level_coord = self.prev_level
			else: warp.to_level_coord = self.next_level
			print("Warp has no costum level set to: %s" % [warp.to_level_coord]) 
		else:
			print("Warp a costum level: %s" % [warp.to_level_coord]) 
	
func _on_WarpZone_player_entered(is_exit, to_level_coord) -> void:
	print("_on_WarpZone_player_entered is_exit=%s to_level=%s" % [is_exit, to_level_coord])
	self.is_active_level = false;
	emit_signal("level_exited", is_exit, to_level_coord)
	pass # Replace with function body.


func _get_all_warps():
	var warps := []
	for node in get_children():
		if node.is_in_group("WarpZones"):
			warps.push_back(node)
	print("_get_all_warps [%s]: #%d of warps found" % [self.name, len(warps)])
	return warps
