extends Control
class_name DialogBubble
signal dialog_finished


var dialog  = [] # Dictionary[] {text:string, callback:function}[]
var _position := 0
var _done := false
var _paused := false
var _started :=false
func resume_dialog():
	_paused = false
func start_dialog():
	$AnimationPlayer.play("slide_in")
	yield($AnimationPlayer, "animation_finished")
	_started = true
	_show_text()

func _show_text():
	if _position >= dialog.size():
		# we are done
		_done = true
		dismiss()
		return
	var sentence = dialog[_position]
	$NinePatchRect/Label.text = sentence.text
	$AnimationPlayer.play("show_text")
	yield($AnimationPlayer, "animation_finished")
	if sentence.callback != null:
		sentence.callback()

func _input(event: InputEvent) -> void:
	if not _started: return
	if Input.is_action_just_pressed("jump"):
		_position += 1
		$AnimationPlayer.play_backwards("show_text")
		yield($AnimationPlayer, "animation_finished")
		_show_text()

	

func dismiss():
	$AnimationPlayer.play_backwards("slide_in")
	yield($AnimationPlayer, "animation_finished")
	emit_signal("dialog_finished")
	queue_free()
