using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Wizard.Battle;
using Wizard.Battle.View.Vfx;

public class SideLogControl : MonoBehaviour
{
	private class SideLog
	{
		private List<SkillBase> _skills;

		private List<int> _skillHashCodes;

		private int _repeatCount;

		public BattleCardBase Card { get; private set; }

		public bool IsPlayer => Card.IsPlayer;

		public bool IsSelect { get; private set; }

		public SideLog(BattleCardBase card, SkillBase skill, bool isSelect)
		{
			Card = card;
			_skills = new List<SkillBase>();
			_skillHashCodes = new List<int>();
			IsSelect = isSelect;
			CheckShowSideLog(skill);
		}

		public bool CheckShowSideLog(SkillBase skill)
		{
			_skills.Add(skill);
			int num = _skills.Where((SkillBase s) => skill == s).Count();
			if (num > _repeatCount)
			{
				_repeatCount = num;
				return true;
			}
			if (skill != null && skill.SkillPrm.ownerCard.BaseParameter.BaseCardId == 900241110)
			{
				return true;
			}
			return false;
		}

		public bool CheckShowSideLogNewReplay(int skillHashCode, BattleCardBase ownerCard)
		{
			_skillHashCodes.Add(skillHashCode);
			int num = _skillHashCodes.Where((int s) => skillHashCode == s).Count();
			if (num > _repeatCount)
			{
				_repeatCount = num;
				return true;
			}
			if (ownerCard.BaseParameter.BaseCardId == 900241110)
			{
				return true;
			}
			return false;
		}

		public bool IsContain(BattleCardBase card, bool isTransformSelect = false)
		{
			if (isTransformSelect)
			{
				BattleCardBase battleCardBase = ((Card.TransformInfo.Type != BattleCardBase.TransformType.Metamorphose) ? Card.TransformInfo.OriginalCard : null);
				if (IsSelect && battleCardBase != null)
				{
					return battleCardBase.BaseParameter.BaseCardId == card.BaseParameter.BaseCardId;
				}
				return false;
			}
			return Card == card;
		}
	}

	private const int PANEL_MAX = 6;

	private const int LABEL_INDEX_CARDNAME = 0;

	private const int LABEL_INDEX_SKILLDESC = 1;

	private static readonly int DEFAULT_SPACING_Y = 12;

	private static readonly int SPACING_START_LINE = 4;

	private static readonly int SPACING_Y_MARGIN = 4;

	private IList<NguiObjs> PanelList;

	private IList<NguiObjs> PanelOnList;

	private IList<bool> PanelOnFlagList;

	private Vector3 BasePos;

	private Vector2 PanelSize;

	private List<SideLog> LogShowingList;

	private SideLog _lastSideLog;

	private void Start()
	{
		PanelList = new List<NguiObjs>();
		PanelOnList = new List<NguiObjs>();
		PanelOnFlagList = new List<bool>();
		LogShowingList = new List<SideLog>();
		for (int i = 0; i < 6; i++)
		{
			GameObject gameObject;
			if (i == 0)
			{
				gameObject = base.transform.Find("Panel").gameObject;
			}
			else
			{
				gameObject = UnityEngine.Object.Instantiate(PanelList[0].gameObject);
				if (null == gameObject)
				{
					continue;
				}
				gameObject.transform.parent = base.transform;
				gameObject.transform.localScale = Vector3.one;
			}
			PanelList.Add(gameObject.GetComponent<NguiObjs>());
			LogShowingList.Add(null);
			PanelList[i].gameObject.SetActive(value: false);
		}
		BasePos = PanelList[0].transform.localPosition;
		PanelSize = PanelList[0].sprites[0].localSize;
	}

	private void Update()
	{
		for (int i = 0; i < PanelOnList.Count; i++)
		{
			int index = PanelList.IndexOf(PanelOnList[i]);
			if (LogShowingList[index] != null)
			{
				Vector3 vector = (LogShowingList[index].IsPlayer ? ((!PanelOnFlagList[i]) ? (new Vector3(0f - PanelSize.x, (PanelSize.y + 10f) * (float)i, 0f) + BasePos) : (new Vector3(0f, (PanelSize.y + 10f) * (float)i, 0f) + BasePos)) : ((!PanelOnFlagList[i]) ? (new Vector3(PanelSize.x, (0f - (PanelSize.y + 10f)) * (float)i, 0f) + BasePos) : (new Vector3(0f, (0f - (PanelSize.y + 10f)) * (float)i, 0f) + BasePos)));
				PanelOnList[i].transform.localPosition += (vector - PanelOnList[i].transform.localPosition) * 0.5f;
			}
		}
	}

	public void SetWrapTextForSideLog(UILabel label, string s)
	{
		label.spacingY = DEFAULT_SPACING_Y;
		label.SetWrapText(s);
		int num = label.text.Length - label.text.Replace("\n", "").Length + 1;
		if (num >= SPACING_START_LINE)
		{
			label.spacingY = Math.Max(DEFAULT_SPACING_Y - (num - SPACING_START_LINE + 1) * SPACING_Y_MARGIN, 0);
			label.SetWrapText(s);
		}
	}

	public bool IsOnSummonOrReturnTimingSkill(SkillBase skill)
	{
		if (skill == null)
		{
			return false;
		}
		if (skill.IsWhenPlaySkill || skill.OnWhenSummonStart != 0 || skill.OnWhenReturnStart != 0)
		{
			return true;
		}
		if (skill.OnWhenChangeInPlay != 0 || skill.OnWhenChangeInPlayImmediate != 0 || skill.OnWhenChangeClassLifeInplay != 0 || skill.OnWhenChangePPTotal != 0 || skill.OnWhenAddToHand != 0)
		{
			return true;
		}
		return false;
	}

	public VfxBase ClearLastShowLogCard()
	{
		return InstantVfx.Create(delegate
		{
			_lastSideLog = null;
		});
	}

	public bool ShowLog(BattleCardBase card, SkillBase skill, string skillDesc, string cardName, bool isEvolSelect, bool isSelect, NetworkBattleReceiver.SideLogSkillInfo newReplaySkillInfo)
	{
		NguiObjs nguiObjs = null;
		int index = 0;
		SideLog sideLog = null;
		if (!isSelect)
		{
			if (LogShowingList.Any((SideLog l) => l?.IsContain(card) ?? false) && !isEvolSelect)
			{
				SideLog sideLog2 = LogShowingList.Where((SideLog l) => l?.IsContain(card) ?? false).First();
				if (!((newReplaySkillInfo != null) ? sideLog2.CheckShowSideLogNewReplay(newReplaySkillInfo.SkillHashCode, card) : sideLog2.CheckShowSideLog(skill)))
				{
					return false;
				}
				sideLog = sideLog2;
			}
			if (IsOnSummonOrReturnTimingSkill(skill) || (newReplaySkillInfo != null && newReplaySkillInfo.IsOnSummonSkill))
			{
				bool flag = newReplaySkillInfo?.IsInvoked ?? skill.IsInvoked;
				if (_lastSideLog != null && _lastSideLog.Card.EquelsID(card) && _lastSideLog.Card.CardId == card.CardId && !_lastSideLog.IsSelect && !flag)
				{
					return false;
				}
			}
		}
		for (int num = 0; num < PanelList.Count; num++)
		{
			if (!PanelList[num].gameObject.activeSelf)
			{
				nguiObjs = PanelList[num];
				index = num;
				break;
			}
		}
		if (nguiObjs == null)
		{
			nguiObjs = PanelOnList[PanelOnList.Count - 1];
			RemoveLog(PanelOnList.Count - 1);
			for (int num2 = 0; num2 < PanelList.Count; num2++)
			{
				if (nguiObjs == PanelList[num2])
				{
					index = num2;
					break;
				}
			}
		}
		nguiObjs.labels[0].text = cardName;
		SetWrapTextForSideLog(nguiObjs.labels[1], skillDesc);
		if (card.IsPlayer)
		{
			nguiObjs.transform.localPosition = new Vector3(0f - PanelSize.x, 0f, 0f) + BasePos;
		}
		else
		{
			nguiObjs.transform.localPosition = new Vector3(PanelSize.x, 0f, 0f) + BasePos;
		}
		if (sideLog == null)
		{
			sideLog = new SideLog(card, skill, isSelect);
			if (newReplaySkillInfo != null)
			{
				sideLog.CheckShowSideLogNewReplay(newReplaySkillInfo.SkillHashCode, card);
			}
		}
		_lastSideLog = sideLog;
		LogShowingList[index] = sideLog;
		nguiObjs.gameObject.SetActive(value: true);
		PanelOnList.Insert(0, nguiObjs);
		PanelOnFlagList.Insert(0, item: true);
		return true;
	}

	public void HideLog(BattleCardBase card, bool isTransformSelect)
	{
		for (int num = PanelOnList.Count - 1; num >= 0; num--)
		{
			int index = PanelList.IndexOf(PanelOnList[num]);
			if (LogShowingList[index] != null && LogShowingList[index].IsContain(card, isTransformSelect))
			{
				PanelOnFlagList[num] = false;
				break;
			}
		}
	}

	public void RemoveLog(BattleCardBase card, bool isTransformSelect)
	{
		for (int num = PanelOnList.Count - 1; num >= 0; num--)
		{
			int index = PanelList.IndexOf(PanelOnList[num]);
			if (LogShowingList[index] != null && LogShowingList[index].IsContain(card, isTransformSelect))
			{
				PanelOnList[num].gameObject.SetActive(value: false);
				PanelOnList.RemoveAt(num);
				PanelOnFlagList.RemoveAt(num);
				LogShowingList[index] = null;
				break;
			}
		}
	}

	public void RemoveLog(int idx)
	{
		if (PanelOnList.Count >= 1)
		{
			int index = PanelList.IndexOf(PanelOnList[idx]);
			PanelOnList[idx].gameObject.SetActive(value: false);
			PanelOnList.RemoveAt(idx);
			PanelOnFlagList.RemoveAt(idx);
			LogShowingList[index] = null;
		}
	}

	public void RemoveAllLog()
	{
		if (PanelOnList.Count >= 1)
		{
			for (int num = PanelOnList.Count - 1; num >= 0; num--)
			{
				int index = PanelList.IndexOf(PanelOnList[num]);
				PanelOnList[num].gameObject.SetActive(value: false);
				PanelOnList.RemoveAt(num);
				PanelOnFlagList.RemoveAt(num);
				LogShowingList[index] = null;
			}
		}
	}
}
