using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Wizard.Scripts.Network.Data.TaskData.Arena;

namespace Wizard;

public class GachaPointExchangePlate : MonoBehaviour
{
	private readonly Quaternion CARDOBJECT_ROTATION_QUATERNION = new Quaternion(0f, 0f, 0f, 0f);

	private const string SPOT_CARD_NUM_FORMAT = "[fcd24a]+{0}[-]";

	[SerializeField]
	private GameObject _objCardRoot;

	[SerializeField]
	private UILabel _labelCardName;

	[SerializeField]
	private UILabel _labelCardPossessionNum;

	[SerializeField]
	private UILabel _labelCardPossessionNumNormal;

	[SerializeField]
	private UILabel _labelCardPossessionNumPremium;

	[SerializeField]
	private UIButton _btnExchangeCard;

	[SerializeField]
	private UILabel _labelReward;

	[SerializeField]
	private UILabel _labelOwnedReward;

	private GameObject _cardObject;

	public void SetData(GachaPointExchangeInfo gachaPointInfo, Action<GachaPointExchangeInfo> onClickExchangButton, bool isExchangeable)
	{
		string text = gachaPointInfo.GetExchangeCardText();
		if (gachaPointInfo.RewardList.Any((Wizard.Scripts.Network.Data.TaskData.Arena.Reward c) => c.UserGoodsData.GoodsType == UserGoods.Type.Skin))
		{
			text = Data.SystemText.Get("Shop_0232") + " " + text;
		}
		if (gachaPointInfo.IsDisplayPrize)
		{
			text = Data.SystemText.Get("Shop_0235") + " " + text;
		}
		_labelCardName.text = text;
		DataMgr dataMgr = GameMgr.GetIns().GetDataMgr();
		int possessionCardNum = dataMgr.GetPossessionCardNum(gachaPointInfo.CardId, isIncludingSpotCard: false);
		int spotCardNum = dataMgr.SpotCardData.GetSpotCardNum(gachaPointInfo.CardId);
		_labelCardPossessionNumNormal.text = possessionCardNum + ((spotCardNum > 0) ? $"[fcd24a]+{spotCardNum}[-]" : string.Empty);
		int cardId = gachaPointInfo.CardId + 1;
		int possessionCardNum2 = dataMgr.GetPossessionCardNum(cardId, isIncludingSpotCard: false);
		_labelCardPossessionNumPremium.text = possessionCardNum2.ToString();
		int num = possessionCardNum + possessionCardNum2 + spotCardNum;
		_labelCardPossessionNum.text = num.ToString();
		_btnExchangeCard.onClick.Clear();
		if (isExchangeable)
		{
			_btnExchangeCard.onClick.Add(new EventDelegate(delegate
			{
				onClickExchangButton(gachaPointInfo);
			}));
			UIManager.SetObjectToGrey(_btnExchangeCard.gameObject, b: false);
		}
		else
		{
			_btnExchangeCard.isEnabled = false;
			UIManager.SetObjectToGrey(_btnExchangeCard.gameObject, b: true);
		}
		if (gachaPointInfo.RewardList.Count > 0)
		{
			_labelReward.text = GetRewardListText(gachaPointInfo.RewardList);
		}
		else
		{
			_labelReward.text = string.Empty;
		}
		if (gachaPointInfo.IsReceived)
		{
			_labelOwnedReward.gameObject.SetActive(value: true);
			_labelOwnedReward.text = Data.SystemText.Get("Shop_0163");
		}
		else
		{
			_labelOwnedReward.gameObject.SetActive(value: false);
		}
	}

	public void SetCardObject(GameObject cardObject, GameObject evacuationParent)
	{
		if (_cardObject != null)
		{
			_cardObject.SetActive(value: false);
			_cardObject.transform.parent = evacuationParent.transform;
		}
		cardObject.transform.parent = _objCardRoot.transform;
		cardObject.transform.localPosition = Vector3.zero;
		cardObject.transform.rotation = CARDOBJECT_ROTATION_QUATERNION;
		cardObject.SetActive(value: true);
		_cardObject = cardObject;
	}

	private static string GetRewardListText(List<Wizard.Scripts.Network.Data.TaskData.Arena.Reward> rewardList)
	{
		return UIUtil.CreateListText(rewardList.Select((Wizard.Scripts.Network.Data.TaskData.Arena.Reward reward) => reward.UserGoodsData).OrderBy(GachaUtil.GetRewardListSortIndex).Select(GachaUtil.GetRewardListGoodsTypeName)
			.Distinct()
			.ToList(), Data.SystemText.Get("Shop_0226"), Data.SystemText.Get("Shop_0225"));
	}
}
