using System.Collections.Generic;

namespace Wizard;

public class EvolvedResidentTagCollection : TagCollection
{
	private readonly AIPlayTagType[] _managedTagTypes = new AIPlayTagType[3]
	{
		AIPlayTagType.EvolvedAttackableCount,
		AIPlayTagType.EvolvedAttackable,
		AIPlayTagType.EvolvedSkill
	};

	private List<AIPlayTag> _evolvedAttackableCountList;

	protected override AIPlayTagType[] MANAGED_TAG_TYPES => _managedTagTypes;

	public EvolvedResidentTagCollection()
		: base(TagCollectionType.EvolvedResident)
	{
		_evolvedAttackableCountList = null;
	}

	private EvolvedResidentTagCollection(EvolvedResidentTagCollection param)
		: base(param)
	{
		_evolvedAttackableCountList = param.CreateEvolvedAttackableCountListClone();
	}

	public override TagCollection Clone()
	{
		return new EvolvedResidentTagCollection(this);
	}

	public List<AIPlayTag> CreateEvolvedAttackableCountListClone()
	{
		return AIPlayTagInitializingUtility.CloneTagList(_evolvedAttackableCountList);
	}

	public void Execute(AIVirtualCard tagOwner, AISituationInfo situation)
	{
		if (!tagOwner.IsEvolution || !base.HasTag)
		{
			return;
		}
		AIVirtualField selfField = tagOwner.SelfField;
		List<int> bestPlayPtn = selfField.BestPlayPtn;
		List<int> conditionPassedIndexList = GetConditionPassedIndexList(tagOwner, selfField, bestPlayPtn, situation);
		if (conditionPassedIndexList != null && conditionPassedIndexList.Count > 0)
		{
			for (int i = 0; i < conditionPassedIndexList.Count; i++)
			{
				base.TagList[conditionPassedIndexList[i]].ArgumentExpressions.Execute(tagOwner, selfField, bestPlayPtn, situation);
			}
		}
	}

	public int GetMaxEvolvedAttackableCount(AIVirtualCard owner, AIVirtualField field, List<int> playPtn, AISituationInfo situation)
	{
		if (_evolvedAttackableCountList == null || _evolvedAttackableCountList.Count <= 0)
		{
			return owner.AttackableCount;
		}
		int num = int.MinValue;
		for (int i = 0; i < _evolvedAttackableCountList.Count; i++)
		{
			AIPlayTag aIPlayTag = _evolvedAttackableCountList[i];
			if (aIPlayTag.CheckCondition(owner, playPtn, field, situation))
			{
				int attackableCount = (aIPlayTag.ArgumentExpressions as AIEvolvedAttackableCount).GetAttackableCount(owner, playPtn, field, situation);
				if (attackableCount > num)
				{
					num = attackableCount;
				}
			}
		}
		if (num >= 0)
		{
			return num;
		}
		return owner.AttackableCount;
	}

	public override void AddTag(AIPlayTag tag)
	{
		base.AddTag(tag);
		if (tag.Type == AIPlayTagType.EvolvedAttackableCount)
		{
			_evolvedAttackableCountList = AIParamQuery.AddElementToList(tag, _evolvedAttackableCountList);
		}
	}

	public override void Clear()
	{
		base.Clear();
		if (_evolvedAttackableCountList != null)
		{
			_evolvedAttackableCountList.Clear();
			_evolvedAttackableCountList = null;
		}
	}

	protected override void RemoveTagFromManagedTagList(AIPlayTag tag)
	{
		RemoveTagFromList(_evolvedAttackableCountList, tag);
	}
}
