using System.Collections;
using Cute;
using UnityEngine;

namespace Wizard;

public class PurchaseConfirm : MonoBehaviour
{
	private const string SPRITE_NAME_CRYSTAL = "icon_crystal_s";

	private const string SPRITE_NAME_RUPY = "icon_rupy_s";

	private const string SPRITE_NAME_2PICK_TICKET = "icon_2pick_s";

	private const string SPRITE_NAME_ORB = "icon_orb_s";

	private const string SPRITE_NAME_ORB_PIECE = "icon_orb_piece_s";

	private const string SPRITE_NAME_RED_ETHER = "icon_liquid_s";

	private const string SPRITE_NAME_SPOT_CARD_POINT = "icon_spotpoint_s";

	[SerializeField]
	private UISprite _spriteConfirmItemIcon;

	[SerializeField]
	private UISprite _spriteHaveItemIcon;

	[SerializeField]
	private UITexture m_TextureConfirmTicket;

	[SerializeField]
	private UITexture m_TextureHaveTicket;

	[SerializeField]
	private UILabel m_LabelUseItemCnt;

	[SerializeField]
	private UILabel m_LabelBuyPack;

	[SerializeField]
	private UILabel m_LabelItemName;

	[SerializeField]
	private UILabel _premiumCardWarning;

	[SerializeField]
	private UILabel m_LabelBeforeItemCnt;

	[SerializeField]
	private UILabel m_LabelAfterItemCnt;

	[SerializeField]
	private UILabel m_LabelAfterItemUnit;

	[SerializeField]
	private GameObject _confirmObj;

	[SerializeField]
	private GameObject _haveObj;

	private const float WARNING_TEXT_OBJ_Y = -40f;

	[SerializeField]
	private UITable _tablePackPoint;

	[SerializeField]
	private UILabel _labelBeforPackPoint;

	[SerializeField]
	private UILabel _labelAfterPackPoint;

	[SerializeField]
	private UITable _preReleasePointTable;

	[SerializeField]
	private UILabel _preReleasePointBeforeLabel;

	[SerializeField]
	private UILabel _preReleasePointAfterlabel;

	[SerializeField]
	private GameObject _preReleasePointArleadyMax;

	[SerializeField]
	private Transform _nonItemRoot;

	[SerializeField]
	private CenteringUIWidget _nonItemCentering;

	[SerializeField]
	private UILabel _nonItemBeforeCountLabel;

	[SerializeField]
	private UILabel _nonItemAfterCountLabel;

	[SerializeField]
	private UILabel _nonItemRemainingLabel;

	[SerializeField]
	private UILabel _nonItemUnitLabel;

	[SerializeField]
	private UILabel _campaignNameLabel;

	[SerializeField]
	private UIScrollView _scrollView;

	[SerializeField]
	private GameObject _rootObj;

	[SerializeField]
	private UIScrollBar _scrollBar;

	[SerializeField]
	private GameObject _jpnLawRoot;

	[SerializeField]
	private GameObject _saleTimeExistLayout;

	[SerializeField]
	private GameObject _saleTimeNoneLayout;

	[SerializeField]
	private UIButton[] _showJpnLawButton;

	[SerializeField]
	private UILabel _expiryTimeLabel;

	private const float CONFIRM_OBJ_Y_WITH_PACKPOINT = -32f;

	private const float HAVE_OBJ_Y_WITH_PACKPOINT = 0f;

	private const float PACK_POINT_Y_WITH_PRE_RELEASE_POINT = -60f;

	private const float NON_ITEM_Y_WITH_PACKPOINT = -12f;

	private void Start()
	{
		UIButton[] showJpnLawButton = _showJpnLawButton;
		for (int i = 0; i < showJpnLawButton.Length; i++)
		{
			showJpnLawButton[i].onClick.Add(new EventDelegate(delegate
			{
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
				UIManager.GetInstance().WebViewHelper.OpenWebView(WebViewHelper.WebViewType.LEGALTEXT);
			}));
		}
		showJpnLawButton = _showJpnLawButton;
		for (int i = 0; i < showJpnLawButton.Length; i++)
		{
			showJpnLawButton[i].gameObject.SetActive(value: false);
		}
		_expiryTimeLabel.gameObject.SetActive(value: false);
	}

	public void SetConfirmDialog(int useNum, string purchaseText, int haveNum, UserGoods.Type type, long userGoodsId, ShopExpirtyInfo expirtyInfo, Texture texture = null)
	{
		switch (type)
		{
		case UserGoods.Type.Crystal:
			SetClystalConfirmDialog(useNum, purchaseText, haveNum, expirtyInfo);
			break;
		case UserGoods.Type.Rupy:
			SetRupyConfirmDialog(useNum, purchaseText, haveNum);
			break;
		case UserGoods.Type.Item:
			switch (Item.GetItemType(type, (int)userGoodsId))
			{
			case Item.Type.TwoPickTicket:
				Set2PickTicketConfirmDialog(useNum, purchaseText, haveNum);
				break;
			case Item.Type.CardPackTicket:
				SetTicketConfirmDialog(useNum, purchaseText, haveNum, texture);
				break;
			case Item.Type.Orb:
				SetOrbConfirmDialog(useNum, purchaseText, haveNum);
				break;
			case Item.Type.OrbPiece:
				SetOrbPieceConfirmDialog(useNum, purchaseText, haveNum);
				break;
			}
			break;
		case UserGoods.Type.RedEther:
			SetRedEtherConfirmDialog(useNum, purchaseText, haveNum);
			break;
		case UserGoods.Type.SpotCardPoint:
			SetSpotCardPointConfirmDialog(useNum, purchaseText, haveNum);
			break;
		}
		UpdateJpnLawObj();
	}

	private void UpdateJpnLawObj()
	{
		_ = _rootObj == null;
	}

	private void HideJpnLawObj()
	{
		if (!(_jpnLawRoot == null))
		{
			_jpnLawRoot.SetActive(value: false);
		}
	}

	public void SetClystalConfirmDialog(int useItemNum, string purchaseText, int haveItemCnt, ShopExpirtyInfo expirtyInfo)
	{
		SetIconImage(UserGoods.Type.Crystal);
		int afterItemNum = haveItemCnt - useItemNum;
		string unit = Data.SystemText.Get("Common_0116");
		string useItemNumText = Data.SystemText.Get("Shop_0091", useItemNum.ToString());
		SetLabelText(Data.SystemText.Get("Common_0201"), useItemNumText, afterItemNum, unit, purchaseText, haveItemCnt);
		UpdateJpnLawObj();
		_expiryTimeLabel.gameObject.SetActive(expirtyInfo.IsEnableText);
		_expiryTimeLabel.text = expirtyInfo.GetText();
		if (_saleTimeExistLayout != null && _saleTimeNoneLayout != null)
		{
			_saleTimeExistLayout.SetActive(expirtyInfo.IsEnableText);
			_saleTimeNoneLayout.SetActive(!expirtyInfo.IsEnableText);
		}
		HideJpnLawObj();
	}

	public void ChangePurchaceConfirmTextWidth()
	{
	}

	public void SetTicketConfirmDialog(int useItemNum, string purchaseText, int haveItemCnt, Texture icon)
	{
		SetIconImage(UserGoods.Type.Item, Item.Type.CardPackTicket, icon);
		int afterItemNum = haveItemCnt - useItemNum;
		string unit = Data.SystemText.Get("Common_0117");
		string useItemNumText = Data.SystemText.Get("Shop_0042", useItemNum.ToString());
		SetLabelText(Data.SystemText.Get("Common_0114"), useItemNumText, afterItemNum, unit, purchaseText, haveItemCnt);
		HideJpnLawObj();
	}

	public void SetRupyConfirmDialog(int useItemNum, string purchaseText, int haveItemCnt)
	{
		SetIconImage(UserGoods.Type.Rupy);
		int afterItemNum = haveItemCnt - useItemNum;
		string unit = Data.SystemText.Get("Common_0120");
		string useItemNumText = Data.SystemText.Get("Shop_0090", useItemNum.ToString());
		SetLabelText(Data.SystemText.Get("Common_0115"), useItemNumText, afterItemNum, unit, purchaseText, haveItemCnt);
		HideJpnLawObj();
	}

	public void Set2PickTicketConfirmDialog(int useItemNum, string purchaseText, int haveItemCnt)
	{
		SetIconImage(UserGoods.Type.Item, Item.Type.TwoPickTicket);
		int afterItemNum = haveItemCnt - useItemNum;
		string unit = Data.SystemText.Get("Common_0117");
		string useItemNumText = Data.SystemText.Get("Shop_0042", useItemNum.ToString());
		SetLabelText(Data.SystemText.Get("Mail_0037"), useItemNumText, afterItemNum, unit, purchaseText, haveItemCnt);
		HideJpnLawObj();
	}

	public void SetOrbConfirmDialog(int useItemNum, string purchaseText, int haveItemCnt)
	{
		SetIconImage(UserGoods.Type.Item, Item.Type.Orb);
		int afterItemNum = haveItemCnt - useItemNum;
		string unit = Data.SystemText.Get("Common_0116");
		string useItemNumText = Data.SystemText.Get("Shop_0133", useItemNum.ToString());
		SetLabelText(Data.SystemText.Get("Common_0158"), useItemNumText, afterItemNum, unit, purchaseText, haveItemCnt);
		HideJpnLawObj();
	}

	public void SetOrbPieceConfirmDialog(int useItemNum, string purchaseText, int haveItemCnt)
	{
		SetIconImage(UserGoods.Type.Item, Item.Type.OrbPiece);
		int afterItemNum = haveItemCnt - useItemNum;
		string unit = Data.SystemText.Get("Common_0116");
		string useItemNumText = Data.SystemText.Get("Shop_0140", useItemNum.ToString());
		SetLabelText(Data.SystemText.Get("Common_0159"), useItemNumText, afterItemNum, unit, purchaseText, haveItemCnt);
		HideJpnLawObj();
	}

	public void SetRedEtherConfirmDialog(int useItemNum, string purchaseText, int haveItemCnt)
	{
		SetIconImage(UserGoods.Type.RedEther);
		int afterItemNum = haveItemCnt - useItemNum;
		string unit = Data.SystemText.Get("Common_0116");
		string useItemNumText = Data.SystemText.Get("Shop_0134", useItemNum.ToString());
		SetLabelText(Data.SystemText.Get("Common_0205"), useItemNumText, afterItemNum, unit, purchaseText, haveItemCnt);
		HideJpnLawObj();
	}

	public void SetSpotCardPointConfirmDialog(int useItemNum, string purchaseText, int haveItemCnt)
	{
		SetIconImage(UserGoods.Type.SpotCardPoint);
		int afterItemNum = haveItemCnt - useItemNum;
		string unit = Data.SystemText.Get("Common_0116");
		string useItemNumText = Data.SystemText.Get("Shop_0151", useItemNum.ToString());
		SetLabelText(Data.SystemText.Get("Common_0161"), useItemNumText, afterItemNum, unit, purchaseText, haveItemCnt);
		HideJpnLawObj();
	}

	public void SetLeaderSkinTicketConfirmDialog(int cost, string purchaseText, int haveItem, long itemId)
	{
		int afterItemNum = haveItem - cost;
		string unit = Data.SystemText.Get("Common_0117");
		string useItemNumText = Data.SystemText.Get("Shop_0042", cost.ToString());
		SetLabelText(Data.SystemText.Get("Common_0114"), useItemNumText, afterItemNum, unit, purchaseText, haveItem);
		SetIconImage(UserGoods.Type.Item, Item.Type.LeaderSkinTicket, Toolbox.ResourcesManager.LoadObject<Texture>(ShopCommonUtility.GetTicketIconRightDownPath(itemId.ToString(), isFetch: true)));
		HideJpnLawObj();
	}

	private void SetIconImage(UserGoods.Type type, Item.Type ticket = (Item.Type)0, Texture packIcon = null)
	{
		switch (type)
		{
		case UserGoods.Type.Crystal:
			ViewIconSprite("icon_crystal_s");
			break;
		case UserGoods.Type.Rupy:
			ViewIconSprite("icon_rupy_s");
			break;
		case UserGoods.Type.Item:
			switch (ticket)
			{
			case Item.Type.TwoPickTicket:
				ViewIconSprite("icon_2pick_s");
				break;
			case Item.Type.CardPackTicket:
			case Item.Type.LeaderSkinTicket:
				_spriteConfirmItemIcon.gameObject.SetActive(value: false);
				_spriteHaveItemIcon.gameObject.SetActive(value: false);
				m_TextureConfirmTicket.gameObject.SetActive(value: true);
				m_TextureHaveTicket.gameObject.SetActive(value: true);
				m_TextureConfirmTicket.mainTexture = packIcon;
				m_TextureHaveTicket.mainTexture = packIcon;
				break;
			case Item.Type.Orb:
				ViewIconSprite("icon_orb_s");
				break;
			case Item.Type.OrbPiece:
				ViewIconSprite("icon_orb_piece_s");
				break;
			}
			break;
		case UserGoods.Type.RedEther:
			ViewIconSprite("icon_liquid_s");
			break;
		case UserGoods.Type.SpotCardPoint:
			ViewIconSprite("icon_spotpoint_s");
			break;
		}
	}

	private void ViewIconSprite(string spriteName)
	{
		_spriteConfirmItemIcon.gameObject.SetActive(value: true);
		_spriteHaveItemIcon.gameObject.SetActive(value: true);
		m_TextureConfirmTicket.gameObject.SetActive(value: false);
		m_TextureHaveTicket.gameObject.SetActive(value: false);
		_spriteConfirmItemIcon.spriteName = spriteName;
		_spriteHaveItemIcon.spriteName = spriteName;
	}

	private void HideIcon()
	{
		_spriteConfirmItemIcon.gameObject.SetActive(value: false);
		_spriteHaveItemIcon.gameObject.SetActive(value: false);
		m_TextureConfirmTicket.gameObject.SetActive(value: false);
		m_TextureHaveTicket.gameObject.SetActive(value: false);
	}

	private void SetLabelText(string itemName, string useItemNumText, int afterItemNum, string unit, string purchaseText, int haveItemCnt)
	{
		m_LabelUseItemCnt.text = useItemNumText;
		m_LabelBuyPack.text = purchaseText;
		m_LabelItemName.text = itemName;
		m_LabelBeforeItemCnt.text = haveItemCnt.ToString();
		m_LabelAfterItemCnt.text = afterItemNum.ToString();
		m_LabelAfterItemUnit.text = unit;
	}

	public void SetCardBuy(string moneyKindName, int moneyHave, int moneyUse, CardParameter cardData)
	{
		SystemText systemText = Data.SystemText;
		m_LabelUseItemCnt.text = systemText.Get("Card_0064", moneyUse.ToString());
		m_LabelBuyPack.text = systemText.Get("Card_0077", cardData.CardName);
		string text = CheckMyRotationCardBuyCheck(cardData);
		if (cardData.IsResurgentCard)
		{
			SetWarningTextId("Card_0300");
		}
		else if (!cardData.IsAvailableFormat(Format.Unlimited, ClassType.None))
		{
			SetWarningTextId("Card_0204");
		}
		else if (cardData.SameKindNumMaxInUnlimited == 1)
		{
			SetWarningTextId("Card_0205");
		}
		else if (cardData.SameKindNumMaxInUnlimited == 2)
		{
			SetWarningTextId("Card_0206");
		}
		else if (text != null)
		{
			SetWarningText(text);
		}
		else if (Data.Crossover.IsWithinPracticePeriod)
		{
			int num = Mathf.Min(cardData.SameKindNumMaxInCrossoverMainClass, cardData.SameKindNumMaxInCrossoverSubClass);
			if (num == 0)
			{
				SetWarningText(systemText.Get("Card_0277"));
			}
			else if (num < FormatBehaviorManager.GetDefaultBehaviour(Format.Crossover).DeckSameKindCardNumMax)
			{
				SetWarningText(systemText.Get("Card_0278", num.ToString()));
			}
		}
		m_LabelItemName.text = systemText.Get("Common_0113");
		int moneyAfter = moneyHave - moneyUse;
		SetCardCommon(moneyHave, moneyAfter);
		HideJpnLawObj();
	}

	private string CheckMyRotationCardBuyCheck(CardParameter cardParameter)
	{
		if (!Data.MyRotationAllInfo.IsMyRotationEnable)
		{
			return null;
		}
		int num = 0;
		int num2 = 0;
		int num3 = 0;
		IFormatBehavior defaultBehaviour = FormatBehaviorManager.GetDefaultBehaviour(Format.MyRotation);
		foreach (MyRotationInfo myRotationInfo in Data.MyRotationAllInfo.MyRotationInfoList)
		{
			int sameCardCount = myRotationInfo.GetSameCardCount(cardParameter.BaseCardId);
			if (sameCardCount != defaultBehaviour.DeckSameKindCardNumMax)
			{
				if (sameCardCount == 0)
				{
					num2++;
				}
				else
				{
					num++;
				}
				num3 = sameCardCount;
			}
		}
		if (num == 0 && num2 == 0)
		{
			return null;
		}
		int count = Data.MyRotationAllInfo.MyRotationInfoList.Count;
		if (num2 == count)
		{
			return Data.SystemText.Get("MyRotation_ID_19");
		}
		if (num2 > 0)
		{
			return Data.SystemText.Get("MyRotation_ID_20");
		}
		if (num == count)
		{
			return Data.SystemText.Get("MyRotation_ID_21", num3.ToString());
		}
		return Data.SystemText.Get("MyRotation_ID_22", num3.ToString());
	}

	public void SetCardSell(string moneyKindName, int moneyHave, int moneyGet, string cardName, bool inIsPremiumCard)
	{
		SystemText systemText = Data.SystemText;
		m_LabelUseItemCnt.text = systemText.Get("Card_0079", moneyGet.ToString());
		m_LabelBuyPack.text = systemText.Get("Card_0078", cardName);
		m_LabelItemName.text = systemText.Get("Card_0097");
		if (inIsPremiumCard)
		{
			_premiumCardWarning.gameObject.SetActive(value: true);
			_premiumCardWarning.text = systemText.Get("Card_0144");
			_confirmObj.transform.localPosition = new Vector3(0f, -40f, 0f);
			_haveObj.transform.localPosition = new Vector3(0f, -40f, 0f);
		}
		else
		{
			_premiumCardWarning.gameObject.SetActive(value: false);
		}
		int moneyAfter = moneyHave + moneyGet;
		SetCardCommon(moneyHave, moneyAfter);
		HideJpnLawObj();
	}

	private void SetCardCommon(int moneyHave, int moneyAfter)
	{
		SetIconImage(UserGoods.Type.RedEther);
		SystemText systemText = Data.SystemText;
		m_LabelBeforeItemCnt.text = moneyHave.ToString();
		m_LabelAfterItemCnt.text = systemText.Get("Shop_0045", moneyAfter.ToString());
		m_LabelAfterItemUnit.text = systemText.Get("Card_0100");
	}

	public void SetWarningTextId(string warningTextId)
	{
		SystemText systemText = Data.SystemText;
		SetWarningText(systemText.Get(warningTextId));
	}

	public void SetWarningText(string warningText)
	{
		_premiumCardWarning.gameObject.SetActive(value: true);
		_premiumCardWarning.text = warningText;
		_confirmObj.transform.localPosition = new Vector3(0f, -40f, 0f);
		_haveObj.transform.localPosition = new Vector3(0f, -40f, 0f);
	}

	public IEnumerator SetCardPackPoint(int beforePoint, int afterPoint)
	{
		_tablePackPoint.gameObject.SetActive(value: true);
		_labelBeforPackPoint.text = beforePoint.ToString();
		_labelAfterPackPoint.text = afterPoint.ToString();
		_confirmObj.transform.localPosition = new Vector3(0f, -32f, 0f);
		_haveObj.transform.localPosition = new Vector3(0f, 0f, 0f);
		if (_nonItemRoot != null)
		{
			_nonItemRoot.localPosition = new Vector3(0f, -12f, 0f);
		}
		yield return null;
		_tablePackPoint.Reposition();
	}

	public IEnumerator SetCardPackPointWithPreRelease(int beforePoint, int afterPoint, int beforePrereleasePoint, int afterPrereleasePoint, bool arleadyLimitPreReleasePoint)
	{
		_tablePackPoint.gameObject.SetActive(value: true);
		_labelBeforPackPoint.text = beforePoint.ToString();
		_labelAfterPackPoint.text = afterPoint.ToString();
		_preReleasePointArleadyMax.SetActive(arleadyLimitPreReleasePoint);
		if (!arleadyLimitPreReleasePoint)
		{
			_preReleasePointTable.gameObject.SetActive(value: true);
		}
		_preReleasePointBeforeLabel.text = beforePrereleasePoint.ToString();
		_preReleasePointAfterlabel.text = afterPrereleasePoint.ToString();
		_confirmObj.transform.localPosition = new Vector3(0f, -32f, 0f);
		_haveObj.transform.localPosition = new Vector3(0f, 0f, 0f);
		_tablePackPoint.transform.localPosition = new Vector3(0f, -60f, 0f);
		yield return null;
		_tablePackPoint.Reposition();
		_preReleasePointTable.Reposition();
	}

	public void SetFreePacksConfirmDialog(string purchaseText, int useNum, int beforeCount, int buyPackCount, string campaignName)
	{
		HideIcon();
		m_LabelUseItemCnt.text = string.Empty;
		m_LabelBuyPack.text = purchaseText;
		_haveObj.SetActive(value: false);
		_nonItemRoot.gameObject.SetActive(value: true);
		_nonItemBeforeCountLabel.text = (beforeCount / buyPackCount).ToString();
		_nonItemAfterCountLabel.text = ((beforeCount - useNum) / buyPackCount).ToString();
		_nonItemRemainingLabel.text = Data.SystemText.Get("Shop_0224");
		_nonItemUnitLabel.text = Data.SystemText.Get("Shop_0221");
		_nonItemCentering.Reposition();
		HideJpnLawObj();
		SetCampaignName(campaignName);
	}

	public void SetFreePacksLayout()
	{
		_confirmObj.transform.localPosition = new Vector3(0f, -45f, 0f);
		_nonItemRoot.localPosition = new Vector3(0f, -24f, 0f);
	}

	private void SetCampaignName(string name)
	{
		if (!(_campaignNameLabel == null))
		{
			_campaignNameLabel.gameObject.SetActive(value: true);
			_campaignNameLabel.text = name;
		}
	}
}
