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

namespace Wizard;

public class SystemText
{
	private enum PARSE_TYPE
	{
		NORMAL,
		SINGULAR_PLURAL,
		MAX
	}

	private string[] patternTbl = new string[2] { "{(?<VALUE>[^@{}]*?(?<INDEX>-?\\d+)[^@{}]*?)}", "{(?<VALUE>[^@{}]*?(?<INDEX>-?\\d+)[^@{}]*?)@(?<SINGULAR>[^@{}]+?)@(?<PLURAL>[^@{}]+?)}" };

	public Dictionary<string, string> TextDictionary { get; private set; }

	public string RegionCode { get; private set; }

	public Global.LANG_TYPE RegionCodeLangType
	{
		get
		{
			foreach (Global.LANG_TYPE item in Enum.GetValues(typeof(Global.LANG_TYPE)).Cast<Global.LANG_TYPE>())
			{
				if (item.ToString() == RegionCode)
				{
					return item;
				}
			}
			return Global.LANG_TYPE.Jpn;
		}
	}

	public SystemText()
	{
		RegionCode = CustomPreference.GetTextLanguage();
		SetPreInstallText();
	}

	private void SetPreInstallText()
	{
		TextDictionary = new Dictionary<string, string>();
		LoadAndParse("systemtext", TextDictionary);
		LoadAndParse("errortext", TextDictionary);
		LoadAndParse("errorheadertext", TextDictionary);
	}

	private void LoadAndParse(string tag, Dictionary<string, string> dic)
	{
		TextAsset textAsset = Resources.Load("Json/Text/" + tag) as TextAsset;
		LocalizeJson.Parse(dic, RegionCode, textAsset.ToString());
	}

	public void Initialize()
	{
		LoadAndParse("commontext", TextDictionary);
		LoadAndParse("dialogtext", TextDictionary);
		LoadAndParse("loadtext", TextDictionary);
		LoadAndParse("titletext", TextDictionary);
		LoadAndParse("mypagetext", TextDictionary);
		LoadAndParse("accounttext", TextDictionary);
		LoadAndParse("mailtext", TextDictionary);
		LoadAndParse("missiontext", TextDictionary);
		LoadAndParse("storytext", TextDictionary);
		LoadAndParse("battletext", TextDictionary);
		LoadAndParse("tutorialtext", TextDictionary);
		LoadAndParse("cardtext", TextDictionary);
		LoadAndParse("shoptext", TextDictionary);
		LoadAndParse("othertoptext", TextDictionary);
		LoadAndParse("otherfriendtext", TextDictionary);
		LoadAndParse("otherconfigtext", TextDictionary);
		LoadAndParse("otherprofiletext", TextDictionary);
		LoadAndParse("otherrankingtext", TextDictionary);
		LoadAndParse("othersharemovietext", TextDictionary);
		LoadAndParse("othercodetext", TextDictionary);
		LoadAndParse("videohostingtext", TextDictionary);
		LoadAndParse("urltext", TextDictionary);
		LoadAndParse("loginbonus", TextDictionary);
		LoadAndParse("ranknametext", TextDictionary);
		LoadAndParse("guildtext", TextDictionary);
		LoadAndParse("sealedtext", TextDictionary);
		LoadAndParse("gatheringtext", TextDictionary);
		LoadAndParse("battlepasstext", TextDictionary);
		LoadAndParse("myrotationtext", TextDictionary);
		LoadAndParse("heroesbattletext", TextDictionary);
		LoadAndParse("speedchallengetext", TextDictionary);
	}

	public string Get(string key)
	{
		return Get(key, enableDebugReturn: true);
	}

	public string Get(string key, bool enableDebugReturn)
	{
		if (TextDictionary.ContainsKey(key))
		{
			return TextDictionary[key];
		}
		return "";
	}

	public string Get(string id, params string[] values)
	{
		return Convert(Get(id), values);
	}

	public string Convert(string text, params string[] values)
	{
		text = Parse(PARSE_TYPE.NORMAL, text, values);
		text = Parse(PARSE_TYPE.SINGULAR_PLURAL, text, values);
		if (RegionCode == Global.LANG_TYPE.Kor.ToString())
		{
			text = HangulManager.ConvertRule(text);
		}
		return text;
	}

	private string Parse(PARSE_TYPE parseType, string text, params string[] values)
	{
		if (values == null)
		{
			return text;
		}
		foreach (Match item in Regex.Matches(text, patternTbl[(int)parseType]))
		{
			string value = item.Groups["INDEX"].Value;
			bool flag = value.Contains("-");
			int num = int.Parse(value);
			if (flag)
			{
				num *= -1;
			}
			if (values.Length <= num || values[num] == null)
			{
				continue;
			}
			string value2 = item.Groups["VALUE"].Value;
			value2 = value2.Replace(item.Groups["INDEX"].Value, (!flag) ? values[num] : "");
			if (parseType == PARSE_TYPE.SINGULAR_PLURAL)
			{
				if (!flag)
				{
					value2 += " ";
				}
				value2 += item.Groups[(int.Parse(values[num]) == 1) ? "SINGULAR" : "PLURAL"].Value;
			}
			text = text.Replace(item.Groups[0].Value, value2);
		}
		return text;
	}

	public int Count(string searchStr)
	{
		return TextDictionary.Keys.Where((string c) => c.Contains(searchStr)).Count();
	}
}
