using AppleHills.Data.CardSystem;
using Core;
using Data.CardSystem;
using UI.DragAndDrop.Core;
using UnityEngine;
namespace UI.CardSystem
{
///
/// Specialized slot for album pages that only accepts a specific card.
/// Validates cards based on their CardDefinition.
/// Self-populates with owned cards when enabled.
///
public class AlbumCardSlot : DraggableSlot
{
[Header("Album Slot Configuration")]
[SerializeField] private CardDefinition targetCardDefinition; // Which card this slot accepts
[SerializeField] private GameObject cardPrefab; // Card prefab to spawn when card is owned
private StateMachine.Card _placedCard;
private void OnEnable()
{
// Check if we should spawn a card for this slot
CheckAndSpawnOwnedCard();
}
///
/// Check if player owns the card for this slot and spawn it if so
///
private void CheckAndSpawnOwnedCard()
{
// Guard: need CardSystemManager and target definition
if (CardSystemManager.Instance == null || targetCardDefinition == null)
return;
// Guard: don't spawn if already occupied
if (_placedCard != null)
return;
// Guard: need prefab to spawn
if (cardPrefab == null)
{
Logging.Warning($"[AlbumCardSlot] No cardPrefab assigned for slot targeting {targetCardDefinition.name}");
return;
}
// Check if player owns this card at ANY rarity (prioritize highest rarity)
CardData ownedCard = null;
// Check in order: Legendary > Rare > Normal
foreach (CardRarity rarity in new[] { CardRarity.Legendary, CardRarity.Rare, CardRarity.Normal })
{
CardData card = CardSystemManager.Instance.GetCardInventory().GetCard(targetCardDefinition.Id, rarity);
if (card != null)
{
ownedCard = card;
break; // Found highest rarity owned
}
}
// Spawn card if owned
if (ownedCard != null)
{
SpawnCard(ownedCard);
}
}
///
/// Spawn a Card in this slot using the PlacedInSlotState
///
private void SpawnCard(CardData cardData)
{
GameObject cardObj = Instantiate(cardPrefab, transform);
var card = cardObj.GetComponent();
if (card != null)
{
// Setup card for album slot (starts in PlacedInSlotState)
card.SetupForAlbumSlot(cardData, this);
_placedCard = card;
// Resize the card to match the slot size
RectTransform cardRect = card.transform as RectTransform;
RectTransform slotRect = transform as RectTransform;
if (cardRect != null && slotRect != null)
{
// Set height to match slot height (AspectRatioFitter will handle width)
float targetHeight = slotRect.rect.height;
cardRect.sizeDelta = new Vector2(cardRect.sizeDelta.x, targetHeight);
// Ensure position and rotation are centered
cardRect.localPosition = Vector3.zero;
cardRect.localRotation = Quaternion.identity;
}
// Register with AlbumViewPage for enlarge/shrink handling
AlbumViewPage albumPage = FindFirstObjectByType();
if (albumPage != null)
{
albumPage.RegisterCardInAlbum(card);
}
Logging.Debug($"[AlbumCardSlot] Spawned owned card '{cardData.Name}' ({cardData.Rarity}) in slot");
}
else
{
Logging.Warning($"[AlbumCardSlot] Spawned prefab has no Card component!");
Destroy(cardObj);
}
}
///
/// Get the target card definition for this slot
///
public CardDefinition TargetCardDefinition => targetCardDefinition;
///
/// Get the target card definition for this slot (method version for compatibility)
///
public CardDefinition GetTargetCardDefinition() => targetCardDefinition;
}
}