extends Node

# Main menu & game spawning
@onready
var mainMenuScene = preload("res://main_menu.tscn")
var mainMenuSceneInstance: Node
@onready
var gameScene = preload("res://molecular/molecular_stage.tscn")
var gameSceneInstance: Node
@onready
var respawnScene = preload("res://respawn_menu.tscn")
var respawnSceneInstance: Node
# Currently active scene (instance)
var currentSceneInstance:Node

# UI effects
var fadeEffect: CanvasLayer


func _ready() -> void:
	# Instantiate effects
	fadeEffect = $UI/Fade
	fadeEffect.visible = false
	
	# Create game (main menu)
	print("Creating Main menu...")
	mainMenuSceneInstance = mainMenuScene.instantiate()
	currentSceneInstance = mainMenuSceneInstance
	add_child(currentSceneInstance)
	
	# Link MainMenu button to start_game
	print("Linking Play button...")
	mainMenuSceneInstance.get_node("PlayButton").connect("pressed", start_game)


func start_game() -> void:
	print("Starting game...")
	
	# Instatiate
	currentSceneInstance.queue_free()
	gameSceneInstance = gameScene.instantiate()
	currentSceneInstance = gameSceneInstance
	add_child(currentSceneInstance)
	
	# Populate GameManager with game scene
	GameManager.foodManager = gameSceneInstance.get_node("FoodManager")
	
	
func go_to_respawn_scene() -> void:
	fadeEffect.visible = true
	await fadeEffect.fade(1.0, 1.5).finished
	print("Switching to Respawn scene.")
	currentSceneInstance.queue_free()
	
	respawnSceneInstance = respawnScene.instantiate()
	respawnSceneInstance.get_node("RespawnButton").connect("pressed", start_game)
	currentSceneInstance = respawnSceneInstance
	
	add_child(currentSceneInstance)
	await fadeEffect.fade(0.0, 1.5).finished
	fadeEffect.visible = false
