extends Node

onready var mini_game = $MiniGame;

var dialogic_node;

var STUDENT_MUSIC = "res://music/etudiant.ogg"
var ENGI_MUSIC = "res://music/ingenieur.ogg"
var USER_MUSIC = "res://music/utilisateur.ogg"

func _ready():
	dialogic_node = Dialogic.start_from_save('00_start')
	add_child_below_node($MiniGame, dialogic_node)
	dialogic_node.connect('dialogic_signal', self, "_on_Dialogic_signal_received")
	play_music()


func play_music():
	var current_phase = Dialogic.get_variable("current_phase")
	var music = STUDENT_MUSIC
	match current_phase:
		"0":
			music = STUDENT_MUSIC
		"1":
			music = ENGI_MUSIC
		"2":
			music = USER_MUSIC
	BackgroundMusic.crossfade_to(music, -10, 1)


func _on_Dialogic_signal_received(value: String):
	print("signal received")
	var args = value.split(" ")
	if args.size() > 0:
		var type = args[0];
		match type:
			"play_music":
				play_music()
			"start_minigame":
				start_minigame()
			"setup_minigame":
				if (args.size() >= 3):
					setup_minigame(args[1], args[2], args[3])
				else:
					print("not enough arguments for start_minigame")
			_:
				print("wrong type")


func setup_minigame(mode: String, next_timeline_lose: String, next_timeline_win):
	mini_game.setup(mode, next_timeline_lose, next_timeline_win)


func start_minigame():
	mini_game.start()


func _on_MiniGame_game_over(next_timeline: String):
	print("Game over received")
	print("next timeline: " + next_timeline)
	dialogic_node = Dialogic.start(next_timeline)
	dialogic_node.connect('dialogic_signal', self, "_on_Dialogic_signal_received")
	add_child_below_node($MiniGame, dialogic_node)
