private static void ReplaceBag(WAEContainer bagToReplace, WAEItem newBag)
    {
        if (bagToReplace.EmptyBagInOtherBags())
        {
            Logger.Log($"Replacing {bagToReplace.ThisBag.Name} with {newBag.Name}");

            if (newBag.InBag == bagToReplace.Position)
            {
                newBag = AllItems.Find(b => b.ItemLink == newBag.ItemLink);
            }

            newBag.MoveToBag(bagToReplace.Position);

            WAELootFilter.ProtectFromFilter(newBag.ItemLink);
            Lua.LuaDoString($"EquipPendingItem(0);");
            //Lua.LuaDoString($"StaticPopup1Button1:Click()");
        }
        Scan();
    }
    public bool EquipSelectRoll(int slotId, string reason)
    {
        WAELootFilter.ProtectFromFilter(ItemLink);

        // ROLL
        if (RollId >= 0)
        {
            WAEGroupRoll.Roll(RollId, this, reason, RollType.NEED);
            HasBeenRolled = true;
            WAEContainers.AllItems.Clear();
            return(true);
        }

        // SELECT REWARD
        if (RewardSlot >= 0)
        {
            Lua.LuaDoString($"GetQuestReward({RewardSlot})");
            Logger.Log($"Selecting quest reward {Name} [{reason}]");
            WAEContainers.AllItems.Clear();
            return(true);
        }

        // EQUIP
        WAECharacterSheetSlot slot = WAECharacterSheet.AllSlots.Find(s => s.InventorySlotID == slotId);

        if (slot.Item?.ItemLink == ItemLink)
        {
            return(true);
        }

        if (ItemSubType != "Arrow" && ItemSubType != "Bullet")
        {
            if (ObjectManager.Me.InCombatFlagOnly || ObjectManager.Me.IsCast)
            {
                return(false);
            }
        }

        if (InBag < 0 || InBagSlot < 0)
        {
            Logger.LogError($"Item {Name} is not recorded as being in a bag. Can't use.");
        }
        else
        {
            Logger.Log($"Equipping {Name} ({WeightScore}) [{reason}]");
            ItemEquipAttempts.Add(ItemLink);
            PickupFromBag();
            DropInInventory(slotId);
            ToolBox.Sleep(100);
            Lua.LuaDoString($"EquipPendingItem(0);");
            //Lua.LuaDoString($"StaticPopup1Button1:Click()");
            ToolBox.Sleep(200);
            WAECharacterSheet.Scan();
            WAEContainers.Scan();
            WAECharacterSheetSlot updatedSlot = WAECharacterSheet.AllSlots.Find(s => s.InventorySlotID == slotId);
            if (updatedSlot.Item == null || updatedSlot.Item.ItemLink != ItemLink)
            {
                if (GetNbEquipAttempts() < _maxNbEquipAttempts)
                {
                    Logger.LogError($"Failed to equip {Name}. Retrying soon ({GetNbEquipAttempts()}).");
                }
                else
                {
                    Logger.LogError($"Failed to equip {Name} after {GetNbEquipAttempts()} attempts.");
                }

                Lua.LuaDoString($"ClearCursor()");
                return(false);
            }
            ItemEquipAttempts.RemoveAll(i => i == ItemLink);
            WAELootFilter.AllowForFilter(ItemLink);
            return(true);
        }
        return(false);
    }
    public static void BagEquip()
    {
        //Logger.LogDebug("*** Bag equip...");
        DateTime dateBegin = DateTime.Now;

        if (AutoEquipSettings.CurrentSettings.AutoEquipBags)
        {
            bool         ImHunterAndNeedAmmoBag  = ObjectManager.Me.WowClass == WoWClass.Hunter && AutoEquipSettings.CurrentSettings.EquipQuiver;
            int          maxAmountOfBags         = ImHunterAndNeedAmmoBag ? 4 : 5;
            WAEContainer equippedQuiver          = ListContainers.Find(bag => bag.IsQuiver);
            WAEContainer equippedAmmoPouch       = ListContainers.Find(bag => bag.IsAmmoPouch);
            bool         hasRangedWeaponEquipped = WAECharacterSheet.Ranged.Item != null;
            string       equippedRanged          = WAECharacterSheet.Ranged.Item?.ItemSubType;

            if (AutoEquipSettings.CurrentSettings.EquipQuiver)
            {
                // Move ammoContainer to position 4
                if (equippedQuiver != null && equippedQuiver.Position != 4)
                {
                    equippedQuiver.MoveToSlot(4);
                    Scan();
                    equippedQuiver = ListContainers.Find(bag => bag.IsQuiver);
                }
                if (equippedAmmoPouch != null && equippedAmmoPouch.Position != 4)
                {
                    equippedAmmoPouch.MoveToSlot(4);
                    Scan();
                    equippedAmmoPouch = ListContainers.Find(bag => bag.IsAmmoPouch);
                }

                // We have an ammo container equipped
                if (ImHunterAndNeedAmmoBag && (equippedQuiver != null || equippedAmmoPouch != null))
                {
                    WAEContainer equippedAmmoContainer   = equippedQuiver == null ? equippedAmmoPouch : equippedQuiver;
                    WAEItem      bestAmmoContainerInBags = GetBiggestAmmoContainerFromBags();

                    if (bestAmmoContainerInBags != null)
                    {
                        // Check we have the right type of ammo container
                        if ((equippedRanged == TypeRanged.Bows.ToString() || equippedRanged == TypeRanged.Crossbows.ToString()) && !equippedAmmoContainer.IsQuiver)
                        {
                            ReplaceBag(equippedAmmoContainer, bestAmmoContainerInBags);
                        }
                        else if (equippedRanged == TypeRanged.Guns.ToString() && !equippedAmmoContainer.IsAmmoPouch)
                        {
                            ReplaceBag(equippedAmmoContainer, bestAmmoContainerInBags);
                        }
                        // Try to find a better one
                        else if (bestAmmoContainerInBags.QuiverCapacity > equippedAmmoContainer.Capacity ||
                                 bestAmmoContainerInBags.AmmoPouchCapacity > equippedAmmoContainer.Capacity)
                        {
                            ReplaceBag(equippedAmmoContainer, bestAmmoContainerInBags);
                        }
                    }
                }

                // We have no ammo container equipped
                if (ImHunterAndNeedAmmoBag && equippedQuiver == null && equippedAmmoPouch == null)
                {
                    if (!hasRangedWeaponEquipped || equippedRanged == TypeRanged.Thrown.ToString())
                    {
                        maxAmountOfBags = 5;
                    }
                    else
                    {
                        WAEItem bestAmmoContainerInBags = GetBiggestAmmoContainerFromBags();
                        // We found an ammo container to equip
                        if (bestAmmoContainerInBags != null)
                        {
                            if (GetEmptyContainerSlots().Count > 0)
                            {
                                // There is an empty slot
                                int availableSpot = GetEmptyContainerSlots().Last();
                                Logger.Log($"Equipping {bestAmmoContainerInBags.Name} in slot {availableSpot}");
                                bestAmmoContainerInBags.MoveToBag(availableSpot);

                                Lua.LuaDoString($"EquipPendingItem(0);");
                                //Lua.LuaDoString($"StaticPopup1Button1:Click()");
                                WAELootFilter.ProtectFromFilter(bestAmmoContainerInBags.ItemLink);

                                Scan();
                            }
                            else
                            {
                                // No empty slot, we need to replace a bag by an ammo container
                                WAEContainer smallestEquippedBag = GetSmallestEquippedBag();
                                ReplaceBag(smallestEquippedBag, bestAmmoContainerInBags);
                            }
                        }
                    }
                }
            }
            else
            {
                // The user doesn't want to have quiver equipped, removing them
                if (equippedQuiver != null || equippedAmmoPouch != null)
                {
                    WAEContainer equippedAmmoContainer = equippedQuiver == null ? equippedAmmoPouch : equippedQuiver;
                    WAEItem      biggestBagInBags      = GetBiggestBagFromBags();
                    if (biggestBagInBags != null)
                    {
                        ReplaceBag(equippedAmmoContainer, biggestBagInBags);
                    }
                }
            }

            // Bag equip if we have at least 1 empty slot
            if (GetNbBagEquipped() < maxAmountOfBags)
            {
                List <int> emptyContainerSlots = GetEmptyContainerSlots();
                int        nbEmpty             = emptyContainerSlots.Count;
                int        nbloop = emptyContainerSlots.Count;

                foreach (int emptySlotId in emptyContainerSlots)
                {
                    WAEItem biggestBag = GetBiggestBagFromBags();

                    if (biggestBag != null)
                    {
                        Logger.Log($"Equipping {biggestBag.Name}");
                        int availableSpot = GetEmptyContainerSlots().First();
                        biggestBag.MoveToBag(availableSpot);

                        Lua.LuaDoString($"EquipPendingItem(0);");
                        //Lua.LuaDoString($"StaticPopup1Button1:Click()");
                        WAELootFilter.ProtectFromFilter(biggestBag.ItemLink);

                        Scan();
                    }
                    if (GetNbBagEquipped() >= maxAmountOfBags)
                    {
                        break;
                    }
                }
            }

            // Bag equip to replace one for better capacity
            if (GetNbBagEquipped() >= maxAmountOfBags)
            {
                WAEContainer smallestEquippedBag = GetSmallestEquippedBag();
                WAEItem      biggestBagInBags    = GetBiggestBagFromBags();

                if (smallestEquippedBag != null &&
                    biggestBagInBags != null &&
                    smallestEquippedBag.Capacity < biggestBagInBags.BagCapacity &&
                    smallestEquippedBag.Position != 0)
                {
                    ReplaceBag(smallestEquippedBag, biggestBagInBags);
                }
            }
        }

        Logger.LogPerformance($"Bag Equip Process time : {(DateTime.Now.Ticks - dateBegin.Ticks) / 10000} ms");
    }