using System; using AppleHills.Data.CardSystem; using Core; using Pixelplacement; using UnityEngine; using UnityEngine.EventSystems; namespace UI.CardSystem { /// /// Album card component that wraps CardDisplay. /// Handles tap-to-enlarge and tap-to-shrink interactions for cards placed in album slots. /// /// TODO: Consider refactoring to state machine pattern (PendingReveal, PlacedInSlot, Enlarged) /// This would eliminate the need for separate AlbumPlacementCard wrapper and simplify the hierarchy. /// See design discussion with state transitions for cleaner architecture. /// public class AlbumCard : MonoBehaviour, IPointerClickHandler { [Header("References")] [SerializeField] private CardDisplay cardDisplay; [Header("Enlarge Settings")] [SerializeField] private float enlargedScale = 2.5f; [SerializeField] private float scaleDuration = 0.3f; // Events for AlbumViewPage to manage backdrop and reparenting public event Action OnEnlargeRequested; public event Action OnShrinkRequested; private AlbumCardSlot _parentSlot; private CardData _cardData; private bool _isEnlarged; private Vector3 _originalScale; private Transform _originalParent; private Vector3 _originalLocalPosition; private Quaternion _originalLocalRotation; private void Awake() { // Auto-find CardDisplay if not assigned if (cardDisplay == null) { cardDisplay = GetComponentInChildren(); } // Store original scale _originalScale = transform.localScale; } /// /// Setup card with data /// public void SetupCard(CardData data) { _cardData = data; if (cardDisplay != null) { cardDisplay.SetupCard(data); } } /// /// Set the parent slot this card belongs to /// public void SetParentSlot(AlbumCardSlot slot) { _parentSlot = slot; } /// /// Get the card data /// public CardData GetCardData() { return _cardData; } /// /// Handle tap on card - request enlarge/shrink from parent page /// Only process clicks when card is placed in a slot (not during reveal flow) /// public void OnPointerClick(PointerEventData eventData) { Logging.Debug($"[CLICK-TRACE-ALBUMCARD] OnPointerClick on {name}, _parentSlot={((_parentSlot != null) ? _parentSlot.name : "NULL")}, _isEnlarged={_isEnlarged}, position={eventData.position}"); // During reveal flow (before placed in slot), forward clicks to parent FlippableCard if (_parentSlot == null) { Logging.Debug($"[CLICK-TRACE-ALBUMCARD] {name} - No parent slot, forwarding click to parent FlippableCard"); // Find parent FlippableCard and forward the click FlippableCard parentFlippable = GetComponentInParent(); if (parentFlippable != null) { Logging.Debug($"[CLICK-TRACE-ALBUMCARD] {name} - Found parent FlippableCard, calling OnPointerClick"); parentFlippable.OnPointerClick(eventData); } else { Logging.Warning($"[CLICK-TRACE-ALBUMCARD] {name} - No parent FlippableCard found!"); } return; } Logging.Debug($"[CLICK-TRACE-ALBUMCARD] {name} - Has parent slot, processing click"); if (_isEnlarged) { Logging.Debug($"[CLICK-TRACE-ALBUMCARD] {name} - Is enlarged, requesting shrink"); OnShrinkRequested?.Invoke(this); } else { Logging.Debug($"[CLICK-TRACE-ALBUMCARD] {name} - Is normal size, requesting enlarge"); OnEnlargeRequested?.Invoke(this); } } /// /// Enlarge card (called by AlbumViewPage after reparenting) /// public void EnlargeCard() { if (_isEnlarged) return; _isEnlarged = true; // Store original transform info for restoration _originalParent = transform.parent; _originalLocalPosition = transform.localPosition; _originalLocalRotation = transform.localRotation; // Scale up with snappy tween Tween.LocalScale(transform, _originalScale * enlargedScale, scaleDuration, 0f, Tween.EaseOutBack); } /// /// Shrink card back to original size (called by AlbumViewPage before reparenting back) /// /// Optional callback to invoke when shrink animation completes public void ShrinkCard(System.Action onComplete = null) { if (!_isEnlarged) return; _isEnlarged = false; // Scale back down with snappy tween, invoke callback when done Tween.LocalScale(transform, _originalScale, scaleDuration, 0f, Tween.EaseInBack, completeCallback: () => onComplete?.Invoke()); } /// /// Get original parent for restoration /// public Transform GetOriginalParent() { return _originalParent; } /// /// Get original local position for restoration /// public Vector3 GetOriginalLocalPosition() { return _originalLocalPosition; } /// /// Get original local rotation for restoration /// public Quaternion GetOriginalLocalRotation() { return _originalLocalRotation; } /// /// Check if card is currently enlarged /// public bool IsEnlarged => _isEnlarged; /// /// Force reset enlarged state (for cleanup scenarios like page closing) /// public void ForceResetEnlargedState() { _isEnlarged = false; transform.localScale = _originalScale; } } }