using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using EFT.UI;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace stupid.solutions.Utils;

public class ItemPresetManager
{
	public Dictionary<string, object> itemParameters;

	public ItemPresetManager()
	{
		itemParameters = new Dictionary<string, object>();
	}

	public void SaveToJson(string filePath)
	{
		try
		{
			string text = (itemParameters.ContainsKey("ItemId") ? itemParameters["ItemId"].ToString() : "");
			if (string.IsNullOrEmpty(text))
			{
				ConsoleScreen.Log("Log: No main item ID found. Cannot save.");
				return;
			}
			Dictionary<string, object> dictionary = new Dictionary<string, object>();
			foreach (string key in itemParameters.Keys)
			{
				if (itemParameters[key] is Dictionary<string, string> dictionary2)
				{
					dictionary[key] = new Dictionary<string, string>
					{
						{
							"SlotID",
							dictionary2["SlotID"]
						},
						{
							"SlotName",
							dictionary2["SlotName"]
						},
						{
							"ItemId",
							dictionary2["ItemId"]
						}
					};
				}
			}
			string contents = JsonConvert.SerializeObject(new Dictionary<string, object> { 
			{
				text,
				new Dictionary<string, object>
				{
					{
						"StackMaxSize",
						itemParameters.ContainsKey("StackMaxSize") ? itemParameters["StackMaxSize"] : ((object)1)
					},
					{
						"QuestItem",
						itemParameters.ContainsKey("QuestItem") ? itemParameters["QuestItem"] : ((object)false)
					},
					{ "Slots", dictionary }
				}
			} }, Formatting.Indented);
			File.WriteAllText(filePath, contents);
			ConsoleScreen.Log("Log: Item preset saved successfully to " + filePath);
		}
		catch (Exception ex)
		{
			ConsoleScreen.Log("Log: Error saving preset: " + ex.Message);
		}
	}

	public Dictionary<string, Dictionary<string, string>> LoadFromJson(string filePath, out string mainItemId)
	{
		mainItemId = null;
		try
		{
			if (!File.Exists(filePath))
			{
				ConsoleScreen.Log("Log: JSON file not found at " + filePath);
				return null;
			}
			Dictionary<string, object> dictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(File.ReadAllText(filePath));
			mainItemId = dictionary.Keys.FirstOrDefault();
			if (string.IsNullOrEmpty(mainItemId))
			{
				ConsoleScreen.Log("Log: No main item ID found in the JSON.");
				return null;
			}
			Dictionary<string, string> dictionary2 = new Dictionary<string, string>();
			if (dictionary[mainItemId] is JObject jObject && jObject.TryGetValue("Slots", out JToken value) && value is JObject jObject2)
			{
				foreach (JProperty item in jObject2.Properties())
				{
					if (item.Value is JObject jObject3)
					{
						string key = jObject3["SlotID"].ToString();
						string value2 = jObject3["ItemId"].ToString();
						dictionary2[key] = value2;
					}
				}
			}
			return new Dictionary<string, Dictionary<string, string>> { { "Slots", dictionary2 } };
		}
		catch (Exception ex)
		{
			ConsoleScreen.Log("Log: Error loading preset: " + ex.Message);
			return null;
		}
	}

	public void Clear()
	{
		itemParameters.Clear();
		ConsoleScreen.Log("Log: Item parameters cleared.");
	}
}
