using System.Collections.Generic;
using System.Text;
using Cute;
using UnityEngine;
using Wizard.Scripts.Network.Data.TaskData.BuildDeckPurchase;

namespace Wizard;

public class BuildDeckProductDetail : BaseProductDetail
{
	[SerializeField]
	private GameObject _labelSetHead;

	[SerializeField]
	private GameObject _topLineCardList;

	[SerializeField]
	private UILabel _labelCardNum;

	[SerializeField]
	private GameObject _rewardCardParent;

	[SerializeField]
	private GameObject _spotCardRoot;

	[SerializeField]
	private GameObject _spotCardOriginal;

	[SerializeField]
	private UIGrid _spotCardGrid;

	public void SetSingleProductDetail(BuildDeckProductInfo productInfo)
	{
		Texture textureProductImage = Toolbox.ResourcesManager.LoadObject<Texture>(Toolbox.ResourcesManager.GetAssetTypePath(productInfo.saleInfo.path, ResourcesManager.AssetLoadPathType.ShopBuildDeckThumbnail, isfetch: true));
		List<ShopCommonRewardInfo> list = new List<ShopCommonRewardInfo>();
		Dictionary<int, int> dictionary = new Dictionary<int, int>();
		Dictionary<int, int> dictionary2 = new Dictionary<int, int>();
		for (int i = 0; i < productInfo.rewardInfoList.Count; i++)
		{
			ShopCommonRewardInfo shopCommonRewardInfo = productInfo.rewardInfoList[i];
			if (shopCommonRewardInfo.Type == 5)
			{
				dictionary.Add((int)shopCommonRewardInfo.UserGoodsId, shopCommonRewardInfo.Num);
			}
			else
			{
				list.Add(productInfo.rewardInfoList[i]);
			}
		}
		for (int j = 0; j < productInfo.cardList.Count; j++)
		{
			BuildDeckCard buildDeckCard = productInfo.cardList[j];
			if (CardMaster.GetInstance(CardMaster.CardMasterId.Default).GetCardParameterFromId(buildDeckCard._cardId).IsBasicCard)
			{
				continue;
			}
			if (buildDeckCard.IsSpotCard)
			{
				if (dictionary2.ContainsKey(buildDeckCard._cardId))
				{
					dictionary2[buildDeckCard._cardId] = dictionary2[buildDeckCard._cardId] + buildDeckCard._number;
				}
				else
				{
					dictionary2[buildDeckCard._cardId] = buildDeckCard._number;
				}
			}
			else if (dictionary.ContainsKey(buildDeckCard._cardId))
			{
				dictionary[buildDeckCard._cardId] += buildDeckCard._number;
			}
			else
			{
				dictionary.Add(buildDeckCard._cardId, buildDeckCard._number);
			}
		}
		_spotCardRoot.SetActive(dictionary2.Count > 0);
		_spotCardOriginal.SetActive(value: true);
		foreach (KeyValuePair<int, int> item in dictionary2)
		{
			string userGoodsName = UserGoods.getUserGoodsName(UserGoods.Type.Card, item.Key);
			NGUITools.AddChild(_spotCardOriginal.transform.parent.gameObject, _spotCardOriginal).GetComponent<UILabel>().text = Data.SystemText.Get("Shop_0121", userGoodsName, item.Value.ToString());
		}
		_spotCardGrid.Reposition();
		_spotCardOriginal.SetActive(value: false);
		List<int> list2 = ConvertSortedCardList(dictionary, productInfo.featured_card_id);
		if (_rewardCardParent != null)
		{
			_rewardCardParent.SetActive(list2.Count > 0);
		}
		StringBuilder stringBuilder = new StringBuilder();
		int num = 0;
		for (int k = 0; k < list2.Count; k++)
		{
			int num2 = list2[k];
			string userGoodsName2 = UserGoods.getUserGoodsName(UserGoods.Type.Card, num2);
			int num3 = dictionary[num2];
			stringBuilder.Append(Data.SystemText.Get("Shop_0121", userGoodsName2, num3.ToString()));
			stringBuilder.Append("\n");
			num += num3;
		}
		_labelCardNum.text = Data.SystemText.Get("Shop_0119", num.ToString());
		if (list.Count <= 0 || productInfo.purchase_num_current > 0)
		{
			list.Clear();
			_labelSetHead.SetActive(value: false);
			_topLineCardList.SetActive(value: false);
		}
		else
		{
			_labelSetHead.SetActive(value: true);
			_topLineCardList.SetActive(value: true);
		}
		SetProductDetail(textureProductImage, list, stringBuilder.ToString());
	}

	private List<int> ConvertSortedCardList(Dictionary<int, int> rewardCardDict, int featuredCardId)
	{
		List<int> list = new List<int>(rewardCardDict.Keys);
		list.Sort(delegate(int x, int y)
		{
			CardParameter cardParameterFromId = CardMaster.GetInstance(CardMaster.CardMasterId.Default).GetCardParameterFromId(x);
			CardParameter cardParameterFromId2 = CardMaster.GetInstance(CardMaster.CardMasterId.Default).GetCardParameterFromId(y);
			if (cardParameterFromId.CardId == featuredCardId)
			{
				return -1;
			}
			if (cardParameterFromId2.CardId == featuredCardId)
			{
				return 1;
			}
			if (cardParameterFromId.IsPrizeCard && cardParameterFromId2.IsPrizeCard)
			{
				return 0;
			}
			if (cardParameterFromId.IsPrizeCard)
			{
				return -1;
			}
			if (cardParameterFromId2.IsPrizeCard)
			{
				return 1;
			}
			if (cardParameterFromId.Rarity != cardParameterFromId2.Rarity)
			{
				return cardParameterFromId2.Rarity.CompareTo(cardParameterFromId.Rarity);
			}
			return (cardParameterFromId.CardSetId != cardParameterFromId2.CardSetId) ? cardParameterFromId2.CardSetId.CompareTo(cardParameterFromId.CardSetId) : cardParameterFromId.CardId.CompareTo(cardParameterFromId2.CardId);
		});
		return list;
	}
}
