using System;
using System.Collections.Generic;
using System.Linq;
using Cute;
using LitJson;
using Wizard.Battle.Recovery;

namespace Wizard;

public class CardMaster
{
	public class UpdateInfo
	{
		public bool NeedUpdateLocalCardMaster { get; private set; }

		public string NewCardMasterHash { get; private set; }

		public UpdateInfo(JsonData data)
		{
			if (data.Keys.Contains("card_master_hash"))
			{
				NeedUpdateLocalCardMaster = true;
				NewCardMasterHash = data["card_master_hash"].ToString();
			}
			else
			{
				NeedUpdateLocalCardMaster = false;
				NewCardMasterHash = string.Empty;
			}
		}
	}

	public enum CardMasterId
	{
		Default = 1,
		NextCardMaster
	}

	private const int CARD_ID_CLASS_CHECK = 100;

	private static Dictionary<CardMasterId, CardMaster> _dictCardMaster = null;

	private readonly IDictionary<int, CardParameter> m_cardParameters;

	private readonly IDictionary<string, int> _hashIdCardId;

	private CardParameter _classCardParam;

	public static CardMasterId BatttleCardMasterId { get; private set; } = CardMasterId.Default;

	public static CardMaster GetInstance(CardMasterId cardMasterId)
	{
		if (_dictCardMaster == null)
		{
			return null;
		}
		CardMaster value = null;
		_dictCardMaster.TryGetValue(cardMasterId, out value);
		return value;
	}

	public static CardMaster GetInstanceForBattle()
	{
		return GetInstance(BatttleCardMasterId);
	}

	public static void DeleteAllInstance()
	{
		_dictCardMaster = null;
		BatttleCardMasterId = CardMasterId.Default;
	}

	private CardMaster(List<CardCSVData> cardList)
	{
		m_cardParameters = cardList.ToDictionary((CardCSVData entry) => int.Parse(entry.card_id), (CardCSVData entry) => new CardParameter(entry));
		if (!IsUseLocalCardMaster())
		{
			_hashIdCardId = cardList.ToDictionary((CardCSVData entry) => entry.CardHashId, (CardCSVData entry) => int.Parse(entry.card_id));
		}
		_classCardParam = new CardParameter();
	}

	public static void InitializeCardMaster(UpdateInfo cardMasterUpdateInfo, Action onSuccess)
	{
		Dictionary<CardMasterId, string> dictCardMasterCSV = null;
		if (cardMasterUpdateInfo.NeedUpdateLocalCardMaster)
		{
			UIManager.GetInstance().createInSceneCenterLoading(notBlack: false, notCollider: false, force: false, Data.SystemText.Get("System_0053"));
			GetCardMasterTask task = new GetCardMasterTask(cardMasterUpdateInfo.NewCardMasterHash);
			UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
			{
				dictCardMasterCSV = ParseJsonToCardMasterCSV(task.CardMasterJsonData);
				SaveLocalCardMasterCSV(dictCardMasterCSV, cardMasterUpdateInfo.NewCardMasterHash);
				CreateAllCardMasterByCSV(dictCardMasterCSV);
				onSuccess.Call();
			}, BaseTask.OnRequestFailed, BaseTask.OnFailedErrorCode, encrypt: true, useJson: false, showLoadingIcon: false));
			ClearSaveData();
		}
		else if (CardMasterLocalFileUtility.ReadLocalCardMasterCSV(out dictCardMasterCSV))
		{
			CreateAllCardMasterByCSV(dictCardMasterCSV);
			onSuccess.Call();
		}
	}

	private static void ClearSaveData()
	{
		PlayerPrefsWrapper.SetBool(PlayerPrefsWrapper.DECK_INTRO_IS_MYROTATION_COPY_EQUAL_PERIOD, flag: false);
		PlayerPrefsWrapper.SetBool(PlayerPrefsWrapper.DECK_INTRO_IS_MYROTATION_COPY_NOT_EQUAL_PERIOD, flag: true);
	}

	private static void SaveLocalCardMasterCSV(Dictionary<CardMasterId, string> dictCardMasterCSV, string cardMasterHash)
	{
		CardMasterLocalFileUtility.WriteAllCardMasterLocalFile(dictCardMasterCSV, cardMasterHash);
		RecoveryRecordManagerBase.DeleteRecoveryFile();
	}

	private static void CreateAllCardMasterByCSV(Dictionary<CardMasterId, string> dictCardMasterCSV)
	{
		if (_dictCardMaster == null)
		{
			_dictCardMaster = new Dictionary<CardMasterId, CardMaster>();
		}
		foreach (KeyValuePair<CardMasterId, string> item in dictCardMasterCSV)
		{
			CardMaster cardMaster = CreateCardMaster(item.Key, item.Value);
			if (cardMaster != null)
			{
				_dictCardMaster[item.Key] = cardMaster;
			}
		}
	}

	private static Dictionary<CardMasterId, string> ParseJsonToCardMasterCSV(JsonData cardMaster)
	{
		Dictionary<CardMasterId, string> dictionary = new Dictionary<CardMasterId, string>();
		foreach (CardMasterId value in Enum.GetValues(typeof(CardMasterId)))
		{
			int num = (int)value;
			string text = num.ToString();
			if (cardMaster.Keys.Contains(text))
			{
				string text2 = cardMaster[text].ToString();
				if (text2 != "")
				{
					dictionary[value] = text2;
				}
			}
		}
		return dictionary;
	}

	private static CardMaster CreateCardMaster(CardMasterId cardMasterId, string cardMasterCSV)
	{
		if (cardMasterCSV == "")
		{
			return null;
		}
		List<string[]> list = Utility.ConvertCSV_Array(cardMasterCSV, removeTitle: false);
		int count = list.Count;
		List<CardCSVData> list2 = new List<CardCSVData>(count);
		Dictionary<string, CardCSVData> dictionary = new Dictionary<string, CardCSVData>(count);
		int num = 0;
		foreach (string[] item in list)
		{
			CardCSVData cardCSVData = new CardCSVData(item, num);
			list2.Add(cardCSVData);
			dictionary.Add(cardCSVData.card_id, cardCSVData);
			num++;
		}
		foreach (CardCSVData item2 in list2)
		{
			item2.CorrectPremiumResourceId(dictionary);
		}
		return new CardMaster(list2);
	}

	public static bool IsClass(int cardId)
	{
		return cardId < 100;
	}

	public static bool IsMutationCardCheck(int baseCardID)
	{
		if (baseCardID > 800000000)
		{
			return baseCardID % 800000000 < 10000000;
		}
		return false;
	}

	public static bool IsChoiceBraveCardCheck(int baseCardId)
	{
		return baseCardId / 1000000 == 930;
	}

	public IEnumerable<CardParameter> GetAllParameters()
	{
		return m_cardParameters.Values;
	}

	public List<int> GetAllCardIds()
	{
		return m_cardParameters.Keys.ToList();
	}

	public CardParameter GetCardParameterFromId(int cardId)
	{
		if (IsClass(cardId))
		{
			return _classCardParam;
		}
		m_cardParameters.TryGetValue(cardId, out var value);
		return value;
	}

	public CardParameter GetCardParamFromCardHashId(string cardHashId)
	{
		_hashIdCardId.TryGetValue(cardHashId, out var value);
		return GetCardParameterFromId(value);
	}

	public bool CardExists(int cardId)
	{
		return m_cardParameters.Keys.Contains(cardId);
	}

	public List<int> GetSameCardListByBaseCardId(int baseCardId)
	{
		List<int> list = new List<int>();
		foreach (KeyValuePair<int, CardParameter> cardParameter in m_cardParameters)
		{
			CardParameter value = cardParameter.Value;
			if (value.BaseCardId == baseCardId)
			{
				list.Add(value.CardId);
			}
		}
		return list;
	}

	public void RegisterCardParameter(int cardId, CardParameter cardParam)
	{
		m_cardParameters.Add(cardId, cardParam);
	}

	public void UnregisterCardParameter(int cardId)
	{
		m_cardParameters.Remove(cardId);
	}

	public static void SetBattleCardMasterId(CardMasterId cardMasterId)
	{
		BatttleCardMasterId = cardMasterId;
	}

	public static void SetBattleCardMasterId(int cardMasterId)
	{
		if (Enum.IsDefined(typeof(CardMasterId), cardMasterId))
		{
			SetBattleCardMasterId((CardMasterId)cardMasterId);
		}
	}

	public static bool IsUseLocalCardMaster()
	{
		return false;
	}
}
