extends Control

var inkPlayer = load("res://addons/inkgd/ink_player.gd")

@onready var _ink_player = InkPlayer.new()

@onready var text_box = $ColorRect/Label

@export var buttons: Array[Button]
@export var type_sounds: Array[AudioStream]

var displaying_choices = false
var active = false

@onready var timer = $TextDisplayTimer

var text = ""
var character_index = 0

var character_time = 0.03

var finished_displaying = true

var variables: Dictionary

# Called when the node enters the scene tree for the first time.
func _ready():
	_ink_player._add_runtime(get_tree().root)
	_ink_player.loads_in_background = true
	_ink_player.connect("loaded", _story_loaded)
	
	for button in buttons:
		button.hide()

func _setup_story(story: Resource):
	_ink_player.ink_file = story
	_ink_player.create_story()
	active = true

func _process(delta):
	if displaying_choices:
		var i = 0
		for button in buttons:
			if button.button_pressed:
				_select_choice(i)
			i += 1
	else:
		if Input.is_action_just_pressed("advance_dialog"):
			_continue_story()

func _story_loaded(successfully: bool):
	if !successfully:
		return
	
	for key in variables:
		var value = variables[key]
		_ink_player.set_variable(key, value)
	_continue_story()

func _continue_story():
	if !finished_displaying:
		return
	if !_ink_player.can_continue and !_ink_player.has_choices:
		text_box.text = ""
		#TODO: Hardcodear las variables aquí
		variables["time_to_add"] = _ink_player.get_variable("time_to_add")
		variables["Foto1"] = _ink_player.get_variable("Foto1")
		variables["Foto2"] = _ink_player.get_variable("Foto2")
		variables["Foto3"] = _ink_player.get_variable("Foto3")
		variables["Foto4"] = _ink_player.get_variable("Foto4")
		variables["Foto5"] = _ink_player.get_variable("Foto5")
		variables["Character1"] = _ink_player.get_variable("Character1")
		variables["Character2"] = _ink_player.get_variable("Character2")
		variables["Character3"] = _ink_player.get_variable("Character3")
		variables["Character4"] = _ink_player.get_variable("Character4")
		variables["Character5"] = _ink_player.get_variable("Character5")
		
		#DIALOGUE END
		Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
		active = false
		return
	
	if _ink_player.has_choices:
		Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
		var i = 0
		for choice in _ink_player.current_choices:
			buttons[i].show()
			buttons[i].text = choice.text
			i += 1
		for j in range(4 - i):
			buttons[j + i].hide()
		displaying_choices = true
		return
	
	text_box.text = ""
	display_text(_ink_player.continue_story())


func _select_choice(index):
	for button in buttons:
		button.hide()
	
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	
	_ink_player.choose_choice_index(index)
	_continue_story()

################## ANIMATIONS ###################

func display_text(text_to_display: String):
	text = text_to_display
	character_index = 0
	finished_displaying = false
	
	_display_letter()

func _display_letter():
	if character_index >= text.length():
		finished_displaying = true
		return
	text_box.text += text[character_index]
	
	character_index += 1
	$AudioStreamPlayer.stream = type_sounds.pick_random()
	$AudioStreamPlayer.play()
	if character_index >= text.length():
		finished_displaying = true
		return
	
	timer.start(character_time)

func _on_text_display_timer_timeout():
	_display_letter()
