extends Node

var YarnScript = preload("yarn_script.gd")

export (Resource) var yarnScript

var say_func : FuncRef = null

func run_all():
	var next_node = yarnScript.nodes["Start"]
	while next_node != null:
		print("-> Jump to " + next_node["title"])
		var next_node_key = run_body(next_node["body"])
		if (yarnScript.nodes.has(next_node_key)):
			next_node = yarnScript.nodes[next_node_key]
		else:
			next_node = null

func run_body(body):
	for element in body:
		if element["type"] == "jump":
			return element["node"]
		elif element["type"] == "choice_blocks":
			var block = decide_choice_block(element["blocks"])
			return block["node"] # todo change later with "-> choices"
		elif element["type"] == "condition_blocks":
			var block = decide_condition_block(element["blocks"])
			var next_node_key = run_body(block["body"])
			if next_node_key != "":
				return next_node_key
		else:
			if say_func == null or !say_func.is_valid():
				print(element)
			else:
				say_func.call_func(element)
				
	return ""

export var choice = 0

func decide_choice_block(blocks):
	return blocks[choice]

func decide_condition_block(blocks):
	for block in blocks:
		var expr := Expression.new()
		var err := expr.parse(block["condition"])
		
		if err == OK and expr.execute():
			return block
	return null
