using AppleHills.Data.CardSystem; using Core; using Core.Lifecycle; using Data.CardSystem; using Pixelplacement; using UI.Core; using UnityEngine; using UnityEngine.UI; namespace UI.CardSystem { /// /// Main UI controller for the card album system. /// Manages the backpack icon and navigation between card system pages. /// public class CardAlbumUI : ManagedBehaviour { [Header("UI References")] [SerializeField] private GameObject backpackIcon; [SerializeField] private UIPage mainMenuPage; [SerializeField] private UIPage albumViewPage; [SerializeField] private UIPage boosterOpeningPage; [Header("UI Elements")] [SerializeField] private Button backpackButton; [SerializeField] private BoosterNotificationDot boosterNotificationDot; [Header("Notification Settings")] [SerializeField] private AudioSource notificationSound; // Public property to access the backpack icon for animations public GameObject BackpackIcon => backpackIcon; private UIPageController PageController => UIPageController.Instance; private CardSystemManager _cardManager; private bool _hasUnseenCards; public override int ManagedAwakePriority => 65; // UI card systems protected override void OnManagedAwake() { base.OnManagedAwake(); // Get card manager - safe to access .Instance here _cardManager = CardSystemManager.Instance; // Set up backpack button if (backpackButton != null) { backpackButton.onClick.AddListener(OnBackpackButtonClicked); } // Hide notification dot initially if (boosterNotificationDot != null) boosterNotificationDot.gameObject.SetActive(false); // Initially show only the backpack icon ShowOnlyBackpackIcon(); // Initialize pages and hide them InitializePages(); // Subscribe to card manager events if (_cardManager != null) { _cardManager.OnBoosterCountChanged += UpdateBoosterCount; _cardManager.OnBoosterOpened += HandleBoosterOpened; _cardManager.OnCardCollected += HandleCardCollected; _cardManager.OnCardRarityUpgraded += HandleCardRarityUpgraded; // Initialize UI with current values UpdateBoosterCount(_cardManager.GetBoosterPackCount()); } // React to global UI hide/show events (top-page only) by toggling this GameObject if (UIPageController.Instance != null) { UIPageController.Instance.OnAllUIHidden += HandleAllUIHidden; UIPageController.Instance.OnAllUIShown += HandleAllUIShown; } } private void OnDestroy() { // Unsubscribe from events if (_cardManager != null) { _cardManager.OnBoosterCountChanged -= UpdateBoosterCount; _cardManager.OnBoosterOpened -= HandleBoosterOpened; _cardManager.OnCardCollected -= HandleCardCollected; _cardManager.OnCardRarityUpgraded -= HandleCardRarityUpgraded; } // Clean up button listeners if (backpackButton != null) { backpackButton.onClick.RemoveListener(OnBackpackButtonClicked); } // Unsubscribe from page controller events if (PageController != null) { PageController.OnPageChanged -= OnPageChanged; } // Unsubscribe from global UI hide/show events if (UIPageController.Instance != null) { UIPageController.Instance.OnAllUIHidden -= HandleAllUIHidden; UIPageController.Instance.OnAllUIShown -= HandleAllUIShown; } } /// /// Initializes all UI pages and sets them up with proper callbacks /// private void InitializePages() { // Ensure all pages are inactive at start if (mainMenuPage != null) { mainMenuPage.gameObject.SetActive(false); } if (albumViewPage != null) { albumViewPage.gameObject.SetActive(false); } if (boosterOpeningPage != null) { boosterOpeningPage.gameObject.SetActive(false); } // Set up page changed callback if (PageController != null) { PageController.OnPageChanged += OnPageChanged; } } /// /// Handles click on the backpack icon /// private void OnBackpackButtonClicked() { // Play button sound if available if (notificationSound != null) notificationSound.Play(); PageController.PushPage(mainMenuPage); // Clear notification for unseen cards when opening menu if (_hasUnseenCards) { _hasUnseenCards = false; } // Hide the backpack button when entering menu if (backpackButton != null) { backpackButton.gameObject.SetActive(false); } GameManager.Instance.RequestPause(this); } /// /// Handles changes to the current page /// private void OnPageChanged(UIPage newPage) { // Hide/show backpack icon based on current page if (newPage == null) { ShowOnlyBackpackIcon(); GameManager.Instance.ReleasePause(this); } else { // Check if any page in the stack has "Card" or "Album" in the name bool hasCardOrAlbumPage = false; if (PageController != null && PageController.PageStack != null) { foreach (var page in PageController.PageStack) { if (page != null && page.PageName != null) { if (page.PageName.Contains("Card") || page.PageName.Contains("Album")) { hasCardOrAlbumPage = true; break; } } } } // Hide backpack button if there's a card/album page in the stack if (backpackButton != null) { backpackButton.gameObject.SetActive(!hasCardOrAlbumPage); } } // Update menu if it's the main menu page if (newPage == mainMenuPage && mainMenuPage is CardMenuPage menuPage) { // Force UI refresh when returning to main menu menuPage.RefreshUI(); } } /// /// Shows only the backpack icon, hiding all other UI elements /// private void ShowOnlyBackpackIcon() { if (backpackButton != null) { backpackButton.gameObject.SetActive(true); // Update notification visibility based on booster count bool hasBooters = _cardManager != null && _cardManager.GetBoosterPackCount() > 0; // Show notification dot if there are boosters or unseen cards if (boosterNotificationDot != null) { boosterNotificationDot.gameObject.SetActive(hasBooters || _hasUnseenCards); } } } /// /// Opens the album view page /// public void OpenAlbumView() { PageController.PushPage(albumViewPage); } /// /// Opens the booster opening page if boosters are available /// public void OpenBoosterPack() { if (_cardManager != null && _cardManager.GetBoosterPackCount() > 0) { PageController.PushPage(boosterOpeningPage); } else { Logging.Debug("[CardAlbumUI] No booster packs available"); } } /// /// Updates the booster count display /// private void UpdateBoosterCount(int count) { if (boosterNotificationDot != null) { boosterNotificationDot.SetCount(count); // Animate the notification dot for feedback boosterNotificationDot.transform.localScale = Vector3.one * 1.2f; Tween.LocalScale(boosterNotificationDot.transform, Vector3.one, 0.3f, 0f, obeyTimescale: false); // Update visibility based on count UpdateBoosterVisibility(); } } /// /// Updates the visibility of the booster notification dot based on current state /// private void UpdateBoosterVisibility() { if (boosterNotificationDot != null) { // Show dot if there are boosters or unseen cards bool hasBooters = _cardManager != null && _cardManager.GetBoosterPackCount() > 0; boosterNotificationDot.gameObject.SetActive(hasBooters || _hasUnseenCards); } } /// /// Handles event when a booster pack is opened /// private void HandleBoosterOpened(System.Collections.Generic.List cards) { Logging.Debug($"[CardAlbumUI] Booster opened with {cards.Count} cards"); // The booster opening page handles the UI for this event } /// /// Handles event when a new card is collected /// private void HandleCardCollected(CardData card) { // If we're not in the album view or booster opening view, // show a notification dot on the backpack if (PageController.CurrentPage != albumViewPage && PageController.CurrentPage != boosterOpeningPage) { _hasUnseenCards = true; UpdateBoosterVisibility(); } Logging.Debug($"[CardAlbumUI] New card collected: {card.Name}"); } /// /// Handles event when a card is upgraded to a higher rarity /// private void HandleCardRarityUpgraded(CardData card) { // Just log the upgrade event without showing a notification Logging.Debug($"[CardAlbumUI] Card upgraded: {card.Name} to {card.Rarity}"); } // Handlers for UI controller hide/show events — toggle this GameObject active state private void HandleAllUIHidden() { // Ensure UI returns to backpack-only state before deactivating so we don't leave the game paused ShowOnlyBackpackIcon(); backpackButton.gameObject.SetActive(false); } private void HandleAllUIShown() { backpackButton.gameObject.SetActive(true); } } }