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

namespace Wizard;

public class ResourceDownloadCardFactory : MonoBehaviour
{
	private List<string> _loadFileList = new List<string>();

	private static string GetClassIconPath(CardBasePrm.ClanType classType, bool isFetch)
	{
		int num = (int)classType;
		return Toolbox.ResourcesManager.GetAssetTypePath("class_card_" + num.ToString("00"), ResourcesManager.AssetLoadPathType.CardFrameClassIcon, isFetch);
	}

	public bool CanView(List<int> cardIdList)
	{
		foreach (string load in GetLoadList(cardIdList, needCardFrame: true))
		{
			AssetHandle assetHandle = Toolbox.AssetManager.GetAssetHandle(load);
			if (load == null)
			{
				return false;
			}
			if (assetHandle.isReDownloadAsset(CustomPreference.IsNormalResource))
			{
				return false;
			}
		}
		return true;
	}

	private List<string> GetLoadList(List<int> cardIdList, bool needCardFrame)
	{
		List<string> cardPath = GetCardPath(cardIdList);
		if (needCardFrame)
		{
			cardPath.Add(UIManager.GetInstance().GetSceneAssetPath(UIAtlasManager.AssetBundleNames.CardFrame));
		}
		for (int i = 0; i < 9; i++)
		{
			cardPath.Add(GetClassIconPath((CardBasePrm.ClanType)i, isFetch: false));
		}
		return cardPath;
	}

	public void Load(List<int> cardIdList, GameObject parent, Action<List<CardListTemplate>> onFinish)
	{
		bool preferSynchronousLoad = true;
		List<string> loadList = GetLoadList(cardIdList, needCardFrame: false);
		_loadFileList.AddRange(loadList);
		loadList.Add(UIManager.GetInstance().GetSceneAssetPath(UIAtlasManager.AssetBundleNames.CardFrame));
		UIManager.GetInstance().StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroup(loadList, delegate
		{
			UIManager.GetInstance().AddResidentAtlas(UIAtlasManager.AssetBundleNames.CardFrame);
			CreateAllCard(cardIdList, parent, onFinish);
		}, isProgress: true, preferSynchronousLoad));
	}

	private List<string> GetCardPath(List<int> cardIdList)
	{
		CardMaster.CardMasterId cardMasterId = CardMaster.CardMasterId.Default;
		List<string> list = new List<string>();
		foreach (int cardId in cardIdList)
		{
			CardParameter cardParameterFromId = CardMaster.GetInstance(cardMasterId).GetCardParameterFromId(cardId);
			string requestPath = GetRequestPath(cardParameterFromId);
			if (!string.IsNullOrEmpty(requestPath))
			{
				list.Add(requestPath);
			}
		}
		return list;
	}

	private string GetRequestPath(CardParameter param)
	{
		int resourceCardId = CardMaster.GetInstance(CardMaster.CardMasterId.Default).GetCardParameterFromId(param.NormalCardId).ResourceCardId;
		if (param.CharType == CardBasePrm.CharaType.NORMAL)
		{
			return Toolbox.ResourcesManager.GetAssetTypePath(resourceCardId.ToString(), ResourcesManager.AssetLoadPathType.UnitCardMaterial);
		}
		return Toolbox.ResourcesManager.GetAssetTypePath(resourceCardId.ToString(), ResourcesManager.AssetLoadPathType.SpellCardMaterial);
	}

	private void CreateAllCard(List<int> cardIdList, GameObject parent, Action<List<CardListTemplate>> onFinish)
	{
		GameObject prefab = Resources.Load("Prefab/Cards/CardListTemplate") as GameObject;
		List<CardListTemplate> list = new List<CardListTemplate>();
		foreach (int cardId in cardIdList)
		{
			list.Add(CreateCard(cardId, parent, prefab));
		}
		onFinish.Call(list);
	}

	private CardListTemplate CreateCard(int cardId, GameObject parent, GameObject prefab)
	{
		CardParameter cardParameterFromId = CardMaster.GetInstance(CardMaster.CardMasterId.Default).GetCardParameterFromId(cardId);
		CardListTemplate component = NGUITools.AddChild(parent, prefab).GetComponent<CardListTemplate>();
		component.SetId(cardId);
		string text = Data.SystemText.Get($"LoadCard_{cardId}");
		component._nameLabel.text = text;
		component._newLabel.gameObject.SetActive(value: false);
		component.RotationOnlyIconVisible = cardParameterFromId.IsResurgentCard;
		if (cardParameterFromId.CharType == CardBasePrm.CharaType.NORMAL)
		{
			component._atkLabel.text = cardParameterFromId.Atk.ToString();
			component._lifeLabel.text = cardParameterFromId.Life.ToString();
			UIManager.GetInstance().getUIBase_CardManager().SetNumberLabelStyle(component._atkLabel, cardParameterFromId.IsFoil);
			UIManager.GetInstance().getUIBase_CardManager().SetNumberLabelStyle(component._lifeLabel, cardParameterFromId.IsFoil);
		}
		component._costLabel.text = cardParameterFromId.Cost.ToString();
		UIManager.GetInstance().getUIBase_CardManager().SetNumberLabelStyle(component._costLabel, cardParameterFromId.IsFoil);
		component._cardTexture.material = UIBase_CardManager.Get2dCardMaterial(cardParameterFromId);
		component._cardTexture.transform.localPosition += new Vector3(0f, 0f, 3f);
		component._frameSprite.transform.localPosition += new Vector3(0f, 0f, 1f);
		component._cardTexture.uvRect = Global.CARD_2D_UV_RECT;
		component.SetFrame(cardParameterFromId);
		Global.SetRepositionNameLabel(component._nameLabel, text, is2D: true);
		UIManager.GetInstance().getUIBase_CardManager().SetNameLabelStyle(component._nameLabel, cardParameterFromId.IsFoil);
		component._classIconTexture.mainTexture = ClassCharaPrm.GetClassIconTexture((int)cardParameterFromId.Clan);
		component.SetBossRushIconSprite(string.Empty);
		return component;
	}

	private void OnDestroy()
	{
		Release();
	}

	public void Release()
	{
		Toolbox.ResourcesManager.RemoveAssetGroup(_loadFileList);
		_loadFileList.Clear();
	}
}
