using Godot;
using Newlon;
using System.Collections.Generic;

public partial class GameRegistry : Node
{
	private const string PLANT_RESOURCE_PATH = "res://resources/plants/";
	private const string ZOMBIE_RESOURCE_PATH = "res://resources/zombies/";
	public static GameRegistry Instance { get; private set; }
	public static readonly Dictionary<string, PlantResource> PlantDictionary = [];
	public static readonly Dictionary<string, ZombieResource> ZombieDictionary = [];

	public override void _Ready()
	{
		//Plant init
		string[] plantFiles = ResourceLoader.ListDirectory(PLANT_RESOURCE_PATH);

		foreach (var file in plantFiles)
		{
			if (ResourceLoader.Exists(PLANT_RESOURCE_PATH + file))
			{
				var plant = ResourceLoader.Load<PlantResource>(PLANT_RESOURCE_PATH + file);
				plant.internal_id = file.ToLower().Split('.')[0];
				PlantDictionary.Add(file.ToLower().Split('.')[0], plant);
			}
		}

		//Zombie init
		string[] zombieFiles = ResourceLoader.ListDirectory(ZOMBIE_RESOURCE_PATH);

		foreach (var file in zombieFiles)
		{
			if (ResourceLoader.Exists(ZOMBIE_RESOURCE_PATH + file))
			{
				var zombie = ResourceLoader.Load<ZombieResource>(ZOMBIE_RESOURCE_PATH + file);
				zombie.internal_id = file.ToLower().Split('.')[0];
				ZombieDictionary.Add(file.ToLower().Split('.')[0], zombie);
			}
		}
	}

	public static PlantResource GetPlantByName(string name)
	{
		if (PlantDictionary.ContainsKey(name) == false) return null;
		return PlantDictionary[name];
	}
	public static List<string> GetPlantNames()
	{
		List<string> result = [];
		foreach (var key in PlantDictionary.Keys)
		{
			result.Add(key);
		}
		return result;
	}
	public static List<PlantResource> GetPlants()
	{
		List<PlantResource> result = [];
		foreach (var value in PlantDictionary.Values)
		{
			result.Add(value);
		}
		return result;
	}
	public static int GetPlantCount()
	{
		return PlantDictionary.Count;
	}

	public static ZombieResource GetZombieByName(string name)
	{
		if (ZombieDictionary.ContainsKey(name) == false) return null;
		return ZombieDictionary[name];
	}
	public static List<string> GetZombieNames()
	{
		List<string> result = [];
		foreach (var key in ZombieDictionary.Keys)
		{
			result.Add(key);
		}
		return result;
	}
	public static List<ZombieResource> GetZombies()
	{
		List<ZombieResource> result = [];
		foreach (var value in ZombieDictionary.Values)
		{
			result.Add(value);
		}
		return result;
	}

	public static int GetZombieCount()
	{
		return ZombieDictionary.Count;
	}

}
