using Common.Extensions;
using Common.Models.Input;
using Common.Models.Output;
using Generator.Weighting;
using System.Collections.Generic;
using System.Linq;

namespace Generator.Helpers.Gear
{
    public static class GearHelpers
    {
        public static void AddEquippedMods(Bot botToUpdate, Datum rawParsedBot)
        {
            var modItemsInRawBot = new List<Item>();
            var itemsWithModsInRawBot = new List<Item>();


                foreach (var inv in rawParsedBot.Inventory.items.Where(x => x.slotId == "mod_magazine"))
                {
                var count = rawParsedBot.Inventory.items.Where(x => x.slotId == "mod_magazine").Count();
                    if (inv._tpl == "60dc519adf4c47305f6d410d")
                    {
                        var y = 1;
                    }
                }

            modItemsInRawBot = rawParsedBot.Inventory.items
                .Where(x => x.slotId != null && (x.slotId.StartsWith("mod_") || x.slotId.StartsWith("patron_in_weapon"))).ToList();

            var x = new List<Item>();
            foreach (var item in rawParsedBot.Inventory.items.Where(x=>x.slotId == "mod_magazine"))
            {
                if (item._tpl == "60dc519adf4c47305f6d410d")
                { 
                    var wow = 1; 
                }
                x.Add(item);
            }

            // get items with Mods by iterating over mod items and getting the parent item
            itemsWithModsInRawBot.AddRange(modItemsInRawBot
                .Select(modItem => rawParsedBot.Inventory.items
                .Find(x => x._id == modItem.parentId)));

            var itemsWithModsDictionary = botToUpdate.inventory.mods;
            foreach (var itemToAdd in itemsWithModsInRawBot)
            {
                var modsToAdd = modItemsInRawBot.Where(x => x.parentId == itemToAdd._id).ToList();

                AddItemToDictionary(itemToAdd, modsToAdd, itemsWithModsDictionary);

                // check if these mods have sub-mods and add those
                foreach (var modAdded in modsToAdd.Where(x=>x.slotId == "mod_magazine"))
                {
                    // look for items where parentId is this mods id
                    var subItems = rawParsedBot.Inventory.items.Where(x => x.parentId == modAdded._id && x.slotId != "cartridges").ToList();
                    if (subItems.Count > 0)
                    {
                        AddItemToDictionary(itemToAdd, subItems, itemsWithModsDictionary);
                    }
                }
            }

            botToUpdate.inventory.mods = itemsWithModsDictionary;
        }

        public static void AddEquippedGear(Bot botToUpdate, Datum bot)
        {
            // add equipped gear
            var weightService = new WeightingService();
            foreach (var inventoryItem in bot.Inventory.items.Where(x=>x.slotId != null))
            {
                switch (inventoryItem.slotId?.ToLower())
                {
                    case "headwear":
                        botToUpdate.inventory.equipment.Headwear.AddUnique(inventoryItem._tpl, weightService.GetItemWeight(inventoryItem._tpl, botToUpdate.botType, "headwear"));
                        break;
                    case "earpiece":
                        botToUpdate.inventory.equipment.Earpiece.AddUnique(inventoryItem._tpl, weightService.GetItemWeight(inventoryItem._tpl, botToUpdate.botType, "earpiece"));
                        break;
                    case "facecover":
                        botToUpdate.inventory.equipment.FaceCover.AddUnique(inventoryItem._tpl, weightService.GetItemWeight(inventoryItem._tpl, botToUpdate.botType, "facecover"));
                        break;
                    case "armorvest":
                        botToUpdate.inventory.equipment.ArmorVest.AddUnique(inventoryItem._tpl, weightService.GetItemWeight(inventoryItem._tpl, botToUpdate.botType, "armorvest"));
                        break;
                    case "eyewear":
                        botToUpdate.inventory.equipment.Eyewear.AddUnique(inventoryItem._tpl, weightService.GetItemWeight(inventoryItem._tpl, botToUpdate.botType, "eyewear"));
                        break;
                    case "armband":
                        botToUpdate.inventory.equipment.ArmBand.AddUnique(inventoryItem._tpl, weightService.GetItemWeight(inventoryItem._tpl, botToUpdate.botType, "armband"));
                        break;
                    case "tacticalvest":
                        botToUpdate.inventory.equipment.TacticalVest.AddUnique(inventoryItem._tpl, weightService.GetItemWeight(inventoryItem._tpl, botToUpdate.botType, "tacticalvest"));
                        break;
                    case "backpack":
                        botToUpdate.inventory.equipment.Backpack.AddUnique(inventoryItem._tpl, weightService.GetItemWeight(inventoryItem._tpl, botToUpdate.botType, "backpack"));
                        break;
                    case "firstprimaryweapon":
                        botToUpdate.inventory.equipment.FirstPrimaryWeapon.AddUnique(inventoryItem._tpl, weightService.GetItemWeight(inventoryItem._tpl, botToUpdate.botType, "firstprimaryweapon"));
                        break;
                    case "secondprimaryweapon":
                        botToUpdate.inventory.equipment.SecondPrimaryWeapon.AddUnique(inventoryItem._tpl, weightService.GetItemWeight(inventoryItem._tpl, botToUpdate.botType, "secondprimaryweapon"));
                        break;
                    case "holster":
                        botToUpdate.inventory.equipment.Holster.AddUnique(inventoryItem._tpl, weightService.GetItemWeight(inventoryItem._tpl, botToUpdate.botType, "holster"));
                        break;
                    case "scabbard":
                        botToUpdate.inventory.equipment.Scabbard.AddUnique(inventoryItem._tpl, weightService.GetItemWeight(inventoryItem._tpl, botToUpdate.botType, "scabbard"));
                        break;
                    case "pockets":
                        botToUpdate.inventory.equipment.Pockets.AddUnique(inventoryItem._tpl, weightService.GetItemWeight(inventoryItem._tpl, botToUpdate.botType, "pockets"));
                        break;
                    case "securedcontainer":
                        botToUpdate.inventory.equipment.SecuredContainer.AddUnique(inventoryItem._tpl, weightService.GetItemWeight(inventoryItem._tpl, botToUpdate.botType, "securedcontainer"));
                        break;
                    default:
                        break;
                }
            }
        }

        public static void AddCartridges(Bot botToUpdate, Datum rawParsedBot)
        {
            var cartridgesInRawBot = rawParsedBot.Inventory.items
                .Where(x => x.slotId?.StartsWith("cartridges") == true).ToList();
            var cartridgeParentIds = cartridgesInRawBot.Select(x => x.parentId).ToList();
            var itemsThatTakeCartridges = rawParsedBot.Inventory.items.Where(x => cartridgeParentIds.Contains(x._id)).ToList();

            var itemsThatTakeCartridgesDict = CreateDictionaryPopulateWithMagazinesAndCartridges(itemsThatTakeCartridges, cartridgesInRawBot);
            foreach (var item in itemsThatTakeCartridgesDict)
            {
                // Item exists update
                if (botToUpdate.inventory.mods.ContainsKey(item.Key))
                {
                    UpdateExistingMagazine(botToUpdate, item);
                }
                else // No item found, add fresh
                {
                    AddNewMagazine(botToUpdate, item);
                }
            }
        }

        private static void AddItemToDictionary(
            Item itemToAdd,
            List<Item> modsToAdd,
            Dictionary<string, Dictionary<string, List<string>>> itemsWithModsDict)
        {
            // item key already exists, need to merge mods
            if (itemsWithModsDict.ContainsKey(itemToAdd._tpl))
            {
                foreach (var modItem in modsToAdd)
                {
                    var itemToAddModsTo = itemsWithModsDict[itemToAdd._tpl];
                    // Item doesnt have this mod, add it then add template id
                    if (!itemToAddModsTo.ContainsKey(modItem.slotId))
                    {
                        // Mod doesnt exist on item
                        itemToAddModsTo.Add(modItem.slotId, new List<string>()); // add mod
                        itemToAddModsTo[modItem.slotId].AddUnique(modItem._tpl);
                    }

                    itemToAddModsTo[modItem.slotId].AddUnique(modItem._tpl); // add template id to it
                }
            }
            else // item doesnt exist, create it
            {
                // Add base item
                itemsWithModsDict.Add(itemToAdd._tpl, new Dictionary<string, List<string>>());
                // Add mod types to item
                foreach (var modItem in modsToAdd)
                {
                    itemsWithModsDict[itemToAdd._tpl].Add(modItem.slotId, new List<string>());
                }

                // Get mod we're adding mod templateIds to
                var modItems = itemsWithModsDict[itemToAdd._tpl];
                foreach (var modItem in modsToAdd)
                {
                    var modToUpdate = modItems[modItem.slotId];
                    modToUpdate.Add(modItem._tpl);
                }
            }
        }


        private static void UpdateExistingMagazine(Bot botToUpdate, KeyValuePair<string, List<string>> item)
        {
            var existingmagazineItem = botToUpdate.inventory.mods[item.Key];
            var cartridges = existingmagazineItem["cartridges"];

            foreach (var itemToAdd in item.Value)
            {
                cartridges.AddUnique(itemToAdd);
            }
        }

        private static void AddNewMagazine(Bot botToUpdate, KeyValuePair<string, List<string>> item)
        {
            var cartridgeDict = new Dictionary<string, List<string>>
                {
                    { "cartridges", item.Value }
                };
            botToUpdate.inventory.mods.Add(item.Key, cartridgeDict);
        }

        private static Dictionary<string, List<string>> CreateDictionaryPopulateWithMagazinesAndCartridges(List<Item> itemsThatTakeCartridges, List<Item> cartridgesInRawBot)
        {
            var itemsThatTakeCartridgesDict = new Dictionary<string, List<string>>();
            foreach (var item in itemsThatTakeCartridges)
            {
                var cartridgeIdsToAdd = cartridgesInRawBot.Where(x => x.parentId == item._id).Select(x => x._tpl).ToList();

                // magazine id already exists, probably has cartridges in it already
                if (itemsThatTakeCartridgesDict.ContainsKey(item._tpl))
                {
                    //get existing magazine and add new cartridges to it
                    var existingMagazine = itemsThatTakeCartridgesDict[item._tpl];
                    foreach (var cartridge in cartridgeIdsToAdd)
                    {
                        existingMagazine.AddUnique(cartridge);
                    }
                }
                else // No magazine found, add new magazine + associated cartridges
                {
                    itemsThatTakeCartridgesDict.Add(item._tpl, cartridgeIdsToAdd);
                }
            }

            return itemsThatTakeCartridgesDict;
        }
    }
}