using Godot;
using System;

public partial class LevelManager : Node
{
	Node2D StartNode;
	Node2D EndNode;
	Node2D Player;
	string EndType = "nothing";
	
	public override void _Ready()
	{
		GetTree().Root.GetNode<CommonData>("CommonData").CurrentHealth = GetTree().Root.GetNode<CommonData>("CommonData").MaxHealth;
		
		StartNode = GetParent().GetNode<Node2D>("StartNode");
		EndNode = GetParent().GetNode<Node2D>("EndNode");
		Player = GetParent().GetNode<Node2D>("Player");
		
		// Play into animation
		GetParent().GetNode("ScreenAnimations").GetNode("LevelStartAssets").GetNode<AnimationPlayer>("LevelStart").CurrentAnimation = "default";
		GetParent().GetNode("ScreenAnimations").GetNode<Node2D>("LevelStartAssets").Reparent(GetTree().Root.GetNode("Camera").GetNode("Camera2D"));
		GetTree().Root.GetNode("Camera").GetNode("Camera2D").GetNode<Node2D>("LevelStartAssets").GlobalPosition = GetTree().Root.GetNode("Camera").GetNode<Node2D>("Camera2D").GlobalPosition;
		GetTree().Root.GetNode("Camera").GetNode("Camera2D").GetNode<Node2D>("LevelStartAssets").Visible = true;
	}

	public override void _Process(double delta)
	{
		
		// Manage conditions to end the level
		if (EndType == "nothing")
		{
			// Death
			if (GetTree().Root.GetNode<CommonData>("CommonData").CurrentHealth <= 0)
			{
				EndManager("Death");
				EndType = "Death";
			}
			// Success
			if (GetParent().HasNode("Player") && Player.GlobalPosition.DistanceTo(EndNode.GlobalPosition) <= 16)
			{
				EndManager("Success");
				EndType = "Success";
			}
		}
	}
	
	public void EndManager(string EndType)
	{
		switch (EndType)
		{
			case "Death": // Player Died
				//Player.QueueFree();
				// Go back to main menu (Replace with proper ending screen and then send back to main menu)
				GetNode<ChangeMenuInitiator>("ChangeMenuInitiator").ChangeMenu("FAST", "MainMenu", GetParent().Name);
				break;
			case "Success": // Success
					// Remove the player to stop checking
					//Player.QueueFree();
					// Add level completed to the hash set
					GetTree().Root.GetNode<CommonData>("CommonData").LevelsCompleted.Add(GetParent().Name);
					// Play exit animation
					GetParent().GetNode("ScreenAnimations").GetNode("LevelCompleteAssets").GetNode<AnimationPlayer>("LevelComplete").CurrentAnimation = "default";
					GetParent().GetNode("ScreenAnimations").GetNode<Node2D>("LevelCompleteAssets").Reparent(GetTree().Root.GetNode("Camera").GetNode("Camera2D"));
					GetTree().Root.GetNode("Camera").GetNode("Camera2D").GetNode<Node2D>("LevelCompleteAssets").GlobalPosition = GetTree().Root.GetNode("Camera").GetNode<Node2D>("Camera2D").GlobalPosition;
					GetTree().Root.GetNode("Camera").GetNode("Camera2D").GetNode<Node2D>("LevelCompleteAssets").Visible = true;
					// Go to exit cutscene after exit animation is complete
					var LevelName = (string)GetParent().Name;
					var LevelNumber = LevelName.Split("Level");
					GetNode<ChangeMenuInitiator>("ChangeMenuInitiator").ChangeMenu("SLOW", $"Cutscenes/ExitCutscene{LevelNumber[0]}", GetParent().Name);
				break;
			default:
				break;
		}
	}
}
