using System;
using System.Collections;
using System.Collections.Generic;
using Cute;
using UnityEngine;

namespace Wizard;

public class CardImageHelpder : MonoBehaviour
{
	private List<string> _loadList = new List<string>();

	private List<UIBase_CardManager.CardObjData> _cardObjDataList;

	private static readonly List<int> BOSS_RUSH_SPECIAL_CARD_ID = new List<int> { 117231010, 117231011, 117241010, 117241011, 117341010, 117341011 };

	public UIBase_CardManager.CardObjData GetCardObjData(int index)
	{
		return _cardObjDataList[index];
	}

	public void Load(List<int> cardIdList, float cardScale, int depth, Action<CardListTemplate> templateCustomize, Action onFinish)
	{
		StartCoroutine(LoadCoroutine(cardIdList, cardScale, depth, templateCustomize, onFinish));
	}

	private void OnDestroy()
	{
		Toolbox.ResourcesManager.RemoveAssetGroup(_loadList);
		_loadList.Clear();
	}

	public static string GetBossRushTexturePath(int id, bool isFetch)
	{
		CardParameter cardParameterFromId = CardMaster.GetInstance(CardMaster.CardMasterId.Default).GetCardParameterFromId(id);
		return Toolbox.ResourcesManager.GetAssetTypePath(cardParameterFromId.BaseCardId.ToString(), ResourcesManager.AssetLoadPathType.BossRushSpecialCard, isFetch);
	}

	public static bool IsSpecialCardId(int id)
	{
		return BOSS_RUSH_SPECIAL_CARD_ID.Contains(id);
	}

	private IEnumerator LoadCoroutine(List<int> cardIdList, float cardScale, int depth, Action<CardListTemplate> templateCustomize, Action onFinish)
	{
		bool isLoaded = false;
		List<UIBase_CardManager.CardObjData> originalCardObjectList = null;
		UIManager.GetInstance().CardLoadSelect(null, cardIdList, base.gameObject.layer, is2D: true, delegate
		{
			isLoaded = true;
			originalCardObjectList = UIManager.GetInstance().getCardList2DObjs();
			foreach (UIBase_CardManager.CardObjData item in originalCardObjectList)
			{
				item.CardObj.SetActive(value: false);
			}
		});
		while (!isLoaded)
		{
			yield return null;
		}
		List<string> specialCardLoadList = new List<string>();
		foreach (int cardId in cardIdList)
		{
			if (IsSpecialCardId(cardId))
			{
				specialCardLoadList.Add(GetBossRushTexturePath(cardId, isFetch: false));
			}
		}
		if (specialCardLoadList.Count > 0)
		{
			yield return StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(specialCardLoadList, delegate
			{
			}));
			_loadList.AddRange(specialCardLoadList);
		}
		_cardObjDataList = new List<UIBase_CardManager.CardObjData>(originalCardObjectList);
		originalCardObjectList.Clear();
		List<string> cardListAssetPathList = Toolbox.ResourcesManager.CardListAssetPathList;
		_loadList.AddRange(new List<string>(cardListAssetPathList));
		cardListAssetPathList.Clear();
		int num = 0;
		List<GameObject> cloneCardList = new List<GameObject>();
		foreach (UIBase_CardManager.CardObjData cardObjData in _cardObjDataList)
		{
			int id = cardIdList[num];
			GameObject cardObj = cardObjData.CardObj;
			CardListTemplate component = cardObj.GetComponent<CardListTemplate>();
			GameObject gameObject = cardObj;
			if (IsSpecialCardId(id))
			{
				component.AttachCardTexture(Toolbox.ResourcesManager.LoadObject(GetBossRushTexturePath(id, isFetch: true)) as Texture);
			}
			cardObj = UnityEngine.Object.Instantiate(cardObjData.CardObj);
			component = cardObj.GetComponent<CardListTemplate>();
			_cardObjDataList[num].CardObj = cardObj;
			cloneCardList.Add(cardObj);
			cardObj.SetActive(value: true);
			component.SetId(id);
			component.SetScale(cardScale);
			component.AddDepth(depth);
			templateCustomize.Call(component);
			gameObject.transform.parent = base.transform;
			gameObject.SetActive(value: false);
			num++;
		}
		foreach (GameObject item2 in cloneCardList)
		{
			item2.SetActive(value: false);
		}
		yield return null;
		foreach (GameObject item3 in cloneCardList)
		{
			item3.SetActive(value: true);
		}
		onFinish();
	}
}
