using Godot;
using System;

public partial class ChangeMenuInitiator : Node
{
	float TransitionDelay = 0; // seconds
	string cameFrom;

	public void ChangeMenu(string type, string destination, string cameFromInput)
	{
		cameFrom = cameFromInput;

		if (type == "SLOW")
		{
			GD.Print("slow switch");
			// Load the menu change manager
			var scene = GD.Load<PackedScene>("res://Assets/Scenes/MenusAndLevels/MenuChangeManager.tscn");
			var inst = (Node2D)scene.Instantiate();
			GetTree().Root.GetNode("Camera").AddChild(inst);
			
			inst.Position = GetTree().Root.GetNode("Camera").GetNode<Node2D>("Camera2D").Position;
			
			// Pass args to menu change manager
			inst.GetNode<MenuChangeManager>("MenuChangeManager").TargetMenu = destination;
			inst.GetNode<MenuChangeManager>("MenuChangeManager").TransitionDelay = TransitionDelay;
			
			// Wait for intro animation to finish before freeing the scene
			inst.GetNode<AnimationPlayer>("AnimationPlayer").AnimationFinished += DeleteMenu;
		}
		if (type == "FAST")
		{
			GD.Print("fast switch");
			var scene = GD.Load<PackedScene>($"res://Assets/Scenes/MenusAndLevels/{destination}.tscn");
			var inst = (Node2D)scene.Instantiate();
			GetTree().Root.AddChild(inst);
			
			//reset camera
			GetTree().Root.GetNode("Camera").GetNode<CameraMovement>("Camera2D").TeleportCamera(Vector2.Zero);
			GetTree().Root.GetNode("Camera").GetNode<CameraMovement>("Camera2D").FixedPosition = GetTree().Root.GetNode("Camera").GetNode<Node2D>("Camera2D").Position;
			GetTree().Root.GetNode("Camera").SetMeta("CameraMode", "FIXED");

			DeleteMenu("LoadIn");
		}
	}
	void DeleteMenu(StringName arg)
	{
		if (arg == "LoadIn")
		{
			// Delete current scene that it came from
			GetTree().Root.GetNode(cameFrom).QueueFree();
		}
	}
	
}
