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

namespace Wizard;

public class UIAtlasManager
{
	public enum AssetBundleNames
	{
		MypageComon,
		Battle,
		BattleLang,
		RoomMatch,
		AreaSelect,
		DeckEdit,
		Arena,
		Gacha,
		Profile,
		Ranking,
		Friend,
		CardFrame,
		CardFramePhantom,
		Tournament,
		Quest,
		Scenario,
		Bingo,
		RedEtherCampaign,
		NeutralPopularityVote,
		BossRush,
		Max
	}

	private List<UIAtlas> _atlasList = new List<UIAtlas>();

	private List<UIAtlas> _residentAtlasList = new List<UIAtlas>();

	private List<string> _loadPath;

	private bool _isLoadFinish;

	private HashSet<AssetBundleNames> _loadedAtlasHash = new HashSet<AssetBundleNames>();

	private bool IsTutorialFinish()
	{
		if (Data.Load != null && Data.Load.data != null && Data.Load.data._userTutorial.tutorial_step != 100)
		{
			return true;
		}
		return false;
	}

	public void LoadCurrentScene(Action callback = null)
	{
		if (_isLoadFinish && callback != null)
		{
			callback();
			return;
		}
		if (_loadPath == null)
		{
			_loadPath = new List<string>();
			_atlasList = new List<UIAtlas>();
		}
		else
		{
			_loadPath.Clear();
			_atlasList.Clear();
		}
		HashSet<AssetBundleNames> assetBundleNameSet = GetLoadTargetSet(UIManager.GetInstance().GetCurrentScene());
		if (IsTutorialFinish())
		{
			assetBundleNameSet.Add(AssetBundleNames.AreaSelect);
			assetBundleNameSet.Add(AssetBundleNames.Battle);
		}
		UpdateAssetPathFromAssetBundleNames(assetBundleNameSet);
		UIManager.GetInstance().StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(_loadPath, delegate
		{
			foreach (AssetBundleNames item in assetBundleNameSet)
			{
				UnityEngine.Object listData = Toolbox.ResourcesManager.LoadObject<GameObject>(GetAtlasPath(item, null, isload: true));
				AddAssetObjList(listData);
			}
			_isLoadFinish = true;
			foreach (AssetBundleNames item2 in assetBundleNameSet)
			{
				_loadedAtlasHash.Add(item2);
			}
			if (callback != null)
			{
				callback();
			}
		}));
	}

	public void RemoveSceneAtlas(UIManager.ViewScene inLeaveScene)
	{
		HashSet<AssetBundleNames> loadTargetSet = GetLoadTargetSet(inLeaveScene);
		HashSet<AssetBundleNames> hashSet = new HashSet<AssetBundleNames>(_loadedAtlasHash);
		hashSet.ExceptWith(loadTargetSet);
		List<string> list = new List<string>(hashSet.ToList().Count);
		foreach (AssetBundleNames item in hashSet)
		{
			list.Add(GetAtlasPath(item, null));
		}
		if (list.Count != 0)
		{
			Toolbox.ResourcesManager.RemoveAssetGroup(list);
		}
		foreach (AssetBundleNames loop_name in hashSet)
		{
			_loadedAtlasHash.RemoveWhere((AssetBundleNames x) => (x == loop_name) ? true : false);
		}
	}

	public string GetAtlasPath(AssetBundleNames assetname, string singlebundlename = "", bool isload = false)
	{
		string result = "";
		if (isload)
		{
			if (string.IsNullOrEmpty(singlebundlename))
			{
				switch (assetname)
				{
				case AssetBundleNames.MypageComon:
					result = "Ui/TextureAtlas/MyPageSubTextureAtlas/" + assetname;
					break;
				case AssetBundleNames.Battle:
					result = "Ui/TextureAtlas/BattleTextureAtlas/" + assetname;
					break;
				case AssetBundleNames.BattleLang:
					result = "Uilang/TextureAtlas/BattleLangTextureAtlas/" + assetname;
					break;
				case AssetBundleNames.RoomMatch:
					result = "Ui/TextureAtlas/RoomMatchAtlas/" + assetname;
					break;
				case AssetBundleNames.AreaSelect:
					result = "Ui/TextureAtlas/AreaSelectTextureAtlas/" + assetname;
					break;
				case AssetBundleNames.DeckEdit:
					result = "Ui/TextureAtlas/DeckEditTextureAtlas/" + assetname;
					break;
				case AssetBundleNames.Arena:
					result = "Ui/TextureAtlas/ArenaTextureAtlas/" + assetname;
					break;
				case AssetBundleNames.Gacha:
					result = "Ui/TextureAtlas/GachaTextureAtlas/" + assetname;
					break;
				case AssetBundleNames.Profile:
					result = "Ui/TextureAtlas/Profile/" + assetname;
					break;
				case AssetBundleNames.Ranking:
					result = "Ui/TextureAtlas/Ranking/" + assetname;
					break;
				case AssetBundleNames.Friend:
					result = "Ui/TextureAtlas/Friend/" + assetname;
					break;
				case AssetBundleNames.CardFrame:
					result = "Ui/TextureAtlas/CardFrame/" + assetname;
					break;
				case AssetBundleNames.CardFramePhantom:
					result = "Ui/TextureAtlas/CardFramePhantom/" + assetname;
					break;
				case AssetBundleNames.Tournament:
					result = "Ui/TextureAtlas/Tournament/" + assetname;
					break;
				case AssetBundleNames.Quest:
					result = "Ui/TextureAtlas/Quest/" + assetname;
					break;
				case AssetBundleNames.Scenario:
					result = "Ui/TextureAtlas/Scenario/" + assetname;
					break;
				case AssetBundleNames.Bingo:
					result = "Ui/TextureAtlas/BingoTextureAtlas/" + assetname;
					break;
				case AssetBundleNames.RedEtherCampaign:
					result = "Ui/TextureAtlas/RedEtherCampaign/" + assetname;
					break;
				case AssetBundleNames.NeutralPopularityVote:
					result = $"Ui/TextureAtlas/PopularityVote/{assetname}";
					break;
				case AssetBundleNames.BossRush:
					result = $"Ui/TextureAtlas/BossRush/{assetname}";
					break;
				}
			}
			else
			{
				switch (assetname)
				{
				case AssetBundleNames.MypageComon:
					result = "Ui/TextureAtlas/MyPageSubTextureAtlas/" + singlebundlename.ToString();
					break;
				case AssetBundleNames.Battle:
					result = "Ui/TextureAtlas/BattleTextureAtlas/" + singlebundlename.ToString();
					break;
				case AssetBundleNames.BattleLang:
					result = "Uilang/TextureAtlas/BattleLangTextureAtlas/" + singlebundlename.ToString();
					break;
				case AssetBundleNames.RoomMatch:
					result = "Ui/TextureAtlas/RoomMatchAtlas/" + singlebundlename.ToString();
					break;
				case AssetBundleNames.AreaSelect:
					result = "Ui/TextureAtlas/AreaSelectTextureAtlas/" + singlebundlename.ToString();
					break;
				case AssetBundleNames.Arena:
					result = "Ui/TextureAtlas/ArenaTextureAtlas/" + singlebundlename.ToString();
					break;
				case AssetBundleNames.Gacha:
					result = "Ui/TextureAtlas/GachaTextureAtlas/" + singlebundlename.ToString();
					break;
				case AssetBundleNames.Profile:
					result = "Ui/TextureAtlas/Profile/" + singlebundlename.ToString();
					break;
				case AssetBundleNames.Ranking:
					result = "Ui/TextureAtlas/Ranking/" + singlebundlename.ToString();
					break;
				case AssetBundleNames.Friend:
					result = "Ui/TextureAtlas/Friend/" + singlebundlename.ToString();
					break;
				case AssetBundleNames.Tournament:
					result = "Ui/TextureAtlas/Tournament/" + singlebundlename.ToString();
					break;
				case AssetBundleNames.Quest:
					result = "Ui/TextureAtlas/Quest/" + singlebundlename.ToString();
					break;
				case AssetBundleNames.Scenario:
					result = "Ui/TextureAtlas/Scenario/" + singlebundlename.ToString();
					break;
				case AssetBundleNames.Bingo:
					result = "Ui/TextureAtlas/BingoTextureAtlas/" + singlebundlename.ToString();
					break;
				case AssetBundleNames.RedEtherCampaign:
					result = "Ui/TextureAtlas/RedEtherCampaign/" + singlebundlename.ToString();
					break;
				case AssetBundleNames.NeutralPopularityVote:
					result = "Ui/TextureAtlas/PopularityVote/" + singlebundlename;
					break;
				case AssetBundleNames.BossRush:
					result = "Ui/TextureAtlas/BossRush/" + singlebundlename;
					break;
				}
			}
		}
		else
		{
			string text = "ui_";
			string text2 = "";
			if (string.IsNullOrEmpty(singlebundlename))
			{
				if (assetname == AssetBundleNames.BattleLang)
				{
					text = "uilang_";
				}
				text2 = assetname.ToString().ToLower();
			}
			else
			{
				if (singlebundlename.ToString().ToLower().Contains("battlelang"))
				{
					text = "uilang_";
				}
				text2 = singlebundlename.ToString().ToLower();
			}
			result = text + text2 + ".unity3d";
		}
		return result;
	}

	public void AddResidentAtlas(AssetBundleNames atlasName)
	{
		string atlasPath = GetAtlasPath(atlasName, null, isload: true);
		UIAtlas component = Toolbox.ResourcesManager.LoadObject<GameObject>(atlasPath).GetComponent<UIAtlas>();
		_residentAtlasList.Add(component);
	}

	public void RemoveResidentAtlas(AssetBundleNames atlasName)
	{
		string name = atlasName.ToString();
		int num = _residentAtlasList.FindIndex((UIAtlas atlas) => atlas.name == name);
		if (num < 0)
		{
			Debug.LogError("not found in resident atlas list : " + name);
		}
		else
		{
			_residentAtlasList.RemoveAt(num);
		}
	}

	private HashSet<AssetBundleNames> GetLoadTargetSet(UIManager.ViewScene scene)
	{
		HashSet<AssetBundleNames> hashSet = new HashSet<AssetBundleNames>();
		switch (scene)
		{
		case UIManager.ViewScene.MyPage:
			hashSet.Add(AssetBundleNames.MypageComon);
			break;
		case UIManager.ViewScene.Battle:
		case UIManager.ViewScene.RankMatch:
			hashSet.Add(AssetBundleNames.Battle);
			hashSet.Add(AssetBundleNames.BattleLang);
			hashSet.Add(AssetBundleNames.Arena);
			break;
		case UIManager.ViewScene.Room:
			hashSet.Add(AssetBundleNames.RoomMatch);
			break;
		case UIManager.ViewScene.StorySelectPage:
		case UIManager.ViewScene.StorySelectionWorld:
		case UIManager.ViewScene.AreaSelect:
		case UIManager.ViewScene.StoryChapterSelectionFlowChart:
			hashSet.Add(AssetBundleNames.AreaSelect);
			break;
		case UIManager.ViewScene.RedEtherCampaignLobby:
			hashSet.Add(AssetBundleNames.AreaSelect);
			hashSet.Add(AssetBundleNames.RedEtherCampaign);
			break;
		case UIManager.ViewScene.DeckCardEdit:
		case UIManager.ViewScene.CardAllList:
		case UIManager.ViewScene.CardDestruct:
		case UIManager.ViewScene.SealedDeckEdit:
			hashSet.Add(AssetBundleNames.DeckEdit);
			break;
		case UIManager.ViewScene.TwoPick:
			hashSet.Add(AssetBundleNames.MypageComon);
			hashSet.Add(AssetBundleNames.Arena);
			break;
		case UIManager.ViewScene.Sealed:
		case UIManager.ViewScene.Colosseum:
		case UIManager.ViewScene.CompetitionLobby:
			hashSet.Add(AssetBundleNames.Arena);
			break;
		case UIManager.ViewScene.SealedCardPackOpen:
		case UIManager.ViewScene.Gacha:
		case UIManager.ViewScene.BuildDeckPurchasePage:
		case UIManager.ViewScene.CardSleevePurchasePage:
		case UIManager.ViewScene.ClassSkinPurchasePage:
			hashSet.Add(AssetBundleNames.Gacha);
			break;
		case UIManager.ViewScene.Bingo:
			hashSet.Add(AssetBundleNames.Bingo);
			hashSet.Add(AssetBundleNames.Quest);
			break;
		case UIManager.ViewScene.Profile:
		case UIManager.ViewScene.CrossoverPortal:
			hashSet.Add(AssetBundleNames.Profile);
			break;
		case UIManager.ViewScene.Ranking:
			hashSet.Add(AssetBundleNames.Ranking);
			break;
		case UIManager.ViewScene.Gathering:
			hashSet.Add(AssetBundleNames.Friend);
			hashSet.Add(AssetBundleNames.Ranking);
			hashSet.Add(AssetBundleNames.Tournament);
			break;
		case UIManager.ViewScene.Guild:
		case UIManager.ViewScene.Friend:
			hashSet.Add(AssetBundleNames.Friend);
			hashSet.Add(AssetBundleNames.Ranking);
			break;
		case UIManager.ViewScene.LotteryPage:
			hashSet.Add(AssetBundleNames.Arena);
			hashSet.Add(AssetBundleNames.Quest);
			break;
		case UIManager.ViewScene.QuestSelectionPage:
			hashSet.Add(AssetBundleNames.Quest);
			hashSet.Add(AssetBundleNames.BossRush);
			break;
		case UIManager.ViewScene.Scenario:
		case UIManager.ViewScene.Scenario2:
			hashSet.Add(AssetBundleNames.Scenario);
			break;
		case UIManager.ViewScene.NeutralPopularityVote:
			hashSet.Add(AssetBundleNames.NeutralPopularityVote);
			break;
		case UIManager.ViewScene.BossRushLobby:
			hashSet.Add(AssetBundleNames.BossRush);
			hashSet.Add(AssetBundleNames.Arena);
			hashSet.Add(AssetBundleNames.Quest);
			break;
		case UIManager.ViewScene.FreePackCampaign:
			hashSet.Add(AssetBundleNames.Arena);
			break;
		}
		return hashSet;
	}

	private void UpdateAssetPathFromAssetBundleNames(HashSet<AssetBundleNames> assetBundleNameSet)
	{
		_loadPath.Clear();
		foreach (AssetBundleNames item in assetBundleNameSet)
		{
			string atlasPath = GetAtlasPath(item);
			_loadPath.Add(atlasPath);
		}
	}

	private void AddAssetObjList(UnityEngine.Object listData)
	{
		try
		{
			UIAtlas component = ((GameObject)listData).GetComponent<UIAtlas>();
			if (component != null)
			{
				_atlasList.Add(component);
			}
		}
		catch
		{
		}
	}

	public List<UIAtlas> GetAtlasList()
	{
		return _residentAtlasList.Union(_atlasList).ToList();
	}

	public bool getAssetBundleEnd()
	{
		return _isLoadFinish;
	}

	public void setAssetBundleEnd(bool flag)
	{
		_isLoadFinish = flag;
	}

	public void OnDestroyView(UIManager.ViewScene scene)
	{
		foreach (AssetBundleNames item in GetLoadTargetSet(scene))
		{
			string atlasPath = GetAtlasPath(item);
			Toolbox.AssetManager.UnloadAsset(atlasPath);
		}
	}

	public void AttachAtlas(List<GameObject> obj_list, bool isTargetChildren = true)
	{
		List<UIAtlas> atlasList = GetAtlasList();
		if (atlasList == null || atlasList.Count <= 0)
		{
			return;
		}
		List<UISprite> list = new List<UISprite>();
		for (int i = 0; i < obj_list.Count; i++)
		{
			UISprite[] collection = (isTargetChildren ? obj_list[i].GetComponentsInChildren<UISprite>(includeInactive: true) : UnityEngine.Object.FindObjectsOfType<UISprite>());
			list.AddRange(collection);
		}
		for (int j = 0; j < list.Count; j++)
		{
			UISprite uISprite = list[j];
			string spriteName = uISprite.spriteName;
			if (uISprite.atlas != null || string.IsNullOrEmpty(spriteName) || spriteName == "collider")
			{
				continue;
			}
			foreach (UIAtlas item in atlasList)
			{
				foreach (UISpriteData sprite in item.spriteList)
				{
					if (spriteName == sprite.name)
					{
						uISprite.atlas = item;
						goto end_IL_00f1;
					}
				}
				continue;
				end_IL_00f1:
				break;
			}
		}
	}
}
