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

namespace Wizard.QuestSpecialResult;

public class QuestAssetManager
{
	public enum AssetId
	{
		Max
	}

	private enum AssetType
	{
		Effect,
		Se,
		Other,
		Max
	}

	private class AssetInfo
	{
		public AssetType AssetType { get; }

		public ResourcesManager.AssetLoadPathType? LoadPathType { get; }

		public string AssetName { get; }

		public string LoadPath { get; }

		public string FetchPath { get; }

		public AssetInfo(AssetType assetType, ResourcesManager.AssetLoadPathType? loadPathType, string assetName)
		{
			AssetType = assetType;
			LoadPathType = loadPathType;
			AssetName = assetName;
			LoadPath = (loadPathType.HasValue ? Toolbox.ResourcesManager.GetAssetTypePath(assetName, loadPathType.Value) : string.Empty);
			FetchPath = (loadPathType.HasValue ? Toolbox.ResourcesManager.GetAssetTypePath(assetName, loadPathType.Value, isfetch: true) : string.Empty);
		}
	}

	private readonly AssetInfo[] _assetInfoTable;

	private readonly Func<AssetInfo, List<string>, IEnumerator>[] _loadCoroutineTable;

	private readonly List<string> _loadedAssets = new List<string>();

	public bool IsLoaded { get; private set; }

	public QuestAssetManager()
	{
		_assetInfoTable = CreateAssetInfoTable();
		_loadCoroutineTable = CreateLoadCoroutineTable();
	}

	private AssetInfo[] CreateAssetInfoTable()
	{
		return new AssetInfo[0];
	}

	private Func<AssetInfo, List<string>, IEnumerator>[] CreateLoadCoroutineTable()
	{
		return new Func<AssetInfo, List<string>, IEnumerator>[3] { LoadEffectCoroutine, LoadSeCoroutine, LoadOtherAssetCoroutine };
	}

	public IEnumerator LoadAllCoroutine()
	{
		if (!IsLoaded)
		{
			AssetInfo[] assetInfoTable = _assetInfoTable;
			foreach (AssetInfo assetInfo in assetInfoTable)
			{
				yield return _loadCoroutineTable[(int)assetInfo.AssetType](assetInfo, _loadedAssets);
			}
			IsLoaded = true;
		}
	}

	private IEnumerator LoadEffectCoroutine(AssetInfo info, List<string> loadedAssets)
	{
		List<string> assets = new List<string> { info.LoadPath };
		yield return Toolbox.ResourcesManager.LoadAssetGroupSync(assets, null);
		loadedAssets.AddRange(assets);
		yield return LoadEffectShaderCoroutine(info, loadedAssets);
	}

	private IEnumerator LoadEffectShaderCoroutine(AssetInfo info, List<string> loadedAssets)
	{
		bool isLoading = true;
		GameObject effectObj = Toolbox.ResourcesManager.LoadObject<GameObject>(info.FetchPath);
		List<string> assets = GameMgr.GetIns().GetEffectMgr().SetUIParticleShader(effectObj, delegate
		{
			isLoading = false;
		});
		while (isLoading)
		{
			yield return null;
		}
		loadedAssets.AddRange(assets);
	}

	private IEnumerator LoadSeCoroutine(AssetInfo info, List<string> loadedAssets)
	{
		bool isLoading = true;
		string asset = GameMgr.GetIns().GetSoundMgr().LoadSe(info.AssetName, delegate
		{
			isLoading = false;
		});
		while (isLoading)
		{
			yield return null;
		}
		loadedAssets.Add(asset);
	}

	private IEnumerator LoadOtherAssetCoroutine(AssetInfo info, List<string> loadedAssets)
	{
		List<string> assets = new List<string> { info.LoadPath };
		yield return Toolbox.ResourcesManager.LoadAssetGroupSync(assets, null);
		loadedAssets.AddRange(assets);
	}

	public void UnloadAll()
	{
		if (IsLoaded)
		{
			Toolbox.ResourcesManager.RemoveAssetGroup(_loadedAssets);
			_loadedAssets.Clear();
			IsLoaded = false;
		}
	}

	private AssetInfo GetAssetInfo(AssetId id)
	{
		return _assetInfoTable[(int)id];
	}

	public string GetAssetName(AssetId id)
	{
		return GetAssetInfo(id).AssetName;
	}

	public string GetFetchPath(AssetId id)
	{
		return GetAssetInfo(id).FetchPath;
	}
}
