using System.Collections.Generic;
using AppleHills.Data.CardSystem;
using Core;
using Data.CardSystem;
using Pixelplacement;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace AppleHills.UI.CardSystem
{
///
/// UI page for viewing the player's card collection in an album.
///
public class AlbumViewPage : UIPage
{
[Header("Album UI Elements")]
[SerializeField] private GridLayoutGroup albumGrid;
[SerializeField] private GameObject cardPrefab;
[Header("Filter UI")]
[SerializeField] private TMP_Dropdown zoneFilterDropdown;
[SerializeField] private TMP_Dropdown rarityFilterDropdown;
[SerializeField] private Button resetFiltersButton;
[SerializeField] private CanvasGroup canvasGroup;
[Header("Navigation")]
[SerializeField] private Button backButton;
// Runtime references
private CardSystemManager _cardManager;
private List _displayedCards = new List();
private void Awake()
{
_cardManager = CardSystemManager.Instance;
// Make sure we have a CanvasGroup for transitions
if (canvasGroup == null)
canvasGroup = GetComponent();
if (canvasGroup == null)
canvasGroup = gameObject.AddComponent();
// Set up back button
if (backButton != null)
{
backButton.onClick.AddListener(OnBackButtonClicked);
}
}
///
/// 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 the player's collection
///
private void InitializeAlbum()
{
// Clear any previous setup
ClearAlbum();
// Setup filter UI
InitializeFilters();
// Get all collected cards
List collectedCards = _cardManager.GetAllCollectedCards();
// If there are cards to display, create UI elements for them
if (collectedCards.Count > 0)
{
// Create card UI elements
CreateCardStack(collectedCards);
}
}
///
/// Creates UI elements for the player's collected cards
///
private void CreateCardStack(List cards)
{
if (albumGrid == 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, albumGrid.transform);
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);
}
}
}
///
/// Sets up drag and drop handlers for a card
///
private void SetupCardDragHandlers(CardUIElement cardUI)
{
// Add click listener to handle card clicks
Button cardButton = cardUI.GetComponent