extends Control
class_name DialogueBox

signal DialogueStarted
signal DialogueEnded

@onready var label: Label = $ColorRect/MarginContainer/Label

var _dialogue_arr: PackedStringArray
var _dialogue_idx := 0
var _dialogue_count := 0

# Public Methods
func OnDialogueTriggered(dialogue_name: String) -> void:
	var text_arr := tr(dialogue_name).split("<nm>")
	_init_dialogue_box(text_arr)
	visible = true
	DialogueStarted.emit()


# Private Methods
func _init_dialogue_box(text_arr: PackedStringArray) -> void:
	_dialogue_arr = text_arr
	_dialogue_idx = 0
	_dialogue_count = len(text_arr)
	
	label.text = _dialogue_arr[_dialogue_idx]


func _proceed_dialogue() -> bool: # Returns true if no more dialogue
	_dialogue_idx += 1
	if _dialogue_idx == _dialogue_count:
		return true
	label.text = _dialogue_arr[_dialogue_idx]
	return false


func _process(_delta: float) -> void:
	if !visible:
		return
	
	if Input.is_action_just_pressed("player_interact"):
		var finished := _proceed_dialogue()
		if finished:
			visible = false
			DialogueEnded.emit()
