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

namespace stupid.solutions.Data;

public class GamePlayer
{
	private Vector3 screenPosition;

	private Vector3 headScreenPosition;

	public Player Player { get; }

	public Vector3 ScreenPosition => screenPosition;

	public Vector3 HeadScreenPosition => headScreenPosition;

	public bool IsOnScreen { get; private set; }

	public bool IsVisible { get; private set; }

	public float Distance { get; private set; }

	public bool ItemInit { get; private set; }

	public float DistanceFromCenter { get; set; }

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

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

	public int? totalinventoryprice { get; private set; }

	public bool isselected { get; set; }

	public GamePlayer(Player player)
	{
		if (player == null)
		{
			throw new ArgumentNullException("player");
		}
		Player = player;
		screenPosition = default(Vector3);
		headScreenPosition = default(Vector3);
		IsOnScreen = false;
		Distance = 0f;
		LootItems = new List<ContainerItem>();
		ItemInit = false;
		totalinventoryprice = null;
	}

	private void DoRefreshItems() 
	{
        LootItems.Clear();

        foreach (Item allItem in Player.Profile.Inventory.Equipment.GetAllItems())
        {
            if (allItem == null)
                continue;

            ContainerItem lootItem = new ContainerItem(allItem);
            lootItem.RecalculateDynamics();
            lootItem.SetItemCat();
            ContainerItem containerItem = LootItems.FirstOrDefault((ContainerItem i) => i.ItemID == lootItem.ItemID);
            if (containerItem != null)
            {
                if (lootItem.Item.StackObjectsCount <= lootItem.Item.StackMaxSize && 100 >= lootItem.Item.StackObjectsCount)
                {
                    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 <= lootItem.Item.StackMaxSize && 100 >= lootItem.Item.StackObjectsCount)
            {
                if (lootItem.Item.StackObjectsCount > 1)
                {
                    lootItem.Count += lootItem.Item.StackObjectsCount - 1;
                }
                LootItems.Add(lootItem);
            }
        }
        foreach (ContainerItem lootItem2 in LootItems)
        {
            lootItem2.CalculateItemPrice();
        }
        totalinventoryprice = LootItems.Sum((ContainerItem item) => item.itemprice);
        ItemInit = true;
    }

	public void RefreshItems()
	{
		try
		{
			DoRefreshItems();
		}
		catch (Exception arg)
		{
			ConsoleScreen.Log($"Error whilst refreshing GamePlayers Inventory Items : {arg} ");
		}
	}

	public void RecalculateDynamics()
	{
		try
		{
			if (!GameUtils.IsPlayerValid(Player))
			{
				return;
			}
			screenPosition = GameUtils.WorldPointToScreenPoint(Player.Transform.position);
			if (Menu2.selectedEntity != null)
			{
				if (Player == Menu2.selectedEntity)
				{
					isselected = true;
				}
				else
				{
					isselected = false;
				}
			}
			if (Player.PlayerBones != null)
			{
				headScreenPosition = GameUtils.WorldPointToScreenPoint(Player.PlayerBones.Head.position);
			}
			IsOnScreen = GameUtils.IsScreenPointVisible(screenPosition);
			Distance = Vector3.Distance(Main.MainCamera.transform.position, Player.Transform.position);
		}
		catch (Exception arg)
		{
			ConsoleScreen.Log($"Error whilst Recalculating Dynamics for GamePlayer Instance : {arg}");
		}
	}
}
