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 DisplayCards(collectedCards); } } /// /// Creates UI elements for the player's collected cards /// private void DisplayCards(List cards) { if (albumGrid == null || cardPrefab == null) return; // Sort cards by collection index cards.Sort((a, b) => a.CollectionIndex.CompareTo(b.CollectionIndex)); // Create card UI elements foreach (var cardData in cards) { GameObject cardObj = Instantiate(cardPrefab, albumGrid.transform); // Configure the card UI with the card data CardUIElement cardUI = cardObj.GetComponent(); if (cardUI != null) { cardUI.SetupCard(cardData); _displayedCards.Add(cardUI); } } } /// /// Clears all card UI elements from the album /// private void ClearAlbum() { foreach (var card in _displayedCards) { if (card != null && card.gameObject != null) Destroy(card.gameObject); } _displayedCards.Clear(); } /// /// Initializes filter dropdowns /// private void InitializeFilters() { // Setup zone filter dropdown if (zoneFilterDropdown != null) { zoneFilterDropdown.ClearOptions(); // Add "All Zones" option List zoneOptions = new List() { "All Zones" }; // Add options for each zone foreach (CardZone zone in System.Enum.GetValues(typeof(CardZone))) { zoneOptions.Add(zone.ToString()); } zoneFilterDropdown.AddOptions(zoneOptions); zoneFilterDropdown.onValueChanged.AddListener(OnFilterChanged); } // Setup rarity filter dropdown if (rarityFilterDropdown != null) { rarityFilterDropdown.ClearOptions(); // Add "All Rarities" option List rarityOptions = new List() { "All Rarities" }; // Add options for each rarity foreach (CardRarity rarity in System.Enum.GetValues(typeof(CardRarity))) { rarityOptions.Add(rarity.ToString()); } rarityFilterDropdown.AddOptions(rarityOptions); rarityFilterDropdown.onValueChanged.AddListener(OnFilterChanged); } // Setup reset filters button if (resetFiltersButton != null) { resetFiltersButton.onClick.AddListener(OnResetFiltersClicked); } } /// /// Handles changes to the filter dropdowns /// private void OnFilterChanged(int value) { ApplyFilters(); } /// /// Resets all filters to their default values /// private void OnResetFiltersClicked() { if (zoneFilterDropdown != null) zoneFilterDropdown.value = 0; if (rarityFilterDropdown != null) rarityFilterDropdown.value = 0; ApplyFilters(); } /// /// Applies the current filter selections to the displayed cards /// private void ApplyFilters() { // Clear current cards ClearAlbum(); // Get selected filters CardZone? selectedZone = null; CardRarity? selectedRarity = null; // Get zone filter value if (zoneFilterDropdown != null && zoneFilterDropdown.value > 0) { selectedZone = (CardZone)(zoneFilterDropdown.value - 1); } // Get rarity filter value if (rarityFilterDropdown != null && rarityFilterDropdown.value > 0) { selectedRarity = (CardRarity)(rarityFilterDropdown.value - 1); } // Get filtered cards List filteredCards = GetFilteredCards(selectedZone, selectedRarity); // Create card UI elements for the filtered cards DisplayCards(filteredCards); Logging.Debug($"[AlbumViewPage] Applied filters. Showing {filteredCards.Count} cards."); } /// /// Gets cards filtered by zone and/or rarity /// private List GetFilteredCards(CardZone? zone, CardRarity? rarity) { List result; // Get all collected cards if (zone == null && rarity == null) { // No filters, return all cards result = _cardManager.GetAllCollectedCards(); } else if (zone != null && rarity != null) { // Both filters, get cards by zone and rarity result = _cardManager.GetCardsByZoneAndRarity(zone.Value, rarity.Value); } else if (zone != null) { // Only zone filter result = _cardManager.GetCardsByZone(zone.Value); } else { // Only rarity filter result = _cardManager.GetCardsByRarity(rarity.Value); } return result; } /// /// Handles click on the back button /// private void OnBackButtonClicked() { // Use the UIPageController to go back to the previous page UIPageController pageController = UIPageController.Instance; if (pageController != null) { pageController.PopPage(); } } protected override void DoTransitionIn(System.Action onComplete) { // Simple fade in animation if (canvasGroup != null) { canvasGroup.alpha = 0f; Tween.Value(0f, 1f, (value) => canvasGroup.alpha = value, transitionDuration, 0f, Tween.EaseInOut, Tween.LoopType.None, null, onComplete); } else { // Fallback if no CanvasGroup onComplete?.Invoke(); } } protected override void DoTransitionOut(System.Action onComplete) { // Simple fade out animation if (canvasGroup != null) { Tween.Value(canvasGroup.alpha, 0f, (value) => canvasGroup.alpha = value, transitionDuration, 0f, Tween.EaseInOut, Tween.LoopType.None, null, onComplete); } else { // Fallback if no CanvasGroup onComplete?.Invoke(); } } private void OnDestroy() { // Clean up button listeners if (backButton != null) { backButton.onClick.RemoveListener(OnBackButtonClicked); } if (zoneFilterDropdown != null) { zoneFilterDropdown.onValueChanged.RemoveListener(OnFilterChanged); } if (rarityFilterDropdown != null) { rarityFilterDropdown.onValueChanged.RemoveListener(OnFilterChanged); } if (resetFiltersButton != null) { resetFiltersButton.onClick.RemoveListener(OnResetFiltersClicked); } } private void OnEnable() { if (_cardManager == null) { _cardManager = CardSystemManager.Instance; } } } }