using System.Collections.Generic;
using LitJson;

namespace Wizard;

public class MyRotationInfo
{
	public class MyRotationBonus
	{
		public string AbilityId { get; }

		public string[] AttachAbilities { get; }

		public int AddStartPp { get; }

		public int AddStartLife { get; }

		public int IncreaseAddPptotalTurn { get; }

		public int IncreaseAddPptotalAmount { get; }

		public string AbilityDesc { get; }

		public bool IsEpRecoverySkill { get; }

		public string IconName { get; }

		public string DarkIconName { get; }

		private string OriginalAbilityText { get; }

		public MyRotationBonus(string abilityId, string abilityText, int addStartPp, int addStartLife, int increaseAddPptotalTurn, int increaseAddPptotalAmount, string abilityDesc)
		{
			AbilityId = abilityId;
			OriginalAbilityText = abilityText;
			AttachAbilities = abilityText.Split(',');
			AddStartPp = addStartPp;
			AddStartLife = addStartLife;
			IncreaseAddPptotalTurn = increaseAddPptotalTurn;
			IncreaseAddPptotalAmount = increaseAddPptotalAmount;
			AbilityDesc = abilityDesc;
			for (int i = 0; i < AttachAbilities.Length; i++)
			{
				if (AttachAbilities[i].Contains("possess_ep_modifier"))
				{
					IsEpRecoverySkill = true;
				}
			}
			IconName = "my_rotation_ability_icon_" + abilityId;
			DarkIconName = "my_rotation_ability_icon_dark_" + abilityId;
		}

		public static bool IsEqual(MyRotationBonus left, MyRotationBonus right)
		{
			if (left.AbilityId == right.AbilityId && left.OriginalAbilityText == right.OriginalAbilityText && left.AddStartPp == right.AddStartPp && left.AddStartLife == right.AddStartLife && left.IncreaseAddPptotalTurn == right.IncreaseAddPptotalTurn)
			{
				return left.IncreaseAddPptotalAmount == right.IncreaseAddPptotalAmount;
			}
			return false;
		}
	}

	private const int NEMESIS_START_PACK_NUMBER = 7;

	private List<UnlimitedRestrictedCard> _restrictedCardData = new List<UnlimitedRestrictedCard>();

	private List<int> _rePrintCardList = new List<int>();

	public string Id { get; }

	public List<string> CardPadkIdList { get; }

	public string PackSelectText { get; private set; }

	public string FirstPackId { get; private set; }

	public string LastPackId { get; private set; }

	public string LastPackText { get; private set; }

	public List<string> AbilityIdList { get; private set; }

	public List<MyRotationBonus> Abilities { get; private set; }

	public Dictionary<int, MyRotationRePrintInfo> RePrintDictionary { get; private set; } = new Dictionary<int, MyRotationRePrintInfo>();

	public bool IsEnableNemesis { get; private set; }

	public static bool IsEqualAbility(MyRotationInfo left, MyRotationInfo right)
	{
		if (left.Abilities.Count != right.Abilities.Count)
		{
			return false;
		}
		for (int i = 0; i < left.Abilities.Count; i++)
		{
			if (!MyRotationBonus.IsEqual(left.Abilities[i], right.Abilities[i]))
			{
				return false;
			}
		}
		return true;
	}

	public MyRotationInfo(JsonData json)
	{
		Id = json["rotation_id"].ToString();
		CardPadkIdList = new List<string>(json["card_set_ids"].ToString().Split('|'));
		AbilityIdList = new List<string>(json["abilities"].ToString().Split('|'));
	}

	public void ParseRestrictJson(JsonData rotationRoot)
	{
		List<UnlimitedRestrictedCard> list = new List<UnlimitedRestrictedCard>();
		foreach (string key in rotationRoot.Keys)
		{
			if (int.TryParse(key, out var result))
			{
				list.Add(new UnlimitedRestrictedCard(result, rotationRoot[key].ToInt()));
			}
		}
		_restrictedCardData = list;
	}

	public bool IsRePrintCard(int baseCardId)
	{
		return _rePrintCardList.Contains(baseCardId);
	}

	public void ParseRePrintJson(JsonData rotationRoot)
	{
		List<int> list = new List<int>();
		if (rotationRoot.Count == 0)
		{
			_rePrintCardList = list;
			return;
		}
		foreach (string key in rotationRoot.Keys)
		{
			list.Add(rotationRoot[key].ToInt());
		}
		_rePrintCardList = list;
	}

	public void ParseAbilitiesJson(JsonData abilities)
	{
		List<MyRotationBonus> list = new List<MyRotationBonus>();
		foreach (JsonData value in abilities.Values)
		{
			if (AbilityIdList.Contains(value["ability_id"].ToString()))
			{
				MyRotationBonus item = new MyRotationBonus(value["ability_id"].ToString(), value["ability"].ToString(), value["add_start_pp"].ToInt(), value["add_start_life"].ToInt(), value["increase_add_pptotal_turn"].ToInt(), value["increase_add_pptotal_amount"].ToInt(), value["ability_desc"].ToString());
				list.Add(item);
			}
		}
		Abilities = list;
	}

	public void InitializeText()
	{
		string text = null;
		foreach (string cardPadkId in CardPadkIdList)
		{
			if (cardPadkId != 10000.ToString())
			{
				text = cardPadkId;
				break;
			}
		}
		string text2 = CardPadkIdList[CardPadkIdList.Count - 1];
		string shortName = Data.Master.CardSetNameMgr.Get(text).ShortName;
		string shortName2 = Data.Master.CardSetNameMgr.Get(text2).ShortName;
		FirstPackId = text;
		LastPackId = text2;
		int num = int.Parse(text) - 10000;
		int num2 = int.Parse(text2) - 10000;
		PackSelectText = Data.SystemText.Get("Card_0296", shortName, shortName2, num.ToString(), num2.ToString());
		LastPackText = shortName2;
		IsEnableNemesis = num2 >= 7;
	}

	public void InitializeRePrintCard(List<CardParameter> allCardParameter)
	{
		foreach (int rePrintCard in _rePrintCardList)
		{
			RePrintDictionary.Add(rePrintCard, new MyRotationRePrintInfo(rePrintCard, allCardParameter));
		}
	}

	public bool IsRePrintCardAvailablePack(int baseCardId, string cardPackId)
	{
		if (!RePrintDictionary.TryGetValue(baseCardId, out var value))
		{
			return false;
		}
		return value.IsRePrintCardAvailablePack(cardPackId);
	}

	public int GetSameCardCount(int baseCardId)
	{
		foreach (UnlimitedRestrictedCard restrictedCardDatum in _restrictedCardData)
		{
			if (baseCardId == restrictedCardDatum.BaseCardId)
			{
				return restrictedCardDatum.Count;
			}
		}
		return FormatBehaviorManager.GetDefaultBehaviour(Format.MyRotation).DeckSameKindCardNumMax;
	}

	public bool IsNotUseCard(int baseCardId)
	{
		return GetSameCardCount(baseCardId) == 0;
	}

	public bool IsEnableCardPackId(string cardPackId)
	{
		foreach (string cardPadkId in CardPadkIdList)
		{
			if (cardPadkId == cardPackId)
			{
				return true;
			}
		}
		return false;
	}
}
