using EFT;
using EFT.InventoryLogic;
using EscapeFromTarkovCheat.Utils;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace EscapeFromTarkovCheat
{
    // Token: 0x02000006 RID: 6
    public static class ItemFeatures
    {
        public static List<Item> collectedItems = new List<Item>();
        public static string _searchQuery = "";
        public static string _itemStringText = "";
        public static string _id = "";
        private static int id = 0;
        public static string _width = "";
        private static int width = 0;
        public static string _height = "";
        private static int height = 0;
        private static float _logTime = Time.time;

        public static string ItemStringText
        {
            get
            {
                return _itemStringText;
            }
        }

        public static string Id
        {
            get
            {
                return _id;
            }
        }

        public static string Width
        {
            get
            {
                return _width;
            }
        }

        public static string Height
        {
            get
            {
                return _height;
            }
        }

        public static string GetSearchQuery() => _searchQuery;

        public static void GetItemsInInventory()
        {
            GameUtils.AddConsoleLog("GetItemsInInventory called");
            collectedItems.Clear();
            List<string> itemStrings = new List<string>();
            EquipmentSlot[] slots = { EquipmentSlot.Backpack };
            if (Main.notReady())
            {
                GameUtils.AddConsoleLog("You are not in raid.");
                return;
            }

            if (Main.LocalPlayer.Profile == null)
            {
                GameUtils.AddConsoleLog("The current raid is in the state of loading.");
                return;
            }

            if (Main.LocalPlayer.Profile.Inventory == null)
            {
                GameUtils.AddConsoleLog("Your Inventory class currently has no instance.");
                return;
            }

            IEnumerable<Item> items = Main.LocalPlayer.Profile.Inventory.GetItemsInSlots(slots);

            if (items == null || items.Count() == 0)
            {
                GameUtils.AddConsoleLog("Your inventory is either empty or not loaded.");
                return;
            }

            GameUtils.AddConsoleLog($"Found {items.Count()} items in backpack");
            foreach (Item item in items)
            {
                if (!GameUtils.IsInventoryItemValid(item))
                    continue;
                if (!item.Name.Localized().ToLower().Contains(_searchQuery.ToLower()))
                    continue;

                itemStrings.Add(item.Name.Localized());
                collectedItems.Add(item);
                GameUtils.AddConsoleLog($"Added item: {item.Name.Localized()}");
            }
            _itemStringText = string.Join("\n", itemStrings);
        }

        public static void Commit()
        {
            GameUtils.AddConsoleLog("SetItemsInInventory called");
            if (collectedItems.Count <= 0)
            {
                GameUtils.AddConsoleLog("No items in collectedItems");
                return;
            }
            foreach (Item item in collectedItems)
            {
                if (!item.Name.ToLower().Contains(_searchQuery.ToLower()))
                    continue;

                item.Template._id = _id;
                GameUtils.AddConsoleLog($"Set {item.Name} _id to {_id}");
                if (int.TryParse(_width, out width)) // 字符串转整数 : Casting string to int
                {
                    item.Template.Width = width;
                    GameUtils.AddConsoleLog($"Set {item.Name.Localized()} Width to {width}");
                }

                if (int.TryParse(_height, out height))
                {
                    item.Template.Height = height;
                    GameUtils.AddConsoleLog($"Set {item.Name.Localized()} Height to {height}");
                }
            }
        }

        public static void ResetItemsInInventory()
        {
            GameUtils.AddConsoleLog("ResetItemsInInventory called");
            DupeItemsInInventory(1);
        }

        public static void DupeItemsInInventory(int count)
        {
            GameUtils.AddConsoleLog($"DupeItemsInInventory with argument {count} called");
            if (collectedItems.Count <= 0)
            {
                GameUtils.AddConsoleLog("No items in collectedItems");
                return;
            }

            foreach (Item item in collectedItems)
            {
                item.StackObjectsCount = count;
                GameUtils.AddConsoleLog($"Duped {item.Name.Localized()} stack to {count}");
            }
        }

        public static void DupeDollarsEuros()
        {
            DupeItemsInInventory(50000);
        }

        public static void DupeRubles()
        {
            DupeItemsInInventory(400000);
        }

        public static void SetInventoryFoundInRaid()
        {
            GameUtils.AddConsoleLog("FoundInRaidInventory called");
            if (Main.notReady())
            {
                GameUtils.AddConsoleLog("You are not in raid.");
                return;
            }

            if (Main.LocalPlayer.Profile == null)
            {
                GameUtils.AddConsoleLog("The current raid is in the state of loading.");
                return;
            }

            if (Main.LocalPlayer.Profile.Inventory == null)
            {
                GameUtils.AddConsoleLog("Your Inventory class currently has no instance.");
                return;
            }
            IEnumerable<Item> items = Main.LocalPlayer.Profile.Inventory.GetPlayerItems(EPlayerItems.All);
            if (items == null || items.Count() == 0)
            {
                GameUtils.AddConsoleLog("Your inventory is either empty or not loaded.");
                return;
            }

            GameUtils.AddConsoleLog($"Found {items.Count()} items in inventory");

            foreach (Item item in items)
            {
                if (!GameUtils.IsInventoryItemValid(item))
                    continue;
                item.SpawnedInSession = true;
                GameUtils.AddConsoleLog($"Set {item.Name.Localized()} to SpawnedInSession");
            }
        }

        public static void SetSearchQuery(string searchQuery)
        {
            _searchQuery = searchQuery;
            if (Time.time > _logTime && Settings.debug)
            {
                GameUtils.AddConsoleLog($"Search query set to: {searchQuery}");
                _logTime = Time.time + 1f;
            }
        }

        public static void UpdateValues(string id, string width, string height)
        {
            _id = id;
            _width = width;
            _height = height;
            if (Time.time > _logTime && Settings.debug)
            {
                GameUtils.AddConsoleLog($"New values set: {id}, {width}, {height}");
                _logTime = Time.time + 1f;
            }
        }
    }
}
