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

namespace stupid.solutions.Data;

public class GameLootContainer
{
	private Vector3 screenPosition;

	public LootableContainer LootableContainer { get; }

	public List<ContainerItem> LootItems { get; private set; }

	public bool ItemInit { get; private set; }

	public Vector3 ScreenPosition => screenPosition;

	public bool IsOnScreen { get; private set; }

	public float Distance { get; private set; }

	public string FormattedDistance => $"{Math.Round(Distance)}m";

	public int? totalprice { get; private set; }

	public int CachedItemCount { get; private set; }

	public string ContainerName { get; set; }

	public GameLootContainer(LootableContainer lootableContainer)
	{
		if (lootableContainer == null)
		{
			throw new ArgumentNullException("lootableContainer");
		}
		LootableContainer = lootableContainer;
		screenPosition = default(Vector3);
		Distance = 0f;
		LootItems = new List<ContainerItem>();
		ItemInit = false;
		ContainerName = "Container";
		totalprice = null;
		CachedItemCount = 0;
	}

	public void RecalculateDynamics()
	{
		if (GameUtils.IsLootableContainerValid(LootableContainer))
		{
			screenPosition = GameUtils.WorldPointToScreenPoint(LootableContainer.transform.position);
			IsOnScreen = GameUtils.IsScreenPointVisible(screenPosition);
			Distance = Vector3.Distance(Main.MainCamera.transform.position, LootableContainer.transform.position);
		}
	}

	public void RefreshItems()
	{
		LootItems.Clear();
		int num = 0;
		foreach (Item allItem in LootableContainer.ItemOwner.RootItem.GetAllItems())
		{
			if (allItem == null)
			{
				continue;
			}
			if (IsContainerName(allItem.LocalizedName()))
			{
				ContainerName = allItem.LocalizedName();
				continue;
			}
			num++;
			ContainerItem lootItem = new ContainerItem(allItem);
			if (lootItem.Item.StackObjectsCount > lootItem.Item.StackMaxSize || lootItem.Item.StackObjectsCount > 100)
			{
				continue;
			}
			lootItem.RecalculateDynamics();
			lootItem.SetItemCat();
			ContainerItem containerItem = LootItems.FirstOrDefault((ContainerItem i) => i.ItemID == lootItem.ItemID);
			if (containerItem != null)
			{
				containerItem.Count += lootItem.Count + (lootItem.Item.StackObjectsCount - 1);
				if (containerItem.Count > 1500)
				{
					containerItem.Count -= 1500;
				}
				if (containerItem.Count > 500)
				{
					containerItem.Count -= 500;
				}
			}
			else
			{
				if (lootItem.Item.StackObjectsCount > 1)
				{
					lootItem.Count += lootItem.Item.StackObjectsCount - 1;
				}
				LootItems.Add(lootItem);
			}
		}
		foreach (ContainerItem lootItem2 in LootItems)
		{
			lootItem2.CalculateItemPrice();
		}
		CachedItemCount = num;
		totalprice = LootItems.Sum((ContainerItem item) => item.itemprice);
		ItemInit = true;
	}

	public bool IsContainerName(string name)
	{
		switch (name)
		{
		case "Toolbox":
		case "Duffle bag":
		case "Wooden ammo box":
		case "Grenade box":
		case "Weapon box":
		case "Wooden crate":
		case "Plastic suitcase":
		case "Dead Scav":
		case "Ground cache":
		case "Buried barrel cache":
		case "PC block":
		case "Jacket":
		case "Drawer":
		case "Cash register":
		case "Medbag SMU06":
		case "Medcase":
		case "Safe":
		case "Technical supply crate":
		case "Ration supply crate":
		case "Medical supply crate":
		case "Civilian body":
		case "PMC body":
		case "Scav body":
		case "Lab technician body":
		case "Bank safe":
		case "Bank cash register":
		case "Shturman's Stash":
			return true;
		default:
			return false;
		}
	}
}
