using System;
using Cute;
using UnityEngine;

namespace Wizard.Scripts.Network.Data.TaskData.BuildDeckPurchase;

public class BuildDeckPlate : MonoBehaviour
{
	private const int MAX_LENGTH_VIEW_DECK_PRODUCT_NAME = 15;

	private const int MAX_LENGTH_VIEW_DECK_PRODUCT_NAME_ALPHABET = 35;

	private readonly Vector3 POS_VIEW_SINGLE_PRODUCT_NAME = new Vector3(13f, -52f, 0f);

	[SerializeField]
	private UIEventListener _eventListenerDeckImage;

	[SerializeField]
	private UILabel _labelCostCrystalLeft;

	[SerializeField]
	private UILabel _labelCostCrystalCenter;

	[SerializeField]
	private UIButton m_BtnBuy;

	[SerializeField]
	private UILabel m_LabelBuy;

	[SerializeField]
	private UILabel m_LabelPurchased;

	[SerializeField]
	private UILabel _LabelPurchaseNum;

	[SerializeField]
	private UILabel _LabelProductName;

	[SerializeField]
	private UITexture _uiDeckTexture;

	[SerializeField]
	private UISprite _spriteClassColorIcon;

	[SerializeField]
	private UILabel _labelCostTicket;

	[SerializeField]
	private UITexture _leaderSkinTicketIcon;

	public BuildDeckProductInfo ProductInfo { get; private set; }

	public Texture ImageTexture { get; private set; }

	private void Start()
	{
		m_LabelPurchased.text = Wizard.Data.SystemText.Get("Shop_0100");
	}

	public void SetData(BuildDeckProductInfo productInfo, EventDelegate onPushBuyBtnCallback, Action onTapDeckImage)
	{
		ProductInfo = productInfo;
		ClassCharacterMasterData charaPrmByClassId = GameMgr.GetIns().GetDataMgr().GetCharaPrmByClassId(productInfo.leader_id);
		_LabelProductName.transform.localPosition = POS_VIEW_SINGLE_PRODUCT_NAME;
		int maxLength = (Global.IsAlphabetLanguage() ? 35 : 15);
		_LabelProductName.text = ShopCommonUtility.TrimProductName(productInfo.saleInfo.name, maxLength);
		ClassCharaPrm.SetClassLabelSetting(_LabelProductName, charaPrmByClassId.ClassColorId);
		_uiDeckTexture.mainTexture = Toolbox.ResourcesManager.LoadObject<Texture>(Toolbox.ResourcesManager.GetAssetTypePath(productInfo.saleInfo.path, ResourcesManager.AssetLoadPathType.ShopBuildDeckThumbnail, isfetch: true));
		_spriteClassColorIcon.gameObject.SetActive(value: true);
		_spriteClassColorIcon.spriteName = ClassCharaPrm.GetIconSpriteName(charaPrmByClassId.clan);
		m_BtnBuy.onClick.Clear();
		m_BtnBuy.onClick.Add(onPushBuyBtnCallback);
		_eventListenerDeckImage.onClick = null;
		_eventListenerDeckImage.onClick = delegate
		{
			onTapDeckImage.Call();
		};
		_SetBuyButton(productInfo);
	}

	private void _SetBuyButton(BuildDeckProductInfo productInfo)
	{
		int? num = null;
		if (productInfo.saleInfo.costCrystal.HasValue)
		{
			num = productInfo.saleInfo.costCrystal.Value;
		}
		if (productInfo.saleInfo.costTicket.HasValue)
		{
			num = productInfo.saleInfo.costTicket.Value;
		}
		UIManager.SetObjectToGrey(m_BtnBuy.gameObject, b: false);
		_labelCostTicket.gameObject.SetActive(value: false);
		_leaderSkinTicketIcon.gameObject.SetActive(value: false);
		int num2 = 0;
		if (productInfo.saleInfo.costTicket.HasValue && productInfo.saleInfo.costTicketItemId.HasValue)
		{
			num2 = PlayerStaticData.GetItemNum((int)productInfo.saleInfo.costTicketItemId.Value);
			_labelCostTicket.text = Wizard.Data.SystemText.Get("Shop_0189", productInfo.saleInfo.costTicket.Value.ToString());
			_leaderSkinTicketIcon.mainTexture = Toolbox.ResourcesManager.LoadObject<Texture>(ShopCommonUtility.GetTicketIconPath(productInfo.saleInfo.costTicketItemId.Value.ToString(), isFetch: true));
		}
		if (!productInfo.is_purchased)
		{
			m_BtnBuy.gameObject.SetActive(value: true);
			m_BtnBuy.isEnabled = true;
			m_LabelPurchased.gameObject.SetActive(value: false);
			if (productInfo.saleInfo.isFree)
			{
				m_LabelBuy.text = Wizard.Data.SystemText.Get("Shop_0099");
				return;
			}
			m_LabelBuy.text = Wizard.Data.SystemText.Get("Shop_0095");
			if (productInfo.saleInfo.costTicket.HasValue && productInfo.saleInfo.costTicketItemId.HasValue)
			{
				_labelCostTicket.gameObject.SetActive(value: true);
				_leaderSkinTicketIcon.gameObject.SetActive(value: true);
				UIManager.SetObjectToGrey(m_BtnBuy.gameObject, num2 < productInfo.saleInfo.costTicket.Value);
			}
			if (!productInfo.saleInfo.costCrystal.HasValue)
			{
				_labelCostCrystalLeft.gameObject.SetActive(value: false);
				_labelCostCrystalCenter.gameObject.SetActive(value: false);
			}
			else if (productInfo.purchase_num_max == 1)
			{
				SetCostLabel(num.Value, isCenter: true);
			}
			else
			{
				SetCostLabel(num.Value, isCenter: false);
			}
			if (productInfo.purchase_num_max == 1)
			{
				_LabelPurchaseNum.gameObject.SetActive(value: false);
				return;
			}
			if (productInfo.is_first_price && productInfo.purchase_num_current <= 0)
			{
				_LabelPurchaseNum.gameObject.SetActive(value: true);
				_LabelPurchaseNum.text = Wizard.Data.SystemText.Get("Shop_0122");
				return;
			}
			_LabelPurchaseNum.gameObject.SetActive(value: true);
			int num3 = productInfo.purchase_num_max - productInfo.purchase_num_current;
			_LabelPurchaseNum.text = Wizard.Data.SystemText.Get("Shop_0123", num3.ToString());
		}
		else
		{
			m_BtnBuy.gameObject.SetActive(value: false);
			m_LabelPurchased.gameObject.SetActive(value: true);
			_LabelPurchaseNum.gameObject.SetActive(value: false);
			if (productInfo.saleInfo.costTicket.HasValue && productInfo.saleInfo.costTicketItemId.HasValue)
			{
				_labelCostTicket.gameObject.SetActive(value: true);
				_leaderSkinTicketIcon.gameObject.SetActive(value: true);
			}
			if (!productInfo.saleInfo.costCrystal.HasValue)
			{
				_labelCostCrystalLeft.gameObject.SetActive(value: false);
				_labelCostCrystalCenter.gameObject.SetActive(value: false);
			}
			else
			{
				SetCostLabel(num.Value, isCenter: true);
			}
		}
	}

	private void SetCostLabel(int cost, bool isCenter)
	{
		string text = Wizard.Data.SystemText.Get("Shop_0112", cost.ToString());
		if (isCenter)
		{
			_labelCostCrystalLeft.gameObject.SetActive(value: false);
			_labelCostCrystalCenter.gameObject.SetActive(value: true);
			_labelCostCrystalCenter.text = text;
		}
		else
		{
			_labelCostCrystalLeft.gameObject.SetActive(value: true);
			_labelCostCrystalCenter.gameObject.SetActive(value: false);
			_labelCostCrystalLeft.text = text;
		}
	}
}
