extends Node3D

@export var close_time: float = 1.0
@export var switches: Array[Node]

@onready var anim = $AnimationPlayer
@onready var audio = $AudioStreamPlayer3D
@onready var close_timer = $Timer

var _opened := false
var _switches_pressed: int = 0


func _ready():
	close_timer.timeout.connect(_close_door)
	close_timer.wait_time = close_time

	# --------------------
	# TODO  7
	# connect switch to door
	# --------------------
	


func _switch_check(is_pressed: bool):
	if is_pressed:
		_switches_pressed += 1
	else:
		_switches_pressed -= 1

	_switches_pressed = clamp(_switches_pressed, 0, switches.size())

	if _switches_pressed == switches.size():
		close_timer.stop()
		if not _opened:
			_open_door()
	elif _opened:
		close_timer.start()


func _open_door():
	_opened = true
	anim.play("open")
	audio.play()


func _close_door():
	_opened = false
	anim.play("close")
	audio.play()
