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

public class FontChanger
{
	public void Start()
	{
	}

	public static IEnumerator FontChange(Action callback)
	{
		string font = getFont();
		string text = "font_" + font + ".unity3d";
		bool isDone = false;
		string filename = text.ToLower();
		Toolbox.AssetManager.CacheAsset(filename, delegate
		{
			AssetHandle assetHandle = Toolbox.AssetManager.GetAssetHandle(filename);
			if (assetHandle != null)
			{
				assetHandle.isMultipleHandleIgnorAsset = true;
			}
			isDone = true;
		});
		while (!isDone)
		{
			yield return 0;
		}
		Global.GAME_FONT = Toolbox.ResourcesManager.LoadObject<Font>("Font/" + font);
		if (Global.GAME_FONT != null)
		{
			Global.GAME_FONT_NAME = font;
		}
		callback?.Invoke();
	}

	public static IEnumerator FontTryChangePersistant(Action callback)
	{
		string font = getFont();
		string text = "font_" + font + ".unity3d";
		bool isDone = false;
		bool isHandle = false;
		string filename = text.ToLower();
		Toolbox.AssetManager.CachePersistantAssetBeforeManifestLoad(filename, delegate
		{
			AssetHandle assetHandle = Toolbox.AssetManager.GetAssetHandle(filename);
			if (assetHandle != null)
			{
				assetHandle.isMultipleHandleIgnorAsset = true;
				Toolbox.ResourcesManager.RegistTemporaryAsset(filename);
				isHandle = true;
			}
			isDone = true;
		});
		while (!isDone)
		{
			yield return 0;
		}
		if (isHandle)
		{
			Global.GAME_FONT = Toolbox.ResourcesManager.LoadObject<Font>("Font/" + font);
		}
		else
		{
			Global.GAME_FONT = null;
		}
		if (Global.GAME_FONT != null)
		{
			Global.GAME_FONT_NAME = font;
		}
		else
		{
			Toolbox.AssetManager.ClearAssetCacheAssetBundle();
			Global.GAME_FONT_NAME = "A-OTF-KaiminTuStd-Bold";
		}
		callback?.Invoke();
	}

	public static void FontReset()
	{
		Global.GAME_FONT = null;
		Global.GAME_FONT_NAME = "A-OTF-KaiminTuStd-Bold";
	}

	private static string getFont()
	{
		return Toolbox.SavedataManager.GetString("LANG_FONT", Global.GetFontLangType(Global.LANG_TYPE.Eng.ToString()));
	}
}
