using System;
using AppleHills.Data.CardSystem;
using Core;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UI.CardSystem
{
///
/// Displays a single card using the new visual system with frames, overlays, backgrounds, and zone shapes.
///
public class CardDisplay : MonoBehaviour
{
[Header("UI Elements")]
[SerializeField] private TextMeshProUGUI cardNameText;
[SerializeField] private Image cardImage;
[SerializeField] private Image frameImage;
[SerializeField] private Image overlayImage;
[SerializeField] private Image backgroundImage;
[SerializeField] private Image zoneShapeImage;
[Header("Card Data")]
[SerializeField] private CardData cardData;
[Header("Visual Settings")]
[SerializeField] private CardVisualConfig visualConfig;
#if UNITY_EDITOR
[Header("Editor Tools")]
[SerializeField] private CardDefinition editorCardDefinition;
#endif
// Events
public event Action OnCardClicked;
///
/// Sets up the card display with the given card data
///
public void SetupCard(CardData data)
{
if (data == null)
{
Logging.Warning("[CardDisplay] Attempted to setup card with null data");
return;
}
cardData = data;
UpdateCardVisuals();
}
///
/// Updates all visual elements based on current card data
///
private void UpdateCardVisuals()
{
if (cardData == null)
{
ClearCard();
return;
}
if (visualConfig == null)
{
Logging.Warning("[CardDisplay] No CardVisualConfig assigned. Visuals may not display correctly.");
}
// Update text
UpdateCardName();
// Update main card image
UpdateCardImage();
// Update rarity-based visuals (frame and overlay)
UpdateRarityVisuals();
// Update zone-based visuals (background and shape)
UpdateZoneVisuals();
Logging.Debug($"[CardDisplay] Updated visuals for card: {cardData.Name} (Rarity: {cardData.Rarity}, Zone: {cardData.Zone})");
}
///
/// Updates the card name text
///
private void UpdateCardName()
{
if (cardNameText != null)
{
cardNameText.text = cardData.Name ?? "Unknown Card";
}
}
///
/// Updates the main card image
///
private void UpdateCardImage()
{
if (cardImage != null)
{
if (cardData.CardImage != null)
{
cardImage.sprite = cardData.CardImage;
cardImage.enabled = true;
}
else
{
cardImage.sprite = null;
cardImage.enabled = false;
}
}
}
///
/// Updates frame and overlay based on card rarity
///
private void UpdateRarityVisuals()
{
if (visualConfig == null) return;
// Update frame
if (frameImage != null)
{
Sprite frameSprite = visualConfig.GetRarityFrame(cardData.Rarity);
if (frameSprite != null)
{
frameImage.sprite = frameSprite;
frameImage.enabled = true;
}
else
{
frameImage.enabled = false;
}
}
// Update overlay (can be null for some rarities)
if (overlayImage != null)
{
Sprite overlaySprite = visualConfig.GetRarityOverlay(cardData.Rarity);
if (overlaySprite != null)
{
overlayImage.sprite = overlaySprite;
overlayImage.enabled = true;
}
else
{
overlayImage.enabled = false;
}
}
}
///
/// Updates background and zone shape based on card zone and rarity
/// Special handling for Legendary cards
///
private void UpdateZoneVisuals()
{
if (visualConfig == null) return;
// Update background
// Legendary cards get special background, others get zone background
if (backgroundImage != null)
{
Sprite bgSprite = visualConfig.GetBackground(cardData.Zone, cardData.Rarity);
if (bgSprite != null)
{
backgroundImage.sprite = bgSprite;
backgroundImage.enabled = true;
}
else
{
backgroundImage.enabled = false;
}
}
// Update zone shape
// Legendary cards don't show shapes
if (zoneShapeImage != null)
{
Sprite shapeSprite = visualConfig.GetZoneShape(cardData.Zone, cardData.Rarity);
if (shapeSprite != null)
{
zoneShapeImage.sprite = shapeSprite;
zoneShapeImage.enabled = true;
}
else
{
zoneShapeImage.enabled = false;
}
}
}
///
/// Clears all visual elements
///
private void ClearCard()
{
if (cardNameText != null) cardNameText.text = "";
if (cardImage != null) cardImage.enabled = false;
if (frameImage != null) frameImage.enabled = false;
if (overlayImage != null) overlayImage.enabled = false;
if (backgroundImage != null) backgroundImage.enabled = false;
if (zoneShapeImage != null) zoneShapeImage.enabled = false;
}
///
/// Called when the card is clicked
///
public void OnClick()
{
OnCardClicked?.Invoke(this);
}
///
/// Returns the card data associated with this display
///
public CardData GetCardData()
{
return cardData;
}
///
/// Updates the visual config reference
///
public void SetVisualConfig(CardVisualConfig config)
{
visualConfig = config;
if (cardData != null)
{
UpdateCardVisuals();
}
}
#if UNITY_EDITOR
///
/// Editor-only: Creates card data from the assigned definition and displays it
///
public void PreviewFromDefinition()
{
if (editorCardDefinition == null)
{
Logging.Warning("[CardDisplay] Cannot preview: No card definition assigned in editor");
return;
}
cardData = editorCardDefinition.CreateCardData();
UpdateCardVisuals();
Logging.Debug($"[CardDisplay] Previewing card from definition: {cardData.Name}");
}
///
/// Editor-only: Clears the preview
///
public void ClearPreview()
{
cardData = null;
ClearCard();
Logging.Debug("[CardDisplay] Preview cleared");
}
#endif
}
#if UNITY_EDITOR
[CustomEditor(typeof(CardDisplay))]
public class CardDisplayEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
CardDisplay display = (CardDisplay)target;
EditorGUILayout.Space();
EditorGUILayout.LabelField("Editor Preview Tools", EditorStyles.boldLabel);
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Preview Card"))
{
display.PreviewFromDefinition();
EditorUtility.SetDirty(display);
}
if (GUILayout.Button("Clear Preview"))
{
display.ClearPreview();
EditorUtility.SetDirty(display);
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
EditorGUILayout.HelpBox(
"Assign a Card Definition in 'Editor Tools' section, then click 'Preview Card' to see how it will look at runtime.",
MessageType.Info
);
}
}
#endif
}