using System.Collections.Generic;
using Core;
using UI.DragAndDrop.Core;
using UnityEngine;
namespace UI.CardSystem
{
///
/// Helper utility for shuffling draggable objects in a SlotContainer.
/// Moves objects to occupy the first available slots (0, 1, 2, etc.)
///
public static class SlotContainerHelper
{
///
/// Shuffles draggable objects to always occupy the first available slots.
/// Unassigns all objects from their current slots, then reassigns them starting from slot 0.
///
/// The slot container holding the slots
/// List of draggable objects to shuffle
/// Whether to animate the movement
public static void ShuffleToFront(SlotContainer container, List objects, bool animate = true)
{
if (container == null || objects == null || objects.Count == 0)
return;
Logging.Debug($"[SlotContainerHelper] Shuffling {objects.Count} objects to front slots");
// Unassign all objects from their current slots
foreach (var obj in objects)
{
if (obj.CurrentSlot != null)
{
obj.CurrentSlot.Vacate();
}
}
// Reassign objects to first N slots starting from slot 0
for (int i = 0; i < objects.Count; i++)
{
DraggableSlot targetSlot = FindSlotByIndex(container, i);
DraggableObject obj = objects[i];
if (targetSlot != null)
{
Logging.Debug($"[SlotContainerHelper] Assigning object to slot with SlotIndex {i}");
obj.AssignToSlot(targetSlot, animate);
}
else
{
Logging.Warning($"[SlotContainerHelper] Could not find slot with SlotIndex {i}");
}
}
}
///
/// Find a slot by its SlotIndex property (not list position)
///
private static DraggableSlot FindSlotByIndex(SlotContainer container, int slotIndex)
{
foreach (var slot in container.Slots)
{
if (slot.SlotIndex == slotIndex)
{
return slot;
}
}
return null;
}
}
}