using System;
using Cute;
using UnityEngine;
using Wizard.Scripts.Network.Data.TaskData.ItemPurchase;

namespace Wizard;

public class ItemPurchasePlate : MonoBehaviour
{
	[SerializeField]
	private UITexture _textureThumbnail;

	[SerializeField]
	private UILabel _labelItemName;

	[SerializeField]
	private UILabel _labelCostName;

	[SerializeField]
	private UILabel _labelStockNum;

	[SerializeField]
	private UIButton _btnBuyItem;

	[SerializeField]
	private UISprite _useItemIconSprite;

	private ItemPurchaseData _itemData;

	public Action<ItemPurchaseData> _buyBtnCallBack;

	private int _dataIndex;

	public void SetData(int dataIndex)
	{
		_dataIndex = dataIndex;
		UpdateDisplay(Data.ItemPurchaseInfo.itemPurchaseList[dataIndex]);
	}

	public void UpdatePlate()
	{
		UpdateDisplay(Data.ItemPurchaseInfo.itemPurchaseList[_dataIndex]);
	}

	private void UpdateDisplay(ItemPurchaseData data)
	{
		_itemData = data;
		_textureThumbnail.mainTexture = Toolbox.ResourcesManager.LoadObject(Toolbox.ResourcesManager.GetAssetTypePath(data.textureName, ResourcesManager.AssetLoadPathType.Item, isfetch: true)) as Texture;
		_labelItemName.text = data.purchase_name;
		string typeName = "";
		string detailName = "";
		ShopCommonUtility.GetRewardNames((int)data.require_item_type, data.RequireUserGoodsId, data.require_item_num, out typeName, out detailName);
		_labelCostName.text = Data.SystemText.Get("Shop_0129", typeName, detailName);
		_useItemIconSprite.gameObject.SetActive(value: true);
		if (data.require_item_type == UserGoods.Type.RedEther)
		{
			_useItemIconSprite.spriteName = "icon_liquid_s";
		}
		else if (data.require_item_type == UserGoods.Type.Item && data.RequireUserGoodsId == 1001)
		{
			_useItemIconSprite.spriteName = "icon_orb_piece_s";
		}
		else
		{
			_useItemIconSprite.gameObject.SetActive(value: false);
		}
		if (data.isMonthryReset)
		{
			_labelStockNum.text = Data.SystemText.Get("Shop_0130", data.rest.ToString());
		}
		else
		{
			_labelStockNum.text = Data.SystemText.Get("Shop_0131", data.rest.ToString());
		}
		int haveUserGoods = PlayerStaticData.GetHaveUserGoods(_itemData.require_item_type, _itemData.RequireUserGoodsId);
		if (_itemData.rest > 0 && haveUserGoods >= _itemData.require_item_num)
		{
			UIManager.SetObjectToGrey(_btnBuyItem.gameObject, b: false);
			_btnBuyItem.isEnabled = true;
		}
		else
		{
			UIManager.SetObjectToGrey(_btnBuyItem.gameObject, b: true);
			_btnBuyItem.isEnabled = false;
		}
	}

	public void OnPushBuyButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		_buyBtnCallBack.Call(_itemData);
	}
}
