using System;
using System.Collections.Generic;
using EFT;
using EFT.InventoryLogic;
using EFT.UI;
using stupid.solutions.Data;
using stupid.solutions.Utils;
using UnityEngine;

namespace stupid.solutions.Features;

public static class EntityManager
{
	public static void KillEntity(Player entity)
	{
		if (entity != null && !entity.IsYourPlayer)
		{
			entity.ActiveHealthController.Kill(EDamageType.Landmine);
			ConsoleScreen.Log("Killed " + entity.name);
		}
	}

	public static void HealEntity(Player entity)
	{
		if (entity == null)
		{
			return;
		}
		foreach (EBodyPart value in Enum.GetValues(typeof(EBodyPart)))
		{
			if (entity.ActiveHealthController.IsBodyPartBroken(value) || entity.ActiveHealthController.IsBodyPartDestroyed(value))
			{
				entity.ActiveHealthController.RestoreBodyPart(value, 1f);
			}
			entity.ActiveHealthController.RemoveNegativeEffects(value);
			entity.ActiveHealthController.RemoveNegativeEffects(EBodyPart.Common);
			entity.ActiveHealthController.RestoreFullHealth();
			entity.ActiveHealthController.ChangeHealth(value, 1000000f, default(_F167));
			entity.ActiveHealthController.ApplyDamage(value, 0f, default(_F167));
			ConsoleScreen.Log("Healed " + entity.name);
		}
	}

	public static void TeleportEntityToCrosshair(Player entity)
	{
		if (!(entity == null) && !(Main.LocalPlayer == null))
		{
			Vector3 position = Main.LocalPlayer.gameObject.transform.position;
			Vector3 forward = Main.LocalPlayer.gameObject.transform.forward;
			float num = 5f;
			Vector3 position2 = position + forward * num;
			entity.Teleport(position2, onServerToo: true);
		}
	}

	public static void ToggleOffEntityObject(Player entity)
	{
		try
		{
			if (entity.AIData.BotOwner.EnemiesController?.EnemyInfos != null && !(Main.LocalPlayer == null))
			{
				if (!entity.AIData.BotOwner.BotsGroup.Allies.Contains(Main.LocalPlayer) && entity.AIData.BotOwner.BotsGroup.Side == entity.Profile.Side)
				{
					entity.AIData.BotOwner.BotsGroup.RemoveEnemy(Main.LocalPlayer);
					entity.AIData.BotOwner.BotsGroup.AddAlly(Main.LocalPlayer);
					ConsoleScreen.Log($"Now an Ally of : {entity.AIData.BotOwner.BotsGroup.Side} Initial WST :{entity.AIData.BotOwner.BotsGroup.InitialBotType}");
				}
				entity.AIData.BotOwner.Memory.GoalTarget.Clear();
				entity.AIData.BotOwner.Memory.GoalEnemy = null;
			}
		}
		catch (Exception arg)
		{
			ConsoleScreen.Log($"Error in Bot Lobotomization code : {arg}");
		}
	}

	public static void MakeBotAlly(Player entity)
	{
		try
		{
			if (entity.AIData.BotOwner.EnemiesController?.EnemyInfos != null && !(Main.LocalPlayer == null) && !entity.AIData.BotOwner.BotsGroup.IsAlly(Main.LocalPlayer) && entity.AIData.BotOwner.BotsGroup.Side == entity.Profile.Side)
			{
				entity.AIData.BotOwner.BotsGroup.RemoveEnemy(Main.LocalPlayer);
				entity.AIData.BotOwner.BotsGroup.AddAlly(Main.LocalPlayer);
				entity.AIData.BotOwner.Memory.GoalTarget.Clear();
				entity.AIData.BotOwner.Memory.GoalEnemy = null;
			}
		}
		catch (Exception arg)
		{
			ConsoleScreen.Log($"Error in Bot Lobotomization code : {arg}");
		}
	}

	public static void GetKnifeOut(Player entity)
	{
		foreach (Item item in entity.AIData.BotOwner.InventoryController.Items)
		{
			entity.AIData.BotOwner.InventoryController.ThrowItem(item);
			entity.AIData.BotOwner.ItemDropper.SetItemToDropNext(item);
			entity.AIData.BotOwner.ItemDropper.TryDoDrop();
		}
	}

