using System;
using EFT.Interactive;
using stupid.solutions.Utils;
using UnityEngine;

namespace stupid.solutions.Data;

public class GameLootItem
{
	private Vector3 screenPosition;

	private Vector2 screenPositionFinal;

	public LootItem LootItem { get; }

	public Vector3 ScreenPosition => screenPosition;

	public Vector2 ScreenPositionFinal => screenPositionFinal;

	public bool IsOnScreen { get; private set; }

	public float Distance { get; private set; }

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

	public string ItemID { get; private set; }

	public string LocalizedName { get; private set; }

	public string ShortName { get; private set; }

	public ItemCategories Itemcat { get; private set; }

	public int? itemprice { get; private set; }

	public int? stackcount { get; private set; }

	public GameLootItem(LootItem lootItem)
	{
		if (lootItem == null)
		{
			throw new ArgumentNullException("lootItem");
		}
		LootItem = lootItem;
		screenPosition = default(Vector3);
		screenPositionFinal = default(Vector2);
		Distance = 0f;
		LocalizedName = string.Empty;
		ItemID = string.Empty;
		ShortName = string.Empty;
		Itemcat = ItemCategories.Uninitialized;
		stackcount = null;
	}

	public void CalculateItemPrice()
	{
		if (!(LootItem == null) && GameUtils.IsLootItemValid(LootItem))
		{
			int? itemPriceBYID = GameUtils.GetItemPriceBYID(LootItem.TemplateId);
			if (LootItem.Item.StackObjectsCount > 0)
			{
				itemprice = itemPriceBYID * LootItem.Item.StackObjectsCount;
				stackcount = LootItem.Item.StackObjectsCount;
			}
			else
			{
				itemprice = itemPriceBYID;
			}
		}
	}

	public void SetItemCat()
	{
		if (!(LootItem == null) && GameUtils.IsLootItemValid(LootItem))
		{
			string text = LootItem.Item.LocalizedName();
			string item = LootItem.Item.TemplateId;
			if (QuestESP.QuestItemIds.Contains(text) || QuestESP.QuestItemIds.Contains(text.ToLower()))
			{
				Itemcat = ItemCategories.Quest;
			}
			else if (Main.IsSuperrare(text))
			{
				Itemcat = ItemCategories.Superrare;
			}
			else if (Main.IsKappa(text))
			{
				Itemcat = ItemCategories.Kappa;
			}
			else if (Main.IsStim(text))
			{
				Itemcat = ItemCategories.Stim;
			}
			else if (Main.SearchedItem(text))
			{
				Itemcat = ItemCategories.Searched;
			}
			else if (Main.wishlistitemids.Contains(item))
			{
				Itemcat = ItemCategories.Wishlist;
			}
			else if (Main.hideoutitemids.Contains(item))
			{
				Itemcat = ItemCategories.Hideout;
			}
			else if (LootItem.Item.LocalizedName() != "Default Inventory")
			{
				Itemcat = ItemCategories.Common;
			}
			else
			{
				Itemcat = ItemCategories.Invalid;
			}
		}
	}

	public void RecalculateDynamics()
	{
		if (GameUtils.IsLootItemValid(LootItem) && !(LootItem.transform == null))
		{
			_ = LootItem.transform.position;
			if (Main.MainCamera != null)
			{
				screenPosition = GameUtils.WorldPointToScreenPoint(LootItem.transform.position);
			}
			screenPositionFinal = new Vector2(screenPosition.x - 50f, screenPosition.y);
			if (screenPosition != Vector3.zero)
			{
				IsOnScreen = GameUtils.IsScreenPointVisible(screenPosition);
			}
			if ((Itemcat != ItemCategories.Quest || Itemcat != ItemCategories.Uninitialized) && QuestESP.QuestItemIds.Contains(LocalizedName) && QuestESP.QuestItemIds.Contains(LocalizedName.ToLower()))
			{
				Itemcat = ItemCategories.Quest;
			}
			if (Main.MainCamera != null)
			{
				Distance = Vector3.Distance(Main.MainCamera.transform.position, LootItem.transform.position);
			}
			if (string.IsNullOrEmpty(LocalizedName))
			{
				LocalizedName = LootItem.Item.LocalizedName();
			}
			if (string.IsNullOrEmpty(ItemID))
			{
				ItemID = LootItem.TemplateId.ToString();
			}
			if (string.IsNullOrEmpty(ShortName))
			{
				ShortName = LootItem.Item.ShortName.Localized();
			}
			if (LootItem.Item.StackObjectsCount > 0 && !stackcount.HasValue)
			{
				stackcount = LootItem.Item.StackObjectsCount;
			}
			else if (!stackcount.HasValue)
			{
				stackcount = 1;
			}
		}
	}
}
