using Core.SaveLoad; using UnityEngine; using UnityEngine.EventSystems; using AppleHills.Core.Settings; using Core; using AppleHills.Data.CardSystem; namespace UI.CardSystem.StateMachine.States { /// /// Enlarged state for REPEAT cards - shows progress bar toward next rarity upgrade. /// Uses ProgressBarController to animate progress filling. /// Auto-upgrades card when threshold is reached. /// public class CardEnlargedRepeatState : AppleState, IPointerClickHandler { [Header("State-Owned Visuals")] [SerializeField] private GameObject progressBarContainer; [SerializeField] private ProgressBarController progressBar; private CardContext _context; private ICardSystemSettings _settings; private Vector3 _originalScale; private bool _waitingForTap = false; private void Awake() { _context = GetComponentInParent(); _settings = GameManager.GetSettingsObject(); } public override void OnEnterState() { // Store original scale _originalScale = _context.RootTransform.localScale; _waitingForTap = false; // Show progress bar container if (progressBarContainer != null) { progressBarContainer.SetActive(true); } // Enlarge the card if (_context.Animator != null) { _context.Animator.PlayEnlarge(_settings.NewCardEnlargedScale); } // Show progress with blink animation if (progressBar != null) { int currentCount = _context.RepeatCardCount + 1; // +1 because we just got this card int maxCount = _settings.CardsToUpgrade; progressBar.ShowProgress(currentCount, maxCount, OnProgressComplete); } else { Logging.Warning("[CardEnlargedRepeatState] ProgressBar component not assigned!"); OnProgressComplete(); } } private void OnProgressComplete() { // Check if this card triggers an upgrade if (ShouldTriggerUpgrade()) { Logging.Debug($"[CardEnlargedRepeatState] Card will trigger upgrade! ({_context.RepeatCardCount + 1}/{_settings.CardsToUpgrade})"); TriggerUpgrade(); } else { // No upgrade - just wait for tap to dismiss Logging.Debug($"[CardEnlargedRepeatState] Progress shown, waiting for tap to dismiss"); _waitingForTap = true; } } private bool ShouldTriggerUpgrade() { int currentCount = _context.RepeatCardCount + 1; // +1 for the card we just got CardRarity currentRarity = _context.CardData.Rarity; // Can't upgrade if already Legendary if (currentRarity == CardRarity.Legendary) return false; // Check if we've hit the threshold return currentCount >= _settings.CardsToUpgrade; } private void TriggerUpgrade() { _context.FireUpgradeTriggered(); CardData cardData = _context.CardData; CardRarity oldRarity = cardData.Rarity; CardRarity newRarity = oldRarity + 1; Logging.Debug($"[CardEnlargedRepeatState] Upgrading card from {oldRarity} to {newRarity}"); // Get the existing card at lower rarity from inventory CardData existingLowerRarity = Data.CardSystem.CardSystemManager.Instance.GetCardInventory().GetCard(cardData.Name, oldRarity); if (existingLowerRarity != null) { // Reset lower rarity count to 0 existingLowerRarity.CopiesOwned = 0; } // Create upgraded card data CardData upgradedCard = new CardData(cardData); upgradedCard.Rarity = newRarity; upgradedCard.CopiesOwned = 1; // Check if this card is new at the higher rarity bool isNewAtHigherRarity = Data.CardSystem.CardSystemManager.Instance.IsCardNew(upgradedCard, out CardData existingHigherRarity); // Add to inventory Data.CardSystem.CardSystemManager.Instance.GetCardInventory().AddCard(upgradedCard); // Update our card data to show upgraded rarity cardData.Rarity = newRarity; // Update the CardDisplay to show new rarity if (_context.CardDisplay != null) { _context.CardDisplay.SetupCard(cardData); } // Determine next transition if (isNewAtHigherRarity || newRarity == CardRarity.Legendary) { // Show as NEW at higher rarity Logging.Debug($"[CardEnlargedRepeatState] Card is NEW at {newRarity}, transitioning to EnlargedNewState"); TransitionToNewCardView(); } else { // Already have copies at higher rarity - show progress there too int ownedAtHigherRarity = existingHigherRarity.CopiesOwned; Logging.Debug($"[CardEnlargedRepeatState] Card is REPEAT at {newRarity} ({ownedAtHigherRarity}/{_settings.CardsToUpgrade}), showing progress"); // Update context for higher rarity _context.IsNewCard = false; _context.RepeatCardCount = ownedAtHigherRarity; // Re-enter this state with updated data (will show progress for higher rarity) // First hide current progress, then show new progress if (progressBar != null) { progressBar.ShowProgress(ownedAtHigherRarity + 1, _settings.CardsToUpgrade, () => { // After showing progress at higher rarity, transition to NEW TransitionToNewCardView(); }); } else { TransitionToNewCardView(); } } } private void TransitionToNewCardView() { // Update context to show as new card _context.IsNewCard = true; // Hide progress bar before transitioning if (progressBarContainer != null) { progressBarContainer.SetActive(false); } // Transition to EnlargedNewState (card is already enlarged, will show NEW badge) _context.StateMachine.ChangeState("EnlargedNewState"); } public void OnPointerClick(PointerEventData eventData) { // Only respond to clicks if waiting for tap if (!_waitingForTap) return; // Fire dismissed event _context.FireCardDismissed(); // Tap to dismiss - shrink back and transition to revealed state if (_context.Animator != null) { _context.Animator.PlayShrink(_originalScale, onComplete: () => { _context.StateMachine.ChangeState("RevealedState"); }); } else { _context.StateMachine.ChangeState("RevealedState"); } } private void OnDisable() { // Hide progress bar when leaving state if (progressBarContainer != null) { progressBarContainer.SetActive(false); } } } }