extends Node2D

@export_file("*.tscn") var golé: String
@export_file("*.tscn") var next_level: String

@export var base_mode: GUIDEMappingContext

@export var vies = 3

@onready var interact: AudioStreamPlayer2D = $Player/Interact
@onready var vo_interjection: AudioStreamPlayer2D = $RightDoor/VOInterjection

var is_interjection_next = false
var current_info = 0
@onready var vo_player_1: AudioStreamPlayer2D = $RightDoor/VOPlayer1
@onready var vo_player_2: AudioStreamPlayer2D = $RightDoor/VOPlayer2
@onready var vo_player_3: AudioStreamPlayer2D = $RightDoor/VOPlayer3
@onready var vo_player_4: AudioStreamPlayer2D = $RightDoor/VOPlayer4
@onready var vo_player_5: AudioStreamPlayer2D = $RightDoor/VOPlayer5
@onready var vo_player_6: AudioStreamPlayer2D = $RightDoor/VOPlayer6
@onready var vo_player_7: AudioStreamPlayer2D = $RightDoor/VOPlayer7
@onready var vo_player_8: AudioStreamPlayer2D = $RightDoor/VOPlayer8
@onready var vo_player_9: AudioStreamPlayer2D = $RightDoor/VOPlayer9
@onready var vo_player_10: AudioStreamPlayer2D = $RightDoor/VOPlayer10
@onready var vo_player_11: AudioStreamPlayer2D = $RightDoor/VOPlayer11
@onready var vo_player_12: AudioStreamPlayer2D = $RightDoor/VOPlayer12
var vo_players: Array[AudioStreamPlayer2D] = []

var currently_playing: AudioStreamPlayer2D
var already_played_batch_1 = false
var already_played_batch_2 = false
var already_played_batch_3 = false

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	GUIDE.enable_mapping_context(base_mode)
	
	vo_players.append(vo_player_1)
	vo_players.append(vo_player_2)
	vo_players.append(vo_player_3)
	vo_players.append(vo_player_4)
	
	vo_players.append(vo_player_5)
	vo_players.append(vo_player_6)
	vo_players.append(vo_player_7)
	vo_players.append(vo_player_8)
	
	vo_players.append(vo_player_9)
	vo_players.append(vo_player_10)
	vo_players.append(vo_player_11)
	vo_players.append(vo_player_12)

func _on_interactible_triggered(event_id: String) -> void:
	interact.play()
	vies -= 1
	if vies == 0:
		SceneLoader.load_scene(golé)

func _on_wrong_door_event_triggered(event_id: String) -> void:
	SceneLoader.load_scene(golé)


func _on_right_door_event_triggered(event_id: String) -> void:
	SceneLoader.load_scene(next_level) 


func _on_timer_between_vo_timeout() -> void:
	if currently_playing == null or not currently_playing.playing:
		currently_playing = vo_interjection
		vo_interjection.play()
		return


func _on_door_event_triggered(event_id: String) -> void:
	interact.play()


func _on_first_batch_trigger_body_entered(body: Node2D) -> void:
	if body.name != "Player":
		return
	if already_played_batch_1:
		return
	already_played_batch_1 = true
	
	if currently_playing != null:
		currently_playing.stop()
		
	currently_playing = vo_player_1
	vo_player_1.play()


func _on_vo_player_1_finished() -> void:
	currently_playing = vo_player_2
	vo_player_2.play()

func _on_vo_player_2_finished() -> void:
	currently_playing = vo_player_3
	vo_player_3.play()


func _on_vo_player_3_finished() -> void:
	currently_playing.stop()


func _on_first_batch_trigger_2_body_entered(body: Node2D) -> void:
	if body.name != "Player":
		return
	if already_played_batch_2:
		return
	already_played_batch_2 = true
	
	currently_playing.stop()
	currently_playing = vo_player_4
	vo_player_4.play()
	
func _on_vo_player_4_finished() -> void:
	currently_playing = vo_player_5
	vo_player_5.play()


func _on_vo_player_5_finished() -> void:
	currently_playing = vo_player_6
	vo_player_6.play()


func _on_vo_player_6_finished() -> void:
	currently_playing = vo_player_7
	vo_player_7.play()


func _on_vo_player_7_finished() -> void:
	currently_playing.stop()


func _on_first_batch_trigger_3_body_entered(body: Node2D) -> void:
	if body.name != "Player":
		return
	if already_played_batch_3:
		return
	already_played_batch_3 = true
	
	currently_playing.stop()
	currently_playing = vo_player_8
	vo_player_8.play()

func _on_vo_player_8_finished() -> void:
	currently_playing = vo_player_9
	vo_player_9.play()


func _on_vo_player_9_finished() -> void:
	currently_playing = vo_player_10
	vo_player_10.play()


func _on_vo_player_10_finished() -> void:
	currently_playing = vo_player_11
	vo_player_11.play()


func _on_vo_player_11_finished() -> void:
	currently_playing = vo_player_12
	vo_player_12.play()


func _on_vo_player_12_finished() -> void:
	currently_playing.stop()


func _on_vo_interjection_finished() -> void:
	currently_playing.stop()
