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

export var exit_area_2d_path : NodePath
export var enter_area_2d_path : NodePath

var _exit_area_2d = null;
var _enter_area_2d = null;

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

func _ready():
	self._exit_area_2d = get_node(exit_area_2d_path)
	self._enter_area_2d = get_node(enter_area_2d_path)
	pass



func _set_active(activated:bool = true):
	is_active_level = activated;
	if activated:
		if _exit_area_2d != null: _exit_area_2d.connect("player_entered", self, "_on_ExitLevelAreaNext_player_entered")
		if _enter_area_2d != null: _enter_area_2d.connect("player_entered", self, "_on_ExitLevelArea_player_entered")
		emit_signal("level_ready")

func set_player(player:Player, is_entering:bool):
	_player = player
	_camera = _player.get_camera()
	_set_camera_limits()
	_set_player_position(is_entering)
	add_child(player)
	
func remove_player():
	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):
	_player.position = level_entry_point if is_entering else level_exit_point
	_player.face_player(is_entering)
	_player.respawn_position = _player.position
	
func set_exit_area_enabled(enabled:bool=true):
	if _exit_area_2d != null: _exit_area_2d.set_block_signals(enabled)
	
func set_enter_area_enabled(enabled:bool=true):
	if _enter_area_2d != null: _enter_area_2d.set_block_signals(enabled)
	
func _on_ExitLevelArea_player_entered(is_exit, zone) -> void:
	print("_on_ExitLevelArea_player_entered")
	self.is_active_level = false;
	emit_signal("level_exited", false)
	pass # Replace with function body.


func _on_ExitLevelAreaNext_player_entered(is_exit, zone) -> void:
	print("_on_ExitLevelAreaNext_player_entered")
	self.is_active_level = false;
	emit_signal("level_exited", true)
	pass # Replace with function body.
