using System; using System.Collections.Generic; using AppleHills.Data.CardSystem; using Core; using UnityEngine; using TMPro; using UnityEngine.UI; #if UNITY_EDITOR using UnityEditor; #endif namespace AppleHills.UI.CardSystem { /// /// Handles displaying and interacting with a single card in the UI. /// public class CardUIElement : MonoBehaviour { [Header("UI Elements")] [SerializeField] private TextMeshProUGUI cardNameText; [SerializeField] private Image cardImage; [SerializeField] private Image frameImage; [SerializeField] private Image backgroundImage; [SerializeField] private Image backgroundShape; [Header("Card Data")] [SerializeField] private CardDefinition cardDefinition; [SerializeField] private CardData cardData; [Header("Visual Settings")] [SerializeField] private CardVisualConfig visualConfig; // Events public event Action OnCardClicked; /// /// Sets up the card UI with the given card data /// public void SetupCard(CardData cardData) { Logging.Debug($"[CardUIElement] Setting up card with data: {cardData}"); this.cardData = cardData; if (cardData == null) { Debug.LogError("[CardUIElement] Attempted to setup card with null data"); return; } UpdateCardVisuals(); } /// /// Updates the card visuals based on the current card data /// private void UpdateCardVisuals() { if (cardData == null) return; // Set basic text information if (cardNameText != null) cardNameText.text = cardData.Name; // Set the card image if (cardImage != null) { if (cardData.CardImage != null) { cardImage.sprite = cardData.CardImage; cardImage.enabled = true; } else { cardImage.sprite = null; cardImage.enabled = false; // Don't log warnings for missing images - this is expected for new cards // and causes console spam in the editor } } // Update frame color based on rarity UpdateFrameByRarity(cardData.Rarity); // Update background color and shape based on zone UpdateBackgroundByZone(cardData.Zone); } /// /// Updates the frame color based on card rarity /// private void UpdateFrameByRarity(CardRarity rarity) { if (frameImage == null) return; // Get color from config if available, otherwise use default colors Color frameColor = visualConfig != null ? visualConfig.GetRarityColor(rarity) : GetDefaultRarityColor(rarity); frameImage.color = frameColor; } /// /// Updates the background color and shape based on card zone /// private void UpdateBackgroundByZone(CardZone zone) { // Update background color if (backgroundImage != null) { Color bgColor = visualConfig != null ? visualConfig.GetZoneColor(zone) : GetDefaultZoneColor(zone); backgroundImage.color = bgColor; } // Update background shape if (backgroundShape != null && visualConfig != null) { Sprite shapeSprite = visualConfig.GetZoneShape(zone); if (shapeSprite != null) { backgroundShape.sprite = shapeSprite; backgroundShape.gameObject.SetActive(true); } else { backgroundShape.gameObject.SetActive(false); } } } /// /// Returns default color for rarity if no config is available /// private Color GetDefaultRarityColor(CardRarity rarity) { switch (rarity) { case CardRarity.Common: return Color.gray; case CardRarity.Uncommon: return Color.green; case CardRarity.Rare: return Color.blue; case CardRarity.Epic: return new Color(0.5f, 0, 0.5f); // Purple case CardRarity.Legendary: return Color.yellow; default: return Color.white; } } /// /// Returns default color for zone if no config is available /// private Color GetDefaultZoneColor(CardZone zone) { // Default zone colors from CardDefinition switch (zone) { case CardZone.AppleHills: return new Color(0.8f, 0.9f, 0.8f); // Light green case CardZone.Quarry: return new Color(0.85f, 0.8f, 0.7f); // Sandy brown case CardZone.Forest: return new Color(0.6f, 0.8f, 0.6f); // Forest green case CardZone.Mountain: return new Color(0.7f, 0.7f, 0.9f); // Bluish case CardZone.Beach: return new Color(0.9f, 0.85f, 0.7f); // Sandy yellow default: return Color.white; } } /// /// Called when the card is clicked /// public void OnClick() { OnCardClicked?.Invoke(this); } /// /// Returns the card data associated with this UI element /// public CardData GetCardData() { return cardData; } // Animation methods that can be called from UI events /// /// Called when the card is revealed from a booster pack /// public void OnShowAnimation() { // Stub for card reveal animation Logging.Debug($"[CardUIElement] Showing card: {cardData?.Name}"); // Could add animation code or call Animation Trigger here } /// /// Called when the card moves to the backpack after being revealed /// public void OnMoveToBackpackAnimation() { // Stub for animation when card moves to backpack Logging.Debug($"[CardUIElement] Moving card to backpack: {cardData?.Name}"); // Could add animation code or call Animation Trigger here } #if UNITY_EDITOR /// /// Create card data from definition and update the UI /// public void CreateFromDefinition() { if (cardDefinition == null) { Logging.Warning("[CardUIElement] Cannot create card data: No card definition assigned"); return; } cardData = cardDefinition.CreateCardData(); UpdateCardVisuals(); Logging.Debug($"[CardUIElement] Created card from definition: {cardData.Name}"); } #endif } #if UNITY_EDITOR [CustomEditor(typeof(CardUIElement))] public class CardUIElementEditor : Editor { public override void OnInspectorGUI() { DrawDefaultInspector(); CardUIElement cardUI = (CardUIElement)target; EditorGUILayout.Space(); if (GUILayout.Button("Create From Definition")) { cardUI.CreateFromDefinition(); EditorUtility.SetDirty(cardUI); } } } #endif }