using System;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using BaseGames.Inventory;
using BaseGames.Localization;
namespace BaseGames.UI.Inventory
{
///
/// 背包道具格子视图(对照 的显式序列化绑定风格)。
/// 由 通过对象池实例化并调用 。
///
[DisallowMultipleComponent]
public class ItemSlotView : MonoBehaviour
{
[Header("Visual")]
[SerializeField] private Image _icon;
[SerializeField] private TMP_Text _countText;
[Tooltip("\"新获得\"角标节点(IsNew 时显示)。")]
[SerializeField] private GameObject _newBadge;
[Header("Interaction")]
[SerializeField] private Button _selectButton;
[Tooltip("选中高亮节点(可空)。")]
[SerializeField] private GameObject _selectedHighlight;
private ItemSO _item;
public ItemSO Item => _item;
public Button SelectButton => _selectButton;
/// 绑定道具条目。点击时回调 onSelect(用于右侧详情面板)。
public void Bind(InventoryEntry entry, Action onSelect)
{
_item = entry.Item;
if (_item == null) return;
if (_icon != null)
{
_icon.sprite = _item.icon;
_icon.enabled = _item.icon != null;
}
if (_countText != null)
{
// 仅可叠加且数量 > 1 时显示数量角标
bool show = _item.stackable && entry.Count > 1;
_countText.text = show ? entry.Count.ToString() : string.Empty;
_countText.enabled = show;
}
if (_newBadge != null) _newBadge.SetActive(entry.IsNew);
if (_selectButton != null)
{
_selectButton.onClick.RemoveAllListeners();
var captured = _item;
if (onSelect != null)
_selectButton.onClick.AddListener(() => onSelect(captured));
}
}
public void SetSelected(bool selected)
{
if (_selectedHighlight != null) _selectedHighlight.SetActive(selected);
}
/// 本地化道具名(容错:未找到键回落 itemId)。
public string ResolveName()
{
if (_item == null) return string.Empty;
string loc = LocalizationManager.Get(_item.displayNameKey, LocalizationTable.Items);
return !string.IsNullOrEmpty(loc) && loc != _item.displayNameKey ? loc : _item.itemId;
}
}
}