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

namespace Wizard;

public static class UIUtil
{
	private const int TEMP_STRING_BUILDER_CAPACITY = 1024;

	public static StringBuilder _tempStringBuilder = new StringBuilder(1024);

	public static StringBuilder GetTempStringBuilder()
	{
		_tempStringBuilder.Length = 0;
		return _tempStringBuilder;
	}

	public static void SetPositionX(Transform targetTransform, float x)
	{
		Vector2 vector = targetTransform.localPosition;
		vector.x = x;
		targetTransform.localPosition = vector;
	}

	public static void SetPositionY(Transform targetTransform, float y)
	{
		Vector2 vector = targetTransform.localPosition;
		vector.y = y;
		targetTransform.localPosition = vector;
	}

	public static void AddPositionY(Transform targetTransform, float addY)
	{
		Vector2 vector = targetTransform.localPosition;
		vector.y += addY;
		targetTransform.localPosition = vector;
	}

	public static void SetLocalPositionY(Transform targetTransform, float y)
	{
		Vector3 localPosition = targetTransform.localPosition;
		localPosition.y = y;
		targetTransform.localPosition = localPosition;
	}

	public static string GetBattleTypeName(NetworkDefine.ServerBattleType battleType)
	{
		switch (battleType)
		{
		case NetworkDefine.ServerBattleType.Free:
			return Data.SystemText.Get("Battle_0001");
		case NetworkDefine.ServerBattleType.Rank:
			return Data.SystemText.Get("Battle_0002");
		case NetworkDefine.ServerBattleType.OpenRoom:
		case NetworkDefine.ServerBattleType.RoomTwoPick:
		case NetworkDefine.ServerBattleType.Gathering:
		case NetworkDefine.ServerBattleType.OfflineEvent:
			return Data.SystemText.Get("Battle_0003");
		case NetworkDefine.ServerBattleType.TwoPick:
			return Data.SystemText.Get("Battle_0004");
		case NetworkDefine.ServerBattleType.Colosseum:
		case NetworkDefine.ServerBattleType.ColosseumTwoPick:
			return Data.SystemText.Get("Colosseum_0001");
		case NetworkDefine.ServerBattleType.Competition:
		case NetworkDefine.ServerBattleType.CompetitionTwoPick:
			return Data.SystemText.Get("Competition_0006");
		case NetworkDefine.ServerBattleType.Sealed:
			return Data.SystemText.Get("BattleName_Sealed");
		default:
			Debug.LogError($"unsupported BattleType : {battleType}({(int)battleType})");
			return string.Empty;
		}
	}

	public static string GetFormatName(Format format)
	{
		return format switch
		{
			Format.Rotation => Data.SystemText.Get("Common_0154"), 
			Format.Unlimited => Data.SystemText.Get("Common_0155"), 
			Format.PreRotation => Data.SystemText.Get("Common_0163"), 
			Format.Sealed => Data.SystemText.Get("BattleName_Sealed"), 
			Format.Hof => Data.SystemText.Get("Colosseum_0108"), 
			Format.Crossover => Data.SystemText.Get("Common_0166"), 
			Format.MyRotation => Data.SystemText.Get("Common_0178"), 
			Format.Avatar => Data.SystemText.Get("HeroesBattle_0001"), 
			_ => string.Empty, 
		};
	}

	public static bool IsTwoPickForReplay(BattleParameter battleParameter)
	{
		if (battleParameter.IsTwoPick)
		{
			if (battleParameter.TwoPickFormat != TwoPickFormat.Cube && battleParameter.TwoPickFormat != TwoPickFormat.Chaos && battleParameter.TwoPickFormat != TwoPickFormat.BackdraftChaos)
			{
				return battleParameter.TwoPickFormat != TwoPickFormat.BackdraftCube;
			}
			return false;
		}
		return false;
	}

	public static string GetFormatSmallSpriteName(Format format)
	{
		return format switch
		{
			Format.Rotation => "icon_timesliprotation_s", 
			Format.Unlimited => "icon_unlimited_s", 
			Format.PreRotation => "icon_prerotation_s", 
			Format.Sealed => "icon_sealed_s", 
			Format.Crossover => "icon_crossover_s", 
			Format.MyRotation => "icon_myrotation_s", 
			Format.Avatar => "icon_heroesbattle_s", 
			_ => string.Empty, 
		};
	}

	public static string GetShortClassName(CardBasePrm.ClanType clan)
	{
		switch (clan)
		{
		case CardBasePrm.ClanType.MIN:
			return Data.SystemText.Get("Common_0170");
		case CardBasePrm.ClanType.ROYAL:
			return Data.SystemText.Get("Common_0171");
		case CardBasePrm.ClanType.WITCH:
			return Data.SystemText.Get("Common_0172");
		case CardBasePrm.ClanType.DRAGON:
			return Data.SystemText.Get("Common_0173");
		case CardBasePrm.ClanType.NECRO:
			return Data.SystemText.Get("Common_0174");
		case CardBasePrm.ClanType.VAMPIRE:
			return Data.SystemText.Get("Common_0175");
		case CardBasePrm.ClanType.BISHOP:
			return Data.SystemText.Get("Common_0176");
		case CardBasePrm.ClanType.NEMESIS:
			return Data.SystemText.Get("Common_0177");
		default:
			Debug.LogError($"unsupported clan type : {clan}");
			return string.Empty;
		}
	}

	public static string GetMyRotationDefaultDeckClassName(CardBasePrm.ClanType clan)
	{
		switch (clan)
		{
		case CardBasePrm.ClanType.MIN:
			return Data.SystemText.Get("Common_0179");
		case CardBasePrm.ClanType.ROYAL:
			return Data.SystemText.Get("Common_0180");
		case CardBasePrm.ClanType.WITCH:
			return Data.SystemText.Get("Common_0181");
		case CardBasePrm.ClanType.DRAGON:
			return Data.SystemText.Get("Common_0182");
		case CardBasePrm.ClanType.NECRO:
			return Data.SystemText.Get("Common_0183");
		case CardBasePrm.ClanType.VAMPIRE:
			return Data.SystemText.Get("Common_0184");
		case CardBasePrm.ClanType.BISHOP:
			return Data.SystemText.Get("Common_0185");
		case CardBasePrm.ClanType.NEMESIS:
			return Data.SystemText.Get("Common_0186");
		default:
			Debug.LogError($"unsupported clan type : {clan}");
			return string.Empty;
		}
	}

	public static bool IsValidIdDigits(string idString, int numOfDigits)
	{
		if (idString.Length != numOfDigits)
		{
			return false;
		}
		if (!int.TryParse(idString, out var result))
		{
			return false;
		}
		return result >= 0;
	}

	public static bool IsValidViewerId(string idString)
	{
		return IsValidIdDigits(idString, 9);
	}

	public static string CreateListText(IList<string> list, string separator, string header = null, string footer = null)
	{
		StringBuilder tempStringBuilder = GetTempStringBuilder();
		if (header != null)
		{
			tempStringBuilder.Append(header);
		}
		if (list.Count > 0)
		{
			tempStringBuilder.Append(list[0]);
			for (int i = 1; i < list.Count; i++)
			{
				tempStringBuilder.Append(separator);
				tempStringBuilder.Append(list[i]);
			}
		}
		if (footer != null)
		{
			tempStringBuilder.Append(footer);
		}
		return tempStringBuilder.ToString();
	}

	public static void AdjustClassInfoPartsSize(ClassInfoParts classInfoParts, FlexibleGrid grid, int widthMax)
	{
		grid.Reposition();
		Bounds bounds = NGUIMath.CalculateRelativeWidgetBounds(grid.transform, considerInactive: false);
		while (bounds.size.x > (float)widthMax)
		{
			int num = classInfoParts.ClassNameLabel.fontSize - 1;
			if (num <= 0)
			{
				Debug.LogError("invalid font size");
				break;
			}
			classInfoParts.ClassNameLabel.fontSize = num;
			if (classInfoParts.SubClassNameLabel != null)
			{
				classInfoParts.SubClassNameLabel.fontSize = num;
			}
			grid.Reposition();
			bounds = NGUIMath.CalculateRelativeWidgetBounds(grid.transform, considerInactive: false);
		}
		UIManager.GetInstance().StartCoroutine(grid.RepositionNextFrame());
	}

	public static void SetCountryTexture(UITexture texture, string countryCode)
	{
		bool flag = !string.IsNullOrEmpty(countryCode);
		texture.gameObject.SetActive(flag);
		texture.mainTexture = (flag ? (Toolbox.ResourcesManager.LoadObject(Toolbox.ResourcesManager.GetAssetTypePath(countryCode, ResourcesManager.AssetLoadPathType.Country_M, isfetch: true)) as Texture) : null);
	}

	public static string ExtractStringAlphabet(string str)
	{
		return Regex.Replace(str, "[^a-zA-z]", string.Empty);
	}

	public static int ExtractStringNumber(string str)
	{
		return int.Parse(Regex.Replace(str, "[^0-9]", string.Empty));
	}
}
