using System;
using Core.Language;
using Core.Utility;
using Excel2Json;
using Fort23.UTool;
using GameLogic.Bag;
namespace Fort23.Mono
{
///
/// 商店购买确认弹框
///
[UIBinding(prefab = "ShopBuyItemPanel")]
public partial class ShopBuyItemPanel : UIPanel
{
private string _poolName = "ShopBuyItemPanelItemWidget";
private string _ShopPackBuyItemPanelPoolName = "ShopPackBuyItemPanelPoolName";
public int currentSelectCount;
public int maxCount;
private AccountFileInfo.ShopItem _shopItem;
private ShopItemConfig _shopItemConfig;
private Action _callback;
private bool _buyMark = false;
private void Init()
{
}
public async void CustomInit(AccountFileInfo.ShopItem shopItem, Action callback)
{
_buyMark = false;
UIManager.Instance.DormancyAllGComponent(_poolName);
_shopItem = shopItem;
ShopConfig shopConfig = ConfigComponent.Instance.Get(shopItem.id);
_shopItemConfig = ConfigComponent.Instance.Get(shopConfig.shopItemID);
_callback = callback;
WidgetItem itemWidget = await UIManager.Instance.CreateGComponent(null, ItemRoot, poolName: _poolName);
itemWidget.InitWidget(new ItemInfo(_shopItemConfig.itemId[0], _shopItemConfig.itemCount[0]));
Text_name.text = LanguageManager.Instance.Text(shopConfig.shopName);
Text_desc.text = LanguageManager.Instance.Text(itemWidget.itemInfo.config.itemName);
UseSlider.value = 1;
UseSlider.minValue = 1;
Text_BuyCount.text = "1";
Refresh();
UpdateShowData(1);
}
private void Refresh()
{
//无限制 最多购买十个
if (_shopItemConfig.buyCount == -1)
{
LimitObj.SetActive(false);
Text_LimitCount.text = "无";
int total = (int)(PlayerManager.Instance.BagController.GetItemCount(_shopItemConfig.costItemId) / _shopItemConfig.price);
maxCount = total >= 10 ? 10 : total;
if (maxCount <= 1)
{
maxCount = 1;
}
SliderObj.SetActive(maxCount > 1);
UseSlider.maxValue = maxCount;
UseSlider.value = 1;
}
else
{
LimitObj.SetActive(true);
Text_LimitCount.text = $"{_shopItemConfig.buyCount - _shopItem.buyCount}/{_shopItemConfig.buyCount}";
maxCount = _shopItemConfig.buyCount - _shopItem.buyCount;
int temp = (int)(PlayerManager.Instance.BagController.GetItemCount(_shopItemConfig.costItemId) / _shopItemConfig.price);
if (temp < maxCount)
{
maxCount = temp;
}
if (maxCount <= 1)
{
maxCount = 1;
}
SliderObj.SetActive(maxCount > 1);
UseSlider.maxValue = maxCount;
UseSlider.value = 1;
}
Text_Price.text = _shopItemConfig.price.ToString();
if (_shopItemConfig.costItemId == GlobalParam.RMB_1031)
{
ImageCurrency.gameObject.SetActive(false);
}
else
{
ImageCurrency.gameObject.SetActive(true);
}
ImageCurrency.icon_name = ConfigComponent.Instance.Get(_shopItemConfig.costItemId).icon;
}
protected override void AddEvent()
{
}
protected override void DelEvent()
{
}
public override void AddButtonEvent()
{
Btn_Close.onClick.AddListener((() => { UIManager.Instance.HideUIUIPanel(this); }));
Btn_Close1.onClick.AddListener((() => { UIManager.Instance.HideUIUIPanel(this); }));
UseSlider.onValueChanged.AddListener(OnValueChanged);
Btn_Add.onClick.AddListener(Add);
Btn_Reduce.onClick.AddListener(Reduce);
Btn_Function.onClick.AddListener(Function);
}
private async void Function()
{
_buyMark = true;
UIManager.Instance.HideUIUIPanel(this);
}
private void Reduce()
{
currentSelectCount--;
if (currentSelectCount < 1)
{
currentSelectCount = 1;
}
UseSlider.value = currentSelectCount;
}
private void Add()
{
currentSelectCount++;
if (currentSelectCount > maxCount)
{
currentSelectCount = maxCount;
}
UseSlider.value = currentSelectCount;
}
private void OnValueChanged(float arg0)
{
UpdateShowData(arg0);
}
private void UpdateShowData(float arg0)
{
int count = (int)arg0;
currentSelectCount = count;
Text_BuyCount.text = count.ToString();
Text_Price.text = $"{(count * _shopItemConfig.price).ToString()}";
}
public override void Close()
{
base.Close();
UIManager.Instance.DormancyAllGComponent(_poolName);
_callback?.Invoke(_buyMark, currentSelectCount);
}
}
}