using System.Collections.Generic;

namespace Wizard;

public class AIDataLibrary
{
	private readonly AIDeckData _basicDic;

	private readonly AIDeckData _commonDic;

	private readonly AIDeckData _allyCommonDic;

	private readonly Dictionary<string, AIDeckData> _deckDic;

	private readonly AIStyleData _commonStyle;

	private readonly Dictionary<string, AIStyleData> _deckStyleDic;

	private AISetUpData setupInfoBuf;

	private readonly Dictionary<string, AIEmoteSet> emoteDic;

	public AISetUpData SetupInfoBuf => setupInfoBuf;

	public AIDataLibrary()
	{
		_basicDic = new AIDeckData();
		_commonDic = new AIDeckData();
		_allyCommonDic = new AIDeckData();
		_commonStyle = new AIStyleData();
		_deckStyleDic = new Dictionary<string, AIStyleData>();
		_deckDic = new Dictionary<string, AIDeckData>();
		emoteDic = new Dictionary<string, AIEmoteSet>();
	}

	public void Clear()
	{
		_basicDic.Clear();
		_commonDic.Clear();
		_allyCommonDic.Clear();
		_deckDic.Clear();
		_commonStyle.Clear();
		_deckStyleDic.Clear();
		emoteDic.Clear();
	}

	public void SaveBattleSetUpInfo(int classID, AI_LOGIC_LV logicLv, string deckName, string styleName, string emoteName, bool useEmote, bool useInnerEmote, int enemyAiID, List<int> specialAbilityList)
	{
		setupInfoBuf = new AISetUpData(classID, logicLv, deckName, styleName, emoteName, useEmote, useInnerEmote, enemyAiID, specialAbilityList);
	}

	public AIStyleData CreateStyle(int enemyClassID, AIStyleData deckStyle)
	{
		AIStyleData aIStyleData = new AIStyleData(new List<AICategory>
		{
			AICategory.ALL,
			AIPolicyData.ConvertClassIDToCategory(enemyClassID)
		});
		aIStyleData.MixInStyle(_commonStyle);
		if (deckStyle != null)
		{
			aIStyleData.MixInStyle(deckStyle);
		}
		return aIStyleData;
	}

	public void RegisterBasicData(AICardDataAsset asset)
	{
		_basicDic.RegisterCardData(asset);
	}

	public bool RegisterCommonData(AICardDataAsset asset)
	{
		AICardData mergeData = _basicDic.SearchCardData(asset.CardID);
		return _commonDic.RegisterCardData(asset, mergeData);
	}

	public bool RegisterAllyCommonData(AICardDataAsset asset)
	{
		AICardData mergeData = _basicDic.SearchCardData(asset.CardID);
		return _allyCommonDic.RegisterCardData(asset, mergeData);
	}

	public void RegisterDeckToDeckDic(string deckName, List<AICardDataAsset> cardDataAssetList)
	{
		AIDeckData aIDeckData = new AIDeckData();
		for (int i = 0; i < cardDataAssetList.Count; i++)
		{
			AICardDataAsset aICardDataAsset = cardDataAssetList[i];
			int cardID = aICardDataAsset.CardID;
			bool useCommon = aICardDataAsset.UseCommon;
			AICardData mergeData = ((!useCommon) ? _basicDic.SearchCardData(cardID) : _allyCommonDic.SearchCardData(cardID));
			aIDeckData.RegisterCardData(aICardDataAsset, mergeData, useCommon);
		}
		RegisterDeck(deckName, aIDeckData);
	}

	public bool RegisterDeck(string deckName, AIDeckData deck)
	{
		if (_deckDic.ContainsKey(deckName))
		{
			return false;
		}
		_deckDic.Add(deckName, deck);
		return true;
	}

	public bool RegisterEmoteSet(string emoteSetName, AIEmoteSet emoteSet)
	{
		if (emoteDic.ContainsKey(emoteSetName))
		{
			return false;
		}
		emoteDic.Add(emoteSetName, emoteSet);
		return true;
	}

	public AIDeckData SearchDeckData(string name)
	{
		if (!_deckDic.ContainsKey(name))
		{
			return null;
		}
		return _deckDic[name];
	}

	public AIStyleData SearchDeckStyle(string name)
	{
		if (!_deckStyleDic.ContainsKey(name))
		{
			return null;
		}
		return _deckStyleDic[name];
	}

	public AIEmoteSet SearchEmoteSet(string name)
	{
		if (!emoteDic.ContainsKey(name))
		{
			return null;
		}
		return emoteDic[name];
	}

	public AIDeckData GetCommonDic()
	{
		return _commonDic;
	}

	public AIDeckData GetAllyCommonDic()
	{
		return _allyCommonDic;
	}

	public void RegisterCommonStyle(List<AIPolicyDataAsset> assetList)
	{
		_commonStyle.CreateFromAsset(assetList);
	}

	public bool RegisterDeckStyle(string styleName, List<AIPolicyDataAsset> assetList)
	{
		if (_deckStyleDic.ContainsKey(styleName))
		{
			return false;
		}
		AIStyleData aIStyleData = new AIStyleData();
		aIStyleData.CreateFromAsset(assetList);
		_deckStyleDic.Add(styleName, aIStyleData);
		return true;
	}

	public static List<int> GetAIDeckCardList(string deckname)
	{
		List<int> list = new List<int>();
		foreach (AICardData value in GameMgr.GetIns().GetDataMgr().m_AIDataLibrary.SearchDeckData(deckname).CardDic.Values)
		{
			for (int i = 0; i < value.CardNum; i++)
			{
				list.Add(value.CardID);
			}
		}
		return list;
	}
}
