extends Node2D

@export var sound_sources: Array[AudioStream]

@export var enabled_speaker: Sprite2D
@export var disabled_speaker: Sprite2D


func _ready():
	_disable_speaker()


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if (Input.is_action_just_pressed("BTN_5")):
		_play_random_sound()


func _play_random_sound():
	var stream = sound_sources.pick_random()
	var length = stream.get_length()
	SoundManager.play_sound(stream)
	
	_enable_speaker()
	if ($Timer.time_left < length):
		$Timer.start(length)

func _enable_speaker():
	enabled_speaker.visible = true
	disabled_speaker.visible = false


func _disable_speaker():
	enabled_speaker.visible = false
	disabled_speaker.visible = true


func _on_timer_timeout():
	_disable_speaker()
