using System.Collections.Generic;
using GTANetworkAPI;
using ReallifeGamemode.Server.Inventory.Interfaces;
using ReallifeGamemode.Server.Managers;
using System.Linq;
using ReallifeGamemode.Server.Entities;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Server.Models;

namespace ReallifeGamemode.Server.Inventory
{
    public class GroundItem : Script
    {
        public int ItemId { get; set; }
        public int Amount { get; set; }
        public Vector3 Position { get; set; }

        public static List<GroundItem> GroundItems = new List<GroundItem>();
        public static List<GTANetworkAPI.Object> GroundObjects = new List<GTANetworkAPI.Object>();
        public static List<TextLabel> GroundTextLabels = new List<TextLabel>();

        public static void AddGroundItem(GroundItem grndItem, GTANetworkAPI.Object grndObject, TextLabel grndTextLabel)
        {
            GroundItems.Add(grndItem);
            GroundObjects.Add(grndObject);
            GroundTextLabels.Add(grndTextLabel);
        }

        public static void PickUpGroundItem(Client player)
        {
            GroundItem nearest = GroundItems.FirstOrDefault(d => d.Position.DistanceTo(player.Position) <= 1.2);
            if (nearest != null)
            {
                var invWeight = InventoryManager.GetUserInventoryWeight(player);
                var itemsToAdd = 0;
                GTANetworkAPI.Object nearestObject = GroundObjects.FirstOrDefault(d => d.Position == nearest.Position);
                TextLabel nearestTextLabel = GroundTextLabels.FirstOrDefault(d => d.Position == nearest.Position);
                IItem nearestItem = InventoryManager.GetItemById(nearest.ItemId);
                UserItem existingItem = InventoryManager.UserHasThisItem(player, nearest.ItemId);
                var user = player.GetUser();
                if (nearestItem.Gewicht * nearest.Amount + invWeight > 40000)
                {
                    for (var i = 1; i <= nearest.Amount; i++)
                    {
                        if (invWeight + (i * nearestItem.Gewicht) > 40000)
                        {
                            break;
                        }
                        else
                        {
                            itemsToAdd = i;
                        }
                    }
                    if (itemsToAdd < 1)
                    {
                        player.SendNotification("~r~Du hast keinen Platz im Inventar!", false);
                    }
                    else
                    {
                        if (existingItem != null)
                        {
                            using (var context = new DatabaseContext())
                            {
                                UserItem existingUserItem = context.UserItems.FirstOrDefault(i => i.Id == existingItem.Id);
                                existingUserItem.Amount += itemsToAdd;
                                context.SaveChanges();
                            }
                        }
                        else
                        {
                            UserItem newItem = new UserItem { ItemId = nearest.ItemId, UserId = user.Id, Amount = nearest.Amount };
                            InventoryManager.AddItemToInventory(player, newItem);
                        }
                        nearest.Amount -= itemsToAdd;
                        nearestTextLabel.Text = nearestItem.Name + " ~s~(~y~" + nearest.Amount + "~s~)";
                        player.SendNotification("Du hast nur ~g~" + itemsToAdd + " ~y~" + nearestItem.Name + "~s~ aufgehoben.");
                    }
                }
                else
                {
                    if (existingItem != null)
                    {
                        using (var context = new DatabaseContext())
                        {
                            UserItem existingUserItem = context.UserItems.FirstOrDefault(i => i.Id == existingItem.Id && i.UserId == user.Id);
                            existingUserItem.Amount += nearest.Amount;
                            context.SaveChanges();
                        }
                    }
                    else
                    {
                        UserItem item = new UserItem() { ItemId = nearest.ItemId, UserId = user.Id, Amount = nearest.Amount };
                        InventoryManager.AddItemToInventory(player, item);
                    }
                    RemoveGroundItem(nearest, nearestObject, nearestTextLabel);
                    player.SendNotification("Du hast ~g~" + nearest.Amount + " ~y~" + nearestItem.Name + " ~s~aufgehoben.");
                }
            }
        }

        public static void RemoveGroundItem(GroundItem grndItem, GTANetworkAPI.Object grndObject, TextLabel grndTextLabel)
        {
            GroundItems.Remove(grndItem);
            NAPI.Entity.DeleteEntity(grndObject);
            NAPI.Entity.DeleteEntity(grndTextLabel);
            GroundObjects.Remove(grndObject);
            GroundTextLabels.Remove(grndTextLabel);
        }
    }
}
