extends Node2D

@export var combat_spawn: Marker2D
@export var return_spawn: Marker2D

@onready var closed_sprite: Sprite2D = $ClosedSprite
@onready var open_sprite: Sprite2D = $OpenSprite
@onready var area: Area2D = $Area2D

var player_inside := false
var player: Player = null
var completed := false

func _ready() -> void:
	open_sprite.visible = false
	closed_sprite.visible = true

	area.body_entered.connect(_on_body_entered)
	area.body_exited.connect(_on_body_exited)

func _process(_delta: float) -> void:
	if player_inside and not completed and Input.is_action_just_pressed("ui_up"):
		enter_wardrobe()

func _on_body_entered(body: Node) -> void:
	if body is Player:
		player_inside = true
		player = body
		print("Press UP to enter wardrobe")

func _on_body_exited(body: Node) -> void:
	if body is Player:
		player_inside = false
		player = null

func enter_wardrobe() -> void:
	if player == null:
		return

	if combat_spawn == null:
		print("No combat_spawn assigned for this wardrobe yet")
		return

	print("Entering wardrobe")

	closed_sprite.visible = false
	open_sprite.visible = true

	player.global_position = combat_spawn.global_position
