using System; using System.Collections; using AppleHills.Data.CardSystem; using Core; using Data.CardSystem; using Pixelplacement; using UnityEngine; using UnityEngine.UI; namespace AppleHills.UI.CardSystem { /// /// Main UI controller for the card album system. /// Manages the backpack icon and navigation between card system pages. /// public class CardAlbumUI : MonoBehaviour { [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 Text boosterCountText; [SerializeField] private GameObject notificationDot; [SerializeField] private GameObject backpackAnimationTarget; [SerializeField] private GameObject newCardNotification; [Header("Notification Settings")] [SerializeField] private float notificationDuration = 3f; [SerializeField] private AudioSource notificationSound; private UIPageController _pageController; private CardSystemManager _cardManager; private bool _isInitialized = false; private bool _hasUnseenCards = false; private Coroutine _notificationCoroutine; private void Awake() { // Get or create a UI page controller _pageController = FindAnyObjectByType(); if (_pageController == null) { GameObject controllerObj = new GameObject("UIPageController"); controllerObj.transform.SetParent(transform); _pageController = controllerObj.AddComponent(); } // Initialize pages and hide them InitializePages(); // Set up backpack button if (backpackButton != null) { backpackButton.onClick.AddListener(OnBackpackButtonClicked); } // Initially show only the backpack icon ShowOnlyBackpackIcon(); // Hide notifications initially if (notificationDot != null) notificationDot.SetActive(false); if (newCardNotification != null) newCardNotification.SetActive(false); } private void Start() { // Get card manager _cardManager = CardSystemManager.Instance; // Subscribe to events if (_cardManager != null) { _cardManager.OnBoosterCountChanged += UpdateBoosterCount; _cardManager.OnBoosterOpened += HandleBoosterOpened; _cardManager.OnCardCollected += HandleCardCollected; _cardManager.OnCardRarityUpgraded += HandleCardRarityUpgraded; // Initialize UI with current values UpdateBoosterCount(_cardManager.GetBoosterPackCount()); } _isInitialized = true; } 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); } // Stop any coroutines if (_notificationCoroutine != null) StopCoroutine(_notificationCoroutine); } /// /// 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(); // If no pages are open, push the main menu if (_pageController.CurrentPage == null) { _pageController.PushPage(mainMenuPage); // Clear notification if (notificationDot != null) notificationDot.SetActive(false); _hasUnseenCards = false; } else if (_pageController.CurrentPage == mainMenuPage) { // If main menu is open, pop it _pageController.PopPage(); } } /// /// Handles changes to the current page /// private void OnPageChanged(UIPage newPage) { // Hide/show backpack icon based on current page if (newPage == null) { ShowOnlyBackpackIcon(); } else { backpackIcon.SetActive(false); } } /// /// Shows only the backpack icon, hiding all other UI elements /// private void ShowOnlyBackpackIcon() { backpackIcon.SetActive(true); } /// /// 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"); ShowNoBoostersNotification(); } } /// /// Shows a notification that no booster packs are available /// private void ShowNoBoostersNotification() { if (newCardNotification != null) { // Set notification text Text notificationText = newCardNotification.GetComponentInChildren(); if (notificationText != null) notificationText.text = "No booster packs available!"; // Show and animate the notification ShowTemporaryNotification(newCardNotification); } } /// /// Shows a notification temporarily and then hides it /// private void ShowTemporaryNotification(GameObject notification) { if (notification == null) return; // Stop any existing notification coroutine if (_notificationCoroutine != null) StopCoroutine(_notificationCoroutine); // Start new notification coroutine _notificationCoroutine = StartCoroutine(ShowNotificationCoroutine(notification)); } private IEnumerator ShowNotificationCoroutine(GameObject notification) { // Show notification notification.SetActive(true); notification.transform.localScale = Vector3.zero; // Animate in Tween.LocalScale(notification.transform, Vector3.one, 0.3f, 0f, Tween.EaseOutBack); // Wait for duration yield return new WaitForSeconds(notificationDuration); // Animate out Tween.LocalScale(notification.transform, Vector3.zero, 0.3f, 0f, Tween.EaseInBack, Tween.LoopType.None, null, () => { notification.SetActive(false); }); } /// /// Updates the booster count display /// private void UpdateBoosterCount(int count) { if (boosterCountText != null) { boosterCountText.text = count.ToString(); // Animate the text for feedback boosterCountText.transform.localScale = Vector3.one * 1.2f; Tween.LocalScale(boosterCountText.transform, Vector3.one, 0.3f, 0f); } } /// /// 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; if (notificationDot != null) notificationDot.SetActive(true); } Logging.Debug($"[CardAlbumUI] New card collected: {card.Name}"); } /// /// Handles event when a card is upgraded to a higher rarity /// private void HandleCardRarityUpgraded(CardData card) { // Show a special notification for rarity upgrade if (newCardNotification != null) { // Set notification text Text notificationText = newCardNotification.GetComponentInChildren(); if (notificationText != null) notificationText.text = $"{card.Name} upgraded to {card.Rarity}!"; // Show and animate the notification ShowTemporaryNotification(newCardNotification); } Logging.Debug($"[CardAlbumUI] Card upgraded: {card.Name} to {card.Rarity}"); } } }