using System.Collections.Generic; using System.Linq; using AppleHills.Data.CardSystem; using Core; using Data.CardSystem; using Pixelplacement; using UI.Core; using UI.DragAndDrop.Core; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; namespace UI.CardSystem { /// /// UI page for viewing the player's card collection in an album. /// Manages booster pack button visibility and opening flow. /// public class AlbumViewPage : UIPage { [Header("UI References")] [SerializeField] private CanvasGroup canvasGroup; [SerializeField] private Button exitButton; [SerializeField] private BookCurlPro.BookPro book; [Header("Zone Navigation")] [SerializeField] private Transform tabContainer; // Container holding all BookTabButton children private BookTabButton[] zoneTabs; // Discovered zone tab buttons [Header("Album Card Reveal")] [SerializeField] private SlotContainer bottomRightSlots; [SerializeField] private GameObject albumCardPlacementPrefab; // The wrapper prefab with flip/drag (AlbumPlacementCard) [Header("Card Enlarge System")] [SerializeField] private GameObject cardEnlargedBackdrop; // Backdrop to block interactions [SerializeField] private Transform cardEnlargedContainer; // Container for enlarged cards (sits above backdrop) [Header("Booster Pack UI")] [SerializeField] private GameObject[] boosterPackButtons; [SerializeField] private BoosterOpeningPage boosterOpeningPage; private Input.InputMode _previousInputMode; private List _activeCards = new List(); private const int MAX_VISIBLE_CARDS = 3; protected override void OnManagedAwake() { base.OnManagedAwake(); // Discover zone tabs from container DiscoverZoneTabs(); // Make sure we have a CanvasGroup for transitions if (canvasGroup == null) canvasGroup = GetComponent(); if (canvasGroup == null) canvasGroup = gameObject.AddComponent(); // Hide backdrop initially if (cardEnlargedBackdrop != null) { cardEnlargedBackdrop.SetActive(false); } // Set up exit button if (exitButton != null) { exitButton.onClick.AddListener(OnExitButtonClicked); } // Set up booster pack button listeners SetupBoosterButtonListeners(); // Subscribe to book page flip events if (book != null) { book.OnFlip.AddListener(OnPageFlipped); Logging.Debug("[AlbumViewPage] Subscribed to book.OnFlip event"); } else { Logging.Warning("[AlbumViewPage] Book reference is null, cannot subscribe to OnFlip event!"); } // Subscribe to CardSystemManager events (managers are guaranteed to be initialized) if (CardSystemManager.Instance != null) { CardSystemManager.Instance.OnBoosterCountChanged += OnBoosterCountChanged; // NOTE: OnPendingCardAdded is subscribed in TransitionIn, not here // This prevents spawning cards when page is not active // Update initial button visibility int initialCount = CardSystemManager.Instance.GetBoosterPackCount(); UpdateBoosterButtons(initialCount); } // UI pages should start disabled gameObject.SetActive(false); } /// /// Discover all BookTabButton components from the tab container /// private void DiscoverZoneTabs() { if (tabContainer == null) { Debug.LogError("[AlbumViewPage] Tab container is not assigned! Cannot discover zone tabs."); zoneTabs = new BookTabButton[0]; return; } // Get all BookTabButton components from children zoneTabs = tabContainer.GetComponentsInChildren(includeInactive: false); if (zoneTabs == null || zoneTabs.Length == 0) { Logging.Warning($"[AlbumViewPage] No BookTabButton components found in tab container '{tabContainer.name}'!"); zoneTabs = new BookTabButton[0]; } else { Logging.Debug($"[AlbumViewPage] Discovered {zoneTabs.Length} zone tabs from container '{tabContainer.name}'"); foreach (var tab in zoneTabs) { Logging.Debug($" - Tab: {tab.name}, Zone: {tab.Zone}, TargetPage: {tab.TargetPage}"); } } } private void SetupBoosterButtonListeners() { if (boosterPackButtons == null) return; for (int i = 0; i < boosterPackButtons.Length; i++) { if (boosterPackButtons[i] == null) continue; // Unsubscribe from book events if (book != null) { book.OnFlip.RemoveListener(OnPageFlipped); } Button button = boosterPackButtons[i].GetComponent