# `play_lib_snd filename namespace`
#
# Plays a sound from the library.
#
# **Parameters**
#
# - *file_name*: File name withot extension
# - *namespace*: Subfolder
#
# @ESC
extends ESCBaseCommand
class_name PlayLibSound


# Return the descriptor of the arguments of this command
func configure() -> ESCCommandArgumentDescriptor:
	return ESCCommandArgumentDescriptor.new(
		1,
		[TYPE_STRING,TYPE_STRING],
		[null,""]
	)


# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
	if not .validate(arguments):
		return false
	
	var subFolder = arguments[1]
	if(arguments[1] != ""):
		subFolder += "/"
	var resourceFile = "res://gymkhana/sounds/" + subFolder + arguments[0] + ".ogg"
	if not ResourceLoader.exists(resourceFile):
		escoria.logger.error(
			self,
			"[%s]: invalid parameter. File %s not found."
					% [get_command_name(), resourceFile]
		)
		return false

	return true


# Run the command
func run(command_params: Array) -> int:
	var resourceFile = "res://gymkhana/sounds/" + command_params[0] + ".ogg"
	escoria.object_manager.get_object("_sound").node.set_state(
		resourceFile
	)
	return ESCExecution.RC_OK


# Function called when the command is interrupted.
func interrupt():
	# Do nothing
	pass

