using System.Collections.Generic;

namespace Wizard;

public class AICardDataAssetSet
{
	private class AICardDataAssetTemporaryRecord
	{
		public string[] Data { get; set; }

		public int DiffFromLatestRotationPack { get; set; }
	}

	public List<AICardDataAsset> Set { get; private set; }

	public AICardDataAssetSet()
	{
		Set = new List<AICardDataAsset>();
	}

	public void ConvertCsvTextToAsset(List<string[]> csv)
	{
		int latestRotationCardPack = GetRotationLatestCardPackNumber();
		Dictionary<int, AICardDataAssetTemporaryRecord> latestCardCandidateDic = new Dictionary<int, AICardDataAssetTemporaryRecord>();
		foreach (string[] item in csv)
		{
			if (IsRegister(item))
			{
				Set.Add(new AICardDataAsset(item));
			}
		}
		foreach (AICardDataAssetTemporaryRecord value2 in latestCardCandidateDic.Values)
		{
			Set.Add(new AICardDataAsset(value2.Data));
		}
		bool IsRegister(string[] cardData)
		{
			string text = cardData[0];
			int num;
			int key;
			if (text.Length > 9)
			{
				num = int.Parse(text.Substring(text.Length - 2));
				key = int.Parse(text.Substring(0, text.Length - 2));
			}
			else
			{
				key = int.Parse(text);
				num = 0;
			}
			if (num == 0)
			{
				return true;
			}
			if (num < latestRotationCardPack)
			{
				return false;
			}
			cardData[0] = key.ToString();
			if (latestCardCandidateDic.TryGetValue(key, out var value))
			{
				int num2 = num - latestRotationCardPack;
				if (num2 < value.DiffFromLatestRotationPack)
				{
					latestCardCandidateDic[key] = new AICardDataAssetTemporaryRecord
					{
						Data = cardData,
						DiffFromLatestRotationPack = num2
					};
				}
			}
			else
			{
				latestCardCandidateDic.Add(key, new AICardDataAssetTemporaryRecord
				{
					Data = cardData,
					DiffFromLatestRotationPack = num - latestRotationCardPack
				});
			}
			return false;
		}
	}

	private int GetRotationLatestCardPackNumber()
	{
		return Data.Load.data.RotationLatestCardPackId % 100;
	}
}