	private static Item CreateLoadedBlicky()
	{
		ItemFactory itemFactory = new ItemFactory();
		dynamic val = itemFactory.CreateItem("66015072e9f84d5680039678");
		if (val == null)
		{
			ConsoleScreen.Log("GiveBotsBlickyKit: Failed to create weapon item!");
			return null;
		}
		dynamic val2 = val.AllSlots;
		foreach (dynamic item3 in val2)
		{
			dynamic val3 = item3?.Filters == null;
			if (val3 || item3.Filters.Length == 0)
			{
				continue;
			}
			foreach (dynamic item4 in item3.Filters)
			{
				val3 = item4?.Filter == null;
				if (val3 || item4.Filter.Length == 0)
				{
					continue;
				}
				string text = item4.Filter[UnityEngine.Random.Range(0, item4.Filter.Length)].ToString();
				if (!string.IsNullOrEmpty(text))
				{
					Item item = itemFactory.CreateItem(text);
					if (item != null)
					{
						item.SpawnedInSession = true;
						item3.AddWithoutRestrictions(item);
					}
				}
				break;
			}
		}
		dynamic currentMagazine = val.GetCurrentMagazine();
		if (currentMagazine != null)
		{
			int num = currentMagazine.Cartridges.MaxCount;
			for (int i = 0; i < num; i++)
			{
				Item item2 = itemFactory.CreateItem("6601546f86889319850bd566");
				if (item2 != null)
				{
					item2.SpawnedInSession = true;
					currentMagazine.Cartridges?.Add(item2, false);
				}
			}
		}
		return val;
	}

	public static void GiveBotsBlickyKit(Player entity)
	{
		try
		{
			if (entity == null)
			{
				ConsoleScreen.Log("GiveBotsBlickyKit: Entity is null!");
				return;
			}
			ConsoleScreen.Log("[GiveBotsBlickyKit] Starting for " + entity.Profile?.Nickname);
			if (entity.AIData == null)
			{
				ConsoleScreen.Log("GiveBotsBlickyKit: AIData is null!");
				return;
			}
			BotOwner botOwner = entity.AIData.BotOwner;
			if (botOwner == null)
			{
				ConsoleScreen.Log("GiveBotsBlickyKit: BotOwner is null!");
				return;
			}
			InventoryController inventoryController = botOwner.InventoryController;
			if (inventoryController == null)
			{
				ConsoleScreen.Log("GiveBotsBlickyKit: InventoryController is null!");
				return;
			}
			InventoryEquipment inventoryEquipment = inventoryController.Inventory?.Equipment;
			if (inventoryEquipment == null)
			{
				ConsoleScreen.Log("GiveBotsBlickyKit: Equipment is null!");
				return;
			}
			ItemFactory itemFactory = new ItemFactory();
			Slot slot = inventoryEquipment.GetSlot(EquipmentSlot.FirstPrimaryWeapon);
			Slot slot2 = inventoryEquipment.GetSlot(EquipmentSlot.SecondPrimaryWeapon);
			Slot slot3 = inventoryEquipment.GetSlot(EquipmentSlot.Holster);
			Slot slot4 = inventoryEquipment.GetSlot(EquipmentSlot.Scabbard);
			Slot slot5 = inventoryEquipment.GetSlot(EquipmentSlot.Headwear);
			Slot slot6 = inventoryEquipment.GetSlot(EquipmentSlot.TacticalVest);
			Slot slot7 = inventoryEquipment.GetSlot(EquipmentSlot.FaceCover);
			inventoryEquipment.GetSlot(EquipmentSlot.Pockets);
			IEnumerable<Slot> allSlots = inventoryEquipment.GetAllSlots();
			foreach (Slot item8 in allSlots)
			{
				item8?.RemoveItemWithoutRestrictions();
			}
			if (slot3 != null && slot3.ContainedItem?.TemplateId != (MongoID?)"66015072e9f84d5680039678")
			{
				Item item = CreateLoadedBlicky();
				slot3.RemoveItemWithoutRestrictions();
				slot3.AddWithoutRestrictions(item);
			}
			if (slot2 != null && slot2.ContainedItem?.TemplateId != (MongoID?)"66015072e9f84d5680039678")
			{
				Item item2 = CreateLoadedBlicky();
				slot2.RemoveItemWithoutRestrictions();
				slot2.AddWithoutRestrictions(item2);
			}
			if (slot != null && slot.ContainedItem?.TemplateId != (MongoID?)"66015072e9f84d5680039678")
			{
				Item item3 = CreateLoadedBlicky();
				slot.RemoveItemWithoutRestrictions();
				slot.AddWithoutRestrictions(item3);
			}
			if (slot4 != null && slot4.ContainedItem?.TemplateId != (MongoID?)"6540d2162ae6d96b540afcaf")
			{
				Item item4 = itemFactory.CreateItem("6540d2162ae6d96b540afcaf");
				slot4.RemoveItemWithoutRestrictions();
				slot4.AddWithoutRestrictions(item4);
			}
			if (slot5 != null && slot5.ContainedItem?.TemplateId != (MongoID?)"60bf74184a63fc79b60c57f6")
			{
				dynamic val = itemFactory.CreateItem("60bf74184a63fc79b60c57f6");
				slot5.RemoveItemWithoutRestrictions();
				dynamic val2 = val.AllSlots;
				foreach (dynamic item9 in val2)
				{
					dynamic val3 = item9?.Filters == null;
					if (val3 || item9.Filters.Length == 0)
					{
						continue;
					}
					foreach (dynamic item10 in item9.Filters)
					{
						val3 = item10?.Filter == null;
						if (val3 || item10.Filter.Length == 0)
						{
							continue;
						}
						string text = item10.Filter[UnityEngine.Random.Range(0, item10.Filter.Length)].ToString();
						if (!string.IsNullOrEmpty(text))
						{
							Item item5 = itemFactory.CreateItem(text);
							if (item5 != null)
							{
								item5.SpawnedInSession = true;
								item9.AddWithoutRestrictions(item5);
							}
						}
						break;
					}
				}
				slot5.AddWithoutRestrictions(val);
			}
			if (slot6 != null)
			{
				slot6.RemoveItemWithoutRestrictions();
				dynamic val4 = itemFactory.CreateItem("5fd4c4fa16cac650092f6771");
				if (val4 == null)
				{
					ConsoleScreen.Log("GiveBotsBlickyKit: Failed to create rig!");
					return;
				}
				dynamic val5 = val4.Grids?[0];
				dynamic val6 = val4.Grids?[1];
				dynamic val7 = val4.Grids?[2];
				dynamic val8 = val4?.Grids?[3];
				if (val5 == null)
				{
					ConsoleScreen.Log("GiveBotsBlickyKit: rigGrid is null!");
					return;
				}
				for (int i = 0; i < 4; i++)
				{
					Item item6 = itemFactory.CreateItem("6601546f86889319850bd566");
					if (item6 == null)
					{
						continue;
					}
					item6.SpawnedInSession = true;
					item6.StackObjectsCount = item6.Template.StackMaxSize;
					LocationInGrid locationInGrid = val5.FindFreeSpace(item6);
					if (locationInGrid != null)
					{
						val5.AddItemWithoutRestrictions(item6, locationInGrid);
					}
					else
					{
						if (!((val6 != null) ? true : false))
						{
							continue;
						}
						LocationInGrid locationInGrid2 = val6.FindFreeSpace(item6);
						if (locationInGrid2 != null)
						{
							val6.AddItemWithoutRestrictions(item6, locationInGrid2);
						}
						else if (val7 != null)
						{
							LocationInGrid locationInGrid3 = val7.FindFreeSpace(item6);
							if (locationInGrid3 != null)
							{
								val7.AddItemWithoutRestrictions(item6, locationInGrid3);
							}
						}
					}
				}
				dynamic val9 = itemFactory.CreateItem("5a0c27731526d80618476ac4");
				LocationInGrid locationInGrid4 = val8.FindFreeSpace(val9);
				val8.AddItemWithoutRestrictions(val9, locationInGrid4);
				slot6.AddWithoutRestrictions(val4);
				slot6.ApplyContainedItem();
			}
			if (slot7 != null)
			{
				Item item7 = itemFactory.CreateItem("62a61bbf8ec41a51b34758d2");
				slot7.RemoveItemWithoutRestrictions();
				slot7.AddWithoutRestrictions(item7);
			}
			ConsoleScreen.Log("GiveBotsBlickyKit: Success!");
			foreach (Slot item11 in allSlots)
			{
				item11.ApplyContainedItem();
			}
			entity.AIData.BotOwner.WeaponManager.UpdateWeaponsList();
			entity.TrySetLastEquippedWeapon();
		}
		catch (Exception arg)
		{
			ConsoleScreen.Log($"[GiveBotsBlickyKit] Exception: {arg}");
		}
	}

	public static (string displayName, Color displayColor) GetEntityDisplayInfo(GamePlayer gamePlayer)
	{
		WildSpawnType role = gamePlayer.Player.Profile.Info.Settings.Role;
		string text = gamePlayer.Player.Profile.Info.Nickname.Localized();
		Color white = Color.white;
		if (gamePlayer.Player.Profile.Info.Settings.IsBoss())
		{
			if (Main.IsBossByName(text))
			{
				text = Main.TranslateBossName(text);
				white = Settings.BossandGuardColor;
			}
			else
			{
				switch (role)
				{
				case WildSpawnType.shooterBTR:
					text = "BTR";
					white = Settings.BTRColor;
					break;
				case WildSpawnType.peacefullZryachiyEvent:
				case WildSpawnType.ravangeZryachiyEvent:
					text = "Event Zryachiy";
					white = Settings.BossandGuardColor;
					break;
				case WildSpawnType.bossTagillaAgro:
					text = "Shadow of Tagilla";
					white = Settings.BossandGuardColor;
					break;
				case WildSpawnType.bossKillaAgro:
					text = "Shadow of Killa";
					white = Settings.BossandGuardColor;
					break;
				case WildSpawnType.tagillaHelperAgro:
					text = "Guard";
					white = Settings.GuardColor;
					break;
				case WildSpawnType.infectedTagilla:
					text = "Zombie Tagilla";
					white = Settings.BossandGuardColor;
					break;
				case WildSpawnType.exUsec:
					text = "Rogue";
					white = Settings.RogueColor;
					break;
				case WildSpawnType.pmcUSEC:
					text = "[USEC] " + text;
					white = Settings.USECColor;
					break;
				case WildSpawnType.pmcBEAR:
					text = "[BEAR] " + text;
					white = Settings.BEARColor;
					break;
				case WildSpawnType.followerZryachiy:
				case WildSpawnType.bossBoarSniper:
					text = "Guard";
					white = Settings.GuardColor;
					break;
				case WildSpawnType.pmcBot:
					text = "Raider";
					white = Settings.RaiderColor;
					break;
				case WildSpawnType.sectantPriest:
				case WildSpawnType.sectactPriestEvent:
				case WildSpawnType.sectantPredvestnik:
				case WildSpawnType.sectantPrizrak:
				case WildSpawnType.sectantOni:
					text = "Cultist";
					white = Settings.CultistColor;
					break;
				case WildSpawnType.marksman:
				case WildSpawnType.assault:
				case WildSpawnType.assaultGroup:
					text = "Scav";
					white = Settings.ScavColor;
					break;
				case WildSpawnType.infectedAssault:
				case WildSpawnType.infectedPmc:
				case WildSpawnType.infectedCivil:
				case WildSpawnType.infectedLaborant:
					text = "Zombie";
					white = Settings.ZombieColor;
					break;
				default:
					text = "Unknown Boss: " + text;
					white = Color.red;
					break;
				}
			}
		}
		else
		{
			switch (role)
			{
			case WildSpawnType.sectantWarrior:
				text = "Cultist";
				white = Settings.CultistColor;
				break;
			case WildSpawnType.marksman:
				text = "Scav Sniper";
				white = Settings.ScavColor;
				break;
			case WildSpawnType.shooterBTR:
				text = "BTR";
				white = Settings.BTRColor;
				break;
			case WildSpawnType.infectedAssault:
			case WildSpawnType.infectedPmc:
			case WildSpawnType.infectedCivil:
			case WildSpawnType.infectedLaborant:
				text = "Zombie";
				white = Settings.ZombieColor;
				break;
			case WildSpawnType.arenaFighter:
				text = "Arena Fighter";
				white = Settings.RaiderColor;
				break;
			default:
				if (role != WildSpawnType.marksman && role != WildSpawnType.assault)
				{
					text = "Guard";
					white = Settings.GuardColor;
				}
				else
				{
					text = "Scav";
					white = Settings.ScavColor;
				}
				break;
			}
		}
		return (displayName: text, displayColor: white);
	}
}
