using System.Collections.Generic;
using Godot;

public partial class NodeDisplay : PanelContainer
{
	[Export] PanelContainer editorDisplay;
	[Export] public Button listDisplay;
	public ProgramNode node;

	[Signal]
	public delegate void OnDeleteNodeEventHandler();

	public void SetNode(ProgramNode node)
	{
		this.node = node;
	}

	public override void _Ready()
	{
		if (node == null) return;

		node.Setup(this);
	}

	public void ShowListDisplay()
	{
		editorDisplay.Visible = false;
		listDisplay.Visible = true;
	}

	public void ShowEditorDisplay()
	{
		editorDisplay.Visible = true;
		listDisplay.Visible = false;
	}

	public void DeleteNodePressed()
	{
		EmitSignal(SignalName.OnDeleteNode);
	}

	public static NodeDisplay Load(string content, Dictionary<ProgramNode, PackedScene> DSLNodes)
	{
		string nodeSanitized = content.Replace("\r\n", "").Trim();
		if (nodeSanitized.Length <= 0) return null;

		string nodeName = nodeSanitized.Split(",")[0].Replace("Name: ", "").ToLower();
		PackedScene prefab = GetPrefab(nodeName, DSLNodes);
		if (prefab == null) return null;

		NodeDisplay result = prefab.Instantiate<NodeDisplay>();

		switch (nodeName)
		{
			case "move":
				result.node = new MoveNode();
				result.LoadMove(nodeSanitized);
				break;
			case "harvest":
				result.node = new HarvestNode();
				result.LoadHarvest(nodeSanitized);
				break;
			case "explore":
				result.node = new ExploreNode();
				result.LoadExplore(nodeSanitized);
				break;
			case "craft":
				result.node = new CraftNode();
				result.LoadCraft(nodeSanitized);
				break;
			default:
				result.QueueFree();
				return null;
		}

		return result;
	}

	private static PackedScene GetPrefab(string nodeName, Dictionary<ProgramNode, PackedScene> DSLNodes)
	{
		foreach (ProgramNode programNode in DSLNodes.Keys)
		{
			if (programNode.DisplayText.ToLower() == nodeName)
			{
				return DSLNodes[programNode];
			}
		}

		return null;
	}

	public void LoadHarvest(string content)
	{
	}

	public void LoadMove(string content)
	{
		HBoxContainer valueContainer = GetNode<HBoxContainer>("./EditorDisplay/HBoxContainer/");
		string[] parts = content.Split(",");
		string positionValues = parts[1].Replace("Position: ", "").Replace("(", "").Replace(")", "");
		int posX = int.Parse(positionValues.Split("|")[0]);
		int posY = int.Parse(positionValues.Split("|")[1]);
		int posZ = int.Parse(positionValues.Split("|")[2]);
		valueContainer.GetNode<SpinBox>("./CoordinateX").Value = posX;
		valueContainer.GetNode<SpinBox>("./CoordinateY").Value = posY;
		valueContainer.GetNode<SpinBox>("./CoordinateZ").Value = posZ;

		MoveNode moveNode = node as MoveNode;
		if (moveNode != null)
		{
			moveNode.targetPosition = new Vector3I(posX, posY, posZ);
		}
	}

	public void LoadExplore(string content)
	{
	}

	public void LoadCraft(string content)
	{
		HBoxContainer valueContainer = GetNode<HBoxContainer>("./EditorDisplay/HBoxContainer/");
		string[] parts = content.Split(",");
		string itemString = parts[1].Replace("Item: ", "").Replace(" ", "");
		if (itemString.ToLower() != "empty")
		{
			CraftNode craftNode = node as CraftNode;
			if (craftNode != null)
			{
				craftNode.selectedItem = new Item { data = GameData.availableItems[itemString] };
			}
		}
		string amountString = parts[2].Replace("Amount: ", "");
		valueContainer.GetNode<SpinBox>("./Amount").Value = int.Parse(amountString);
	}
}
