extends Node

export var splash_screen_path: String
export var main_menu_path: String
export var world_path: String


func _ready() -> void:
	var splash_screen: Node = play_splash_screen()
	yield(splash_screen, 'complete')
	splash_screen = null

	var main_menu: Node = play_main_menu()
	yield(main_menu, 'complete')
	free_connected_node(main_menu, 'main_menu_option')
	main_menu = null
	return


func play_splash_screen() -> Node:
	var splash_screen: Node = load(splash_screen_path).instance()
	if splash_screen.connect('complete', self, 'free_connected_node', [splash_screen, 'free_connected_node']) != OK:
		print('ERROR: Splash Screen "complete" signal already connected.')

	add_child(splash_screen)
	return splash_screen


func play_main_menu() -> Node:
	var main_menu: Node = load(main_menu_path).instance()
	if main_menu.connect('complete', self, 'main_menu_option') != OK:
		print('ERROR: Main Menu "quit" signal already connected.')

	add_child(main_menu)
	return main_menu


func main_menu_option(option: String) -> void:
	if option == 'new game':
		new_game()
	elif option == 'quit':
		quit_game()
	return


func free_connected_node(node: Node, connected_function: String) -> void:
	node.disconnect('complete', self, connected_function)
	remove_child(node)
	node.queue_free()
	return


func new_game() -> void:
	#if get_tree().change_scene(world_path) != OK:
	if get_tree().change_scene("res://Levels/Level 4.tscn"):
		print('ERROR: Main failed to change scene to World.')
	return


func quit_game() -> void:
	get_tree().quit()
	return
