extends StaticBody2D

var isOpen = false

@onready var collision = $CollisionShape
@onready var lightOccluder = $LightOccluder
@onready var animatedSprite = $AnimatedSprite
@onready var interactIcon = $InteractIcon
@onready var interactionRadius = $InteractionRadius

func _ready() -> void:
	interactIcon.hide()
	interactIcon.rotation_degrees -= rotation_degrees
	collision.disabled = false

func toggle():
	if isOpen:
		close()
	else:
		open()
	
func open():
	await get_tree().physics_frame
	isOpen = true
	collision.disabled = true
	lightOccluder.visible = false
	animatedSprite.play("open")
	
func close():
	await get_tree().physics_frame
	isOpen = false
	collision.disabled = false
	lightOccluder.visible = true
	animatedSprite.play_backwards("open")


func _on_interaction_radius_area_entered(area: Area2D) -> void:
	if area.is_in_group("Player"):
		interactIcon.show()


func _on_interaction_radius_area_exited(area: Area2D) -> void:
	if area.is_in_group("Player") :
		interactIcon.hide()


func _on_interaction_radius_body_entered(body: Node2D) -> void:
	if body.is_in_group("Entity"):
		if not isOpen:
			open()

func _on_interaction_radius_body_exited(body: Node2D) -> void:
	if body.is_in_group("Entity") and interactionRadius.get_overlapping_bodies() == []:
		if isOpen:
			close()
