using System;
using System.Reflection;
using EFT.InventoryLogic;
using EFT.UI;

public class Gridmanager
{
	private static Type _ued1aType;

	private static ConstructorInfo _ued1aConstructor;

	static Gridmanager()
	{
		LoadUed1aType();
	}

	private static void LoadUed1aType()
	{
		try
		{
			Type[] types = Assembly.Load("Assembly-CSharp").GetTypes();
			foreach (Type type in types)
			{
				if (type.Name == "\ued6f")
				{
					_ued1aType = type;
					ConsoleScreen.Log("Found type: \ued6f");
					break;
				}
			}
			if (_ued1aType != null)
			{
				ConstructorInfo[] constructors = _ued1aType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				foreach (ConstructorInfo constructorInfo in constructors)
				{
					ParameterInfo[] parameters = constructorInfo.GetParameters();
					ConsoleScreen.Log($"Constructor found with {parameters.Length} parameters:");
					for (int j = 0; j < parameters.Length; j++)
					{
						ParameterInfo parameterInfo = parameters[j];
						ConsoleScreen.Log($"Param {j + 1}: {parameterInfo.ParameterType.FullName} ({parameterInfo.Name})");
					}
					if (parameters.Length == 8 && parameters[0].ParameterType == typeof(string) && parameters[1].ParameterType == typeof(int) && parameters[2].ParameterType == typeof(int) && parameters[3].ParameterType == typeof(bool) && parameters[4].ParameterType == typeof(bool) && parameters[5].ParameterType.IsArray && parameters[6].ParameterType.Name.Contains("CompoundItem") && parameters[7].ParameterType == typeof(int))
					{
						_ued1aConstructor = constructorInfo;
						ConsoleScreen.Log("Located the obfuscated constructor successfully.");
						break;
					}
				}
				if (_ued1aConstructor == null)
				{
					ConsoleScreen.Log("Failed to locate \ued6f constructor by matching parameter types.");
				}
			}
			else
			{
				ConsoleScreen.Log("Failed to find \ued6f type.");
			}
		}
		catch (Exception ex)
		{
			ConsoleScreen.Log("Error during LoadUed1aType: " + ex.Message);
		}
	}

	public static dynamic CreateGrid(string id, int gridWidth, int gridHeight, bool canStretchVertically, bool canStretchHorizontally, CompoundItem parentItem)
	{
		try
		{
			if (_ued1aConstructor != null)
			{
				ItemFilter[] array = new ItemFilter[0];
				ConsoleScreen.Log($"Attempting to create new grid with ID: {id}, Width: {gridWidth}, Height: {gridHeight}");
				return _ued1aConstructor.Invoke(new object[8] { id, gridWidth, gridHeight, canStretchVertically, canStretchHorizontally, array, parentItem, -1 });
			}
			ConsoleScreen.Log("Failed to create new grid. Constructor is null.");
			return null;
		}
		catch (Exception ex)
		{
			ConsoleScreen.Log("Error creating grid: " + ex.Message);
			return null;
		}
	}

	public async void ReplaceGrid(dynamic item, int newGridWidth, int newGridHeight, string newGridID = null)
	{
		try
		{
			ConsoleScreen.Log("Starting grid replacement process...");
			dynamic val = new ItemFactory().CreateItem("5c0a794586f77461c458f892");
			if (val == null)
			{
				ConsoleScreen.Log("Failed to create boss container.");
				return;
			}
			ConsoleScreen.Log("Boss container created successfully.");
			dynamic val2 = val.Grids.FirstOrDefault();
			if (val2 == null)
			{
				ConsoleScreen.Log("Failed to retrieve the boss container's grid.");
				return;
			}
			ConsoleScreen.Log($"Boss container grid found. Grid ID: {(object)val2.ID}");
			if (!(item.Grids[0]?.ParentItem is CompoundItem compoundItem))
			{
				ConsoleScreen.Log("Failed to retrieve the original parent item.");
				return;
			}
			ConsoleScreen.Log("Original parent item retrieved successfully.");
			if (item.Grids != null && item.Grids.Length > 0)
			{
				dynamic val3 = item.Grids[0];
				ConsoleScreen.Log("Original grid found.");
				string text = val3.ID;
				string text2 = (string.IsNullOrEmpty(newGridID) ? text : newGridID);
				ConsoleScreen.Log("Replacing original grid with Boss container grid. Final Grid ID: " + text2);
				val2.ParentItem = compoundItem;
				item.Grids[0] = val2;
				ConsoleScreen.Log("Successfully replaced the grid with the boss container's grid.");
			}
			else
			{
				ConsoleScreen.Log("Item does not have any grids to replace.");
			}
		}
		catch (Exception ex)
		{
			ConsoleScreen.Log("Error replacing grid: " + ex.Message + "\n" + ex.StackTrace);
		}
	}
}
