extends Node

@export var audio_player: AudioStreamPlayer

@export var audio1: AudioStream
@export var audio2l: AudioStream
@export var audio3: AudioStream
@export var audio4l: AudioStream
@export var audio5: AudioStream
@export var audio6: AudioStream
@export var audio7l: AudioStream
@export var audio8: AudioStream

var characters_talked_to: int = 0
var ending: bool = false

# Called when the node enters the scene tree for the first time.
func _ready():
	_play(audio1)
	DialogueManager.dialogue_finished.connect(_quit_dialogue)

func _play(audio_source: AudioStream):
	audio_player.stream = audio_source
	audio_player.play()


func _on_audio_stream_player_finished():
	match audio_player.stream:
		audio1:
			if characters_talked_to > 0:
				_play(audio3)
			else:
				_play(audio2l)
		audio2l:
			if characters_talked_to > 0:
				_play(audio3)
			else:
				_play(audio2l)
		audio3:
			if characters_talked_to > 1:
				_play(audio3)
			else:
				_play(audio4l)
		audio4l:
			if characters_talked_to > 3:
				_play(audio5)
			else:
				_play(audio4l)
		audio5:
			_play(audio6)
		audio6:
			_play(audio7l)
		audio7l:
			if ending:
				_play(audio8)
			else:
				_play(audio7l)

func _quit_dialogue(_time_to_add: int):
	characters_talked_to = (
		int(DialogueManager.global_variables["Character1"]) +
		int(DialogueManager.global_variables["Character2"]) +
		int(DialogueManager.global_variables["Character3"]) +
		int(DialogueManager.global_variables["Character4"])
	)
