using Cielonos.MainGame.Inventory;
using Cielonos.MainGame.Inventory.Collections;
using SLSUtilities.General;
using SLSUtilities.UI;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Cielonos.MainGame.UI
{
///
/// 奖励选择卡片 UI 组件。
/// 在奖励台(RewardTable)打开的界面中代表一个可选择的奖励选项。
/// 支持显示物品图标、名称、堆叠数,并且会对装备升级组件(PrefabricatedComponent)做特殊显示(显示目标装备图标,前缀为“升级:”)。
///
public class RewardChoiceSelector : UIElementBase
{
private RewardChoiceUIPage Page => PlayerCanvas.MainGamePages.rewardChoicePage;
/// 当前卡片绑定的奖励物品数据。
public ItemBase item;
/// 当前奖励的数量/堆叠数。
public int stack = 1;
public Button button;
public Image background, itemIcon, rarityFrame, selectorHint;
public TMP_Text itemName, itemStack;
public RectTransform tagContainer;
[Header("视觉设置")]
public Color normalColor = new Color(0.2f, 0.2f, 0.2f, 0.8f);
public Color selectedColor = new Color(0.4f, 0.6f, 0.9f, 0.9f);
private bool isSelected;
///
/// 初始化卡片,填充奖励的图标、文字、按钮监听等信息。
///
public void Setup(ItemBase itemRef)
{
item = itemRef;
if (item == null)
{
Hide();
return;
}
stack = 1;
if (item is ConsumableBase consumable)
{
stack = consumable.stackAmount;
}
ContentData data = item.contentData;
// 针对升级组件 PrefabricatedComponent 进行特殊处理:显示其所指向的目标升级装备的图标和信息
if (item is PrefabricatedComponent prefabComp && prefabComp.targetEquipment != null)
{
data = prefabComp.targetEquipment.contentData;
}
if (data == null)
{
Debug.LogWarning($"[RewardChoiceSelector] 隐藏选项:项 {item.gameObject.name} (或其目标装备) 的 ContentData 未配置!");
Hide();
return;
}
Sprite icon = data.itemIcon;
if (itemIcon != null)
{
itemIcon.sprite = icon;
itemIcon.enabled = icon != null;
}
if (itemName != null)
{
string nameText = data.displayNameKey.Localize("Items");
if (item is PrefabricatedComponent)
{
// 升级选项增加中文“升级:”前缀
nameText = $"升级: {nameText}";
}
itemName.text = nameText;
}
RefreshStackText();
SetSelected(false);
if (button != null)
{
button.onClick.RemoveAllListeners();
button.onClick.AddListener(OnClicked);
}
Show();
}
///
/// 设定当前选项是否被选中,更新背景颜色和指示状态。
///
public void SetSelected(bool selected)
{
isSelected = selected;
if (background != null)
{
background.color = isSelected ? selectedColor : normalColor;
}
if (selectorHint != null)
{
selectorHint.enabled = isSelected;
}
}
/// 卡片被点击时的回调,通知父级 UIPage 选中自己。
private void OnClicked()
{
Page?.SelectItem(this);
}
/// 根据当前堆叠数量,刷新卡片右下角的数量文本显示。
private void RefreshStackText()
{
if (this.itemStack == null) return;
if (stack > 1)
{
this.itemStack.text = $"x{stack}";
this.itemStack.gameObject.SetActive(true);
}
else
{
this.itemStack.gameObject.SetActive(false);
}
}
}
}