using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Wizard;
using Wizard.Battle.UI;

public class BuffDetailInfoUI : MonoBehaviour
{
	[SerializeField]
	private UILabel _titleLabel;

	[SerializeField]
	private UILabel _buffLabel;

	private const float TITLE_HEIGHT = 50f;

	public const string NOT_SHOW_BUFF_DETAIL_STORY_SPECIAL_BATTLE_ID = "42";

	public float Height => 50f + (float)_buffLabel.height;

	public void Initialize()
	{
		_titleLabel.text = Data.SystemText.Get("BattleLog_0268");
	}

	public void SetBuffDetailLabel(BattleCardBase cardBase)
	{
		StringBuilder stringBuilder = new StringBuilder();
		List<SkillBase> allBuffSkills = GetAllBuffSkills(cardBase, cardBase.BuffInfoList.Select((BuffInfo b) => b.SkillFrom).ToList());
		if (!cardBase.IsClass)
		{
			if (cardBase.Cost != cardBase.BaseCost)
			{
				int num = cardBase.Cost - cardBase.BaseCost;
				string text = ((num > 0) ? ("+" + num) : num.ToString());
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0273", text));
			}
			if (allBuffSkills.Any((SkillBase s) => s is Skill_powerup || s is Skill_power_down))
			{
				int currentAtkBuff = cardBase.GetCurrentAtkBuff();
				int currentLifeBuff = cardBase.GetCurrentLifeBuff();
				if (currentAtkBuff != 0 || currentLifeBuff != 0)
				{
					string retAttack = string.Empty;
					string retLife = string.Empty;
					BattleLogUtility.GetBuffValueStringFormatted(currentAtkBuff, currentLifeBuff, ref retAttack, ref retLife);
					AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0040", retAttack, retLife));
				}
			}
			bool flag = allBuffSkills.Any((SkillBase s) => s is Skill_quick && (!cardBase.IsInHand || s.OnWhenPlayStart == 0));
			bool flag2 = allBuffSkills.Any((SkillBase s) => s is Skill_rush && (!cardBase.IsInHand || s.OnWhenPlayStart == 0));
			bool flag3 = allBuffSkills.Any((SkillBase s) => s is Skill_killer && (!cardBase.IsInHand || s.OnWhenPlayStart == 0));
			bool flag4 = allBuffSkills.Any((SkillBase s) => s is Skill_drain && (!cardBase.IsInHand || s.OnWhenPlayStart == 0));
			if (ExistCopiedSkillNeedDetailText(cardBase))
			{
				IEnumerable<BuffInfo> source = cardBase.BuffInfoList.Where((BuffInfo b) => b.IsCopied);
				for (int num2 = 0; num2 < source.Count(); num2++)
				{
					if (source.ElementAt(num2).SkillFrom is SkillBaseCopy skillBaseCopy)
					{
						switch (skillBaseCopy.SkillType)
						{
						case "quick":
							flag = true;
							break;
						case "rush":
							flag2 = true;
							break;
						case "killer":
							flag3 = true;
							break;
						case "drain":
							flag4 = true;
							break;
						}
					}
				}
			}
			if (flag)
			{
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0011"));
			}
			if (flag2)
			{
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0012"));
			}
			if (flag3)
			{
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0009"));
			}
			if (flag4)
			{
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0006"));
			}
			if (allBuffSkills.Any((SkillBase s) => s is Skill_attack_count && (!cardBase.IsInHand || s.OnWhenPlayStart == 0)))
			{
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0018", cardBase.MaxAttackableCount.ToString()));
			}
			if (allBuffSkills.Any((SkillBase s) => s is Skill_ignore_guard && (!cardBase.IsInHand || s.OnWhenPlayStart == 0)))
			{
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0008"));
			}
			if (allBuffSkills.Any((SkillBase s) => s is Skill_consume_ep_modifier && (!cardBase.IsInHand || s.OnWhenPlayStart == 0)) || NeedNoConsumeEpText(cardBase))
			{
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0201"));
			}
		}
		IEnumerable<Skill_shield> source2 = from s in allBuffSkills
			where s is Skill_shield && (!cardBase.IsInHand || s.OnWhenPlayStart == 0)
			select (Skill_shield)s;
		if (source2.Any())
		{
			if (source2.Any((Skill_shield s) => s.IsAllDamageShield))
			{
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0274"));
			}
			if (source2.Any((Skill_shield s) => s.IsNextDamageShield))
			{
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0275"));
			}
			if (source2.Any((Skill_shield s) => s.IsSkillDamageShield))
			{
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0276"));
			}
			if (source2.Any((Skill_shield s) => s.IsSpellDamageShield))
			{
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0277"));
			}
		}
		List<Skill_damage_cut> source3 = (from s in allBuffSkills
			where s is Skill_damage_cut && (!cardBase.IsInHand || s.OnWhenPlayStart == 0)
			select (Skill_damage_cut)s).ToList();
		if (source3.Any())
		{
			if (source3.Any((Skill_damage_cut s) => s.IsAllDamageCut))
			{
				int num3 = -source3.Where((Skill_damage_cut s) => s.IsAllDamageCut).Sum((Skill_damage_cut s) => s.CutAmount);
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0278", num3.ToString()));
			}
			if (source3.Any((Skill_damage_cut s) => s.IsNextDamageCut))
			{
				int num4 = -source3.Where((Skill_damage_cut s) => s.IsNextDamageCut).Sum((Skill_damage_cut s) => s.CutAmount);
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0279", num4.ToString()));
			}
			if (source3.Any((Skill_damage_cut s) => s.IsSkillDamageCut))
			{
				int num5 = -source3.Where((Skill_damage_cut s) => s.IsSkillDamageCut).Sum((Skill_damage_cut s) => s.CutAmount);
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0280", num5.ToString()));
			}
			if (source3.Any((Skill_damage_cut s) => s.IsDamageClipping))
			{
				List<int> list = (from s in source3
					where s.ClippingMax != int.MaxValue
					select s.ClippingMax).ToList();
				if (source3.Any((Skill_damage_cut s) => !IsNotShowDamageCutLifeLowerLimitBuffDetail(s)))
				{
					list.AddRange(from s in source3
						where s.LifeLowerLimit != -1
						select (!(s.SkillPrm.ownerCard is UnitBattleCard)) ? (s.SkillPrm.ownerCard.SelfBattlePlayer.Class.Life - 1) : (s.SkillPrm.ownerCard.Life - 1));
				}
				if (list.Any())
				{
					int num6 = list.Min();
					AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0281", (num6 + 1).ToString(), num6.ToString()));
				}
			}
		}
		_buffLabel.text = stringBuilder.ToString();
	}

	public void SetBuffDetailLabelInReplay(List<NetworkBattleReceiver.ReplayBuffInfoLabel> buffInfoTextTypeList, BattleCardBase card)
	{
		StringBuilder stringBuilder = new StringBuilder();
		if (!card.IsClass)
		{
			if (buffInfoTextTypeList.Any((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.Cost))
			{
				int num = card.Cost - card.BaseCost;
				string text = ((num > 0) ? ("+" + num) : num.ToString());
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0273", text));
			}
			if (buffInfoTextTypeList.Any((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.StatusBuff))
			{
				string retAttack = string.Empty;
				string retLife = string.Empty;
				BattleLogUtility.GetBuffValueStringFormatted(card.GetCurrentAtkBuff(), card.GetCurrentLifeBuff(), ref retAttack, ref retLife);
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0040", retAttack, retLife));
			}
			if (buffInfoTextTypeList.Any((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.Quick))
			{
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0011"));
			}
			if (buffInfoTextTypeList.Any((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.Rush))
			{
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0012"));
			}
			if (buffInfoTextTypeList.Any((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.Killer))
			{
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0009"));
			}
			if (buffInfoTextTypeList.Any((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.Drain))
			{
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0006"));
			}
			if (buffInfoTextTypeList.Any((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.AttackCount))
			{
				int value = buffInfoTextTypeList.FirstOrDefault((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.AttackCount).Value;
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0018", value.ToString()));
			}
			if (buffInfoTextTypeList.Any((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.IgnoreGuard))
			{
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0008"));
			}
			if (buffInfoTextTypeList.Any((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.ConsumeEpModifier))
			{
				AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0201"));
			}
		}
		if (buffInfoTextTypeList.Any((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.AllDamageShield))
		{
			AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0274"));
		}
		if (buffInfoTextTypeList.Any((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.NextDamageShield))
		{
			AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0275"));
		}
		if (buffInfoTextTypeList.Any((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.SkillDamageShield))
		{
			AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0276"));
		}
		if (buffInfoTextTypeList.Any((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.SpellDamageShield))
		{
			AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0277"));
		}
		if (buffInfoTextTypeList.Any((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.AllDamageCut))
		{
			int num2 = -buffInfoTextTypeList.FirstOrDefault((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.AllDamageCut).Value;
			AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0278", num2.ToString()));
		}
		if (buffInfoTextTypeList.Any((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.NextDamageCut))
		{
			int num3 = -buffInfoTextTypeList.FirstOrDefault((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.NextDamageCut).Value;
			AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0279", num3.ToString()));
		}
		if (buffInfoTextTypeList.Any((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.SkillDamageCut))
		{
			int num4 = -buffInfoTextTypeList.FirstOrDefault((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.SkillDamageCut).Value;
			AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0280", num4.ToString()));
		}
		if (buffInfoTextTypeList.Any((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.DamageClipping))
		{
			int value2 = buffInfoTextTypeList.FirstOrDefault((NetworkBattleReceiver.ReplayBuffInfoLabel b) => b.Type == NetworkBattleReceiver.ReplayBuffInfoTextType.DamageClipping).Value;
			AppendBuffText(stringBuilder, Data.SystemText.Get("BattleLog_0281", (value2 + 1).ToString(), value2.ToString()));
		}
		_buffLabel.text = stringBuilder.ToString();
	}

	public static List<SkillBase> GetAllBuffSkills(BattleCardBase cardBase, List<SkillBase> buffSkills)
	{
		List<SkillBase> list = new List<SkillBase>();
		foreach (SkillBase buffSkill in buffSkills)
		{
			if (buffSkill is Skill_attach_skill)
			{
				IEnumerable<SkillBase> source = from b in buffSkill.GetBuffInfoContainer()
					where cardBase == b._targetCard
					select b._attachSkill;
				list.AddRange(GetAllBuffSkills(cardBase, source.Where((SkillBase s) => !cardBase.IsClass || !ExistClassSkillNeedBuffDetailText(s)).ToList()));
			}
			else
			{
				list.Add(buffSkill);
			}
		}
		return list;
	}

	public void AppendBuffText(StringBuilder buffTextsStringBuilder, string buffText)
	{
		if (buffTextsStringBuilder.Length > 0)
		{
			buffTextsStringBuilder.Append("\r\n");
		}
		buffTextsStringBuilder.Append(buffText);
	}

	public static bool NeedBuffDetailText(BattleCardBase cardBase)
	{
		if (cardBase is ClassBattleCardBase)
		{
			return cardBase.BuffInfoList.Any((BuffInfo s) => ExistClassSkillNeedBuffDetailText(s.SkillFrom));
		}
		if (!cardBase.BuffInfoList.Exists((BuffInfo b) => ExistSkillNeedBuffDetailText(cardBase, b.SkillFrom)))
		{
			return NeedNoConsumeEpText(cardBase);
		}
		return true;
	}

	public static bool IsNotShowDamageCutLifeLowerLimitBuffDetail(SkillBase skill)
	{
		bool num = skill is Skill_damage_cut { IsDamageClipping: not false } skill_damage_cut && skill_damage_cut.LifeLowerLimit != -1;
		DataMgr.SpecialBattleSetting specialBattleSettingInfo = GameMgr.GetIns().GetDataMgr().SpecialBattleSettingInfo;
		if (num && specialBattleSettingInfo != null && specialBattleSettingInfo.Id == "42")
		{
			return skill.SkillPrm.ownerCard is EnemyClassBattleCard;
		}
		return false;
	}

	private static bool ExistSkillNeedBuffDetailText(BattleCardBase cardBase, SkillBase skill)
	{
		if (cardBase.IsInHand && skill.Option.Contains("(timing:when_play)"))
		{
			return false;
		}
		if (!(skill is Skill_quick) && !(skill is Skill_ignore_guard) && !(skill is Skill_attack_count) && !(skill is Skill_shield) && !(skill is Skill_damage_cut) && !(skill is Skill_rush) && !ExistSkillCostChangeNeedDetailText(cardBase, skill) && !(skill is Skill_consume_ep_modifier) && !(skill is Skill_killer) && !(skill is Skill_drain) && !ExistSKillPowerChangeNeedDetailText(cardBase, skill) && !ExistAttachSkillNeedDetailText(cardBase, skill))
		{
			return ExistCopiedSkillNeedDetailText(cardBase);
		}
		return true;
	}

	private static bool ExistClassSkillNeedBuffDetailText(SkillBase skill)
	{
		if (!(skill is Skill_shield))
		{
			if (skill is Skill_damage_cut)
			{
				return !IsNotShowDamageCutLifeLowerLimitBuffDetail(skill);
			}
			return false;
		}
		return true;
	}

	public static bool NeedNoConsumeEpText(BattleCardBase cardBase)
	{
		if (cardBase.BuffInfoList.Any((BuffInfo b) => b.SkillFrom is Skill_consume_ep_modifier))
		{
			return true;
		}
		if (DetailPanelControl.IsNeedNoConsumeEp(cardBase))
		{
			return true;
		}
		return false;
	}

	private static bool ExistSKillPowerChangeNeedDetailText(BattleCardBase cardBase, SkillBase skill)
	{
		if (skill is Skill_powerup || skill is Skill_power_down)
		{
			int currentAtkBuff = cardBase.GetCurrentAtkBuff();
			int currentLifeBuff = cardBase.GetCurrentLifeBuff();
			if (currentAtkBuff == 0)
			{
				return currentLifeBuff != 0;
			}
			return true;
		}
		return false;
	}

	private static bool ExistAttachSkillNeedDetailText(BattleCardBase cardBase, SkillBase skill)
	{
		if (skill is Skill_attach_skill)
		{
			return skill.GetBuffInfoContainer().Any((SkillBase.BuffInfoContainer b) => cardBase == b._targetCard && ExistSkillNeedBuffDetailText(cardBase, b._attachSkill));
		}
		return false;
	}

	public static bool ExistCopiedSkillNeedDetailText(BattleCardBase cardBase)
	{
		return cardBase.BuffInfoList.Any(delegate(BuffInfo b)
		{
			if (!b.IsCopied)
			{
				return false;
			}
			return b.SkillFrom is SkillBaseCopy skillBaseCopy && skillBaseCopy.IsNeedDetail;
		});
	}

	private static bool ExistSkillCostChangeNeedDetailText(BattleCardBase cardBase, SkillBase skill)
	{
		if (skill is Skill_cost_change)
		{
			return cardBase.Cost != cardBase.BaseCost;
		}
		return false;
	}
}
