# `say_last_dialog_option`
#
# Current player says the text of the last selected dialog option.
#
# @ESC
extends ESCBaseCommand
class_name SayLastDialogOptionCommand


# Return the descriptor of the arguments of this command
func configure() -> ESCCommandArgumentDescriptor:
  return ESCCommandArgumentDescriptor.new()

# Run the command
func run(_command_params: Array) -> int:
	escoria.current_state = escoria.GAME_STATE.DIALOG

	if !escoria.dialog_player:
		escoria.logger.error(
			self,
			"[%s]: No dialog player was registered and the say command was encountered."
					% get_command_name()
		)
		escoria.current_state = escoria.GAME_STATE.DEFAULT
		return ESCExecution.RC_ERROR

	if not escoria.main.current_scene.player:
		escoria.logger.warn(
			self,
			"[%s]: No player item in the current scene was registered and the say command was encountered."
					% get_command_name()
		)
		escoria.current_state = escoria.GAME_STATE.DEFAULT
		return ESCExecution.RC_CANCEL

	var last_chosen_option = escoria.globals_manager.get_global("ESC_DIALOG_CHOSEN_OPTION")
	# Surround text with quotes. Required by escoria.dialog_player.say()
	var text = "\"%s\"" % last_chosen_option

	var speaking_character_global_id = escoria.main.current_scene.player.global_id

	escoria.dialog_player.say(
		speaking_character_global_id,
		"",
		text
	)
	yield(escoria.dialog_player, "say_finished")
	escoria.current_state = escoria.GAME_STATE.DEFAULT

	return ESCExecution.RC_OK
