using System.Collections.Generic;
using AppleHills.Data.CardSystem;
using Core;
using Data.CardSystem;
using Pixelplacement;
using UnityEngine;
using UnityEngine.UI;
namespace AppleHills.UI.CardSystem
{
///
/// UI page for viewing and organizing the player's card collection in an album.
///
public class AlbumViewPage : UIPage
{
[Header("Album UI Elements")]
[SerializeField] private GridLayoutGroup albumGrid;
[SerializeField] private RectTransform cardStackContainer;
[SerializeField] private GameObject emptyAlbumMessage;
[SerializeField] private GameObject cardSlotPrefab;
[SerializeField] private GameObject cardPrefab;
// Runtime references
private CardSystemManager _cardManager;
private List _displayedCards = new List();
private Dictionary _albumSlots = new Dictionary();
// Drag and drop handling
private CardUIElement _currentlyDraggedCard = null;
private Vector3 _cardOriginalPosition;
private void Awake()
{
_cardManager = CardSystemManager.Instance;
}
///
/// Sets up the album when the page becomes active
///
public override void TransitionIn()
{
base.TransitionIn();
// Initialize the album when the page becomes active
InitializeAlbum();
}
///
/// Initializes the album with card slots and the player's collection
///
private void InitializeAlbum()
{
// Clear any previous setup
ClearAlbum();
// Get all collected cards
List collectedCards = _cardManager.GetAllCollectedCards();
// Show/hide empty message based on collection
if (emptyAlbumMessage != null)
{
emptyAlbumMessage.SetActive(collectedCards.Count == 0);
}
if (collectedCards.Count == 0)
{
return;
}
// Set up the album slots
SetupAlbumSlots();
// Create card UI elements for the stack
CreateCardStack(collectedCards);
}
///
/// Sets up empty slots in the album grid
///
private void SetupAlbumSlots()
{
if (albumGrid == null || cardSlotPrefab == null) return;
// Create predefined slots in the album
// For a simple implementation, we'll create a 5x5 grid of slots
int slotsPerZone = 5; // 5 slots per zone (one row)
int totalZones = System.Enum.GetValues(typeof(CardZone)).Length;
for (int zone = 0; zone < totalZones; zone++)
{
for (int i = 0; i < slotsPerZone; i++)
{
// Create a slot at this position
GameObject slotObj = Instantiate(cardSlotPrefab, albumGrid.transform);
// Calculate the collection index for this slot
int collectionIndex = zone * 100 + i; // Zone*100 + position to ensure unique indices
// Store the slot reference
_albumSlots[collectionIndex] = slotObj.transform;
// Set the slot label (optional)
Text slotLabel = slotObj.GetComponentInChildren();
if (slotLabel != null)
{
CardZone zoneEnum = (CardZone)zone;
slotLabel.text = $"{zoneEnum} #{i+1}";
}
}
}
}
///
/// Creates UI elements for the player's collected cards
///
private void CreateCardStack(List cards)
{
if (cardStackContainer == null || cardPrefab == null) return;
// Stack offset for visual effect
Vector3 stackOffset = new Vector3(5f, -5f, 0f);
Vector3 basePosition = Vector3.zero;
// Sort cards by collection index
cards.Sort((a, b) => a.CollectionIndex.CompareTo(b.CollectionIndex));
// Create card UI elements
for (int i = 0; i < cards.Count; i++)
{
GameObject cardObj = Instantiate(cardPrefab, cardStackContainer);
CardUIElement cardUI = cardObj.GetComponent();
if (cardUI != null)
{
// Position in stack
cardObj.GetComponent().anchoredPosition = basePosition + (stackOffset * i);
// Set up card data
cardUI.SetupCard(cards[i]);
// Add drag handlers
SetupCardDragHandlers(cardUI);
// Add to tracked cards
_displayedCards.Add(cardUI);
// Check if this card should be placed in a slot already
int collectionIndex = cards[i].CollectionIndex;
if (_albumSlots.TryGetValue(collectionIndex, out Transform slot))
{
// Card has a designated slot, place it there
PlaceCardInSlot(cardUI, slot);
}
}
}
}
///
/// Sets up drag and drop handlers for a card
///
private void SetupCardDragHandlers(CardUIElement cardUI)
{
// // Get drag handler component (you might need to implement this)
// DragHandler dragHandler = cardUI.GetComponent();
// if (dragHandler == null)
// {
// // This is a stub for the drag handler
// // In a real implementation, you'd have a proper drag handler component
// // For now, we'll just add click listeners
//
// // Add click listener
// Button cardButton = cardUI.GetComponent