using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEngine;
using Wizard;
using Wizard.Battle.View;

public class BattleKeywordInfoListMgr : MonoBehaviour
{
	private const string KEYWORD = "KEYWORD";

	private static readonly string[] KEYWORD_PATTERNS = new string[2] { "\\[u\\]\\[(ffcd45|524522)\\](?<KEYWORD>.*?)\\[-\\]\\[/u\\]", "\\[b\\](?<KEYWORD>.*?)\\[/b\\]" };

	private static readonly string KEYWORD_TITLE_REMOVE_PATTERN = "<<\\{me.hand_self.count\\}\\+\\d\\?\\?>>";

	private const int LABEL_KEYWORD = 0;

	private const int LABEL_DESC = 1;

	private const int LINE_SPRITE = 0;

	private const int PADDING_OBJECT = 1;

	private const int LINE_SPRITE_OFFSET = -12;

	private const int CLASS_EFFECT_TABLE_PADDING_Y = 30;

	private const int CLASS_EFFECT_FONT_SIZE = 21;

	[SerializeField]
	public UIScrollView ScrollView;

	[SerializeField]
	private NguiObjs SkillInfo;

	[SerializeField]
	public UITable _table;

	[SerializeField]
	private UIPanel _panel;

	private IList<NguiObjs> SkillInfoList = new List<NguiObjs>();

	private Action ResetTableFinishAction;

	public static IList<string> GetKeywords(CardParameter cardParameter)
	{
		return GetKeywords(cardParameter.SkillDescription + cardParameter.EvoSkillDescription);
	}

	public static IList<string> GetKeywords(string skillDescription)
	{
		List<string> list = new List<string>();
		for (int i = 0; i < KEYWORD_PATTERNS.Length; i++)
		{
			foreach (Match item in Regex.Matches(skillDescription, KEYWORD_PATTERNS[i]))
			{
				string value = item.Groups["KEYWORD"].Value;
				if (!list.Contains(value))
				{
					list.Add(value);
				}
			}
		}
		return list;
	}

	public void SetKeywordInfos(IList<string> currentWords, CardMaster.CardMasterId cardMasterId)
	{
		List<string> list = new List<string>();
		for (int i = 0; i < currentWords.Count; i++)
		{
			string keywordTitle = string.Empty;
			string skillinfo = string.Empty;
			string text = currentWords[i];
			string valueOrDefault = Data.Master.BattleKeywordReplaceDic.GetValueOrDefault(text, text);
			if (GetKeywordData(valueOrDefault, cardMasterId, out keywordTitle, out skillinfo) && !list.Contains(keywordTitle))
			{
				AddKeywordInfo(keywordTitle, skillinfo, LabelDefine.TEXT_COLOR_KEYWORD, cardMasterId);
				list.Add(keywordTitle);
			}
		}
		StartCoroutine(RepositionTable());
	}

	public void SetBuffInfos(List<int> baseCardID, List<string> buff)
	{
		string empty = string.Empty;
		for (int i = 0; i < baseCardID.Count; i++)
		{
			empty = ((CardMaster.GetInstanceForBattle().GetCardParameterFromId(baseCardID[i]).CardName != null && !(CardMaster.GetInstanceForBattle().GetCardParameterFromId(baseCardID[i]).CardName == string.Empty)) ? CardMaster.GetInstanceForBattle().GetCardParameterFromId(baseCardID[i]).CardName : Data.SystemText.Get("BattleLog_0097"));
			AddKeywordInfo(empty, buff[i], LabelDefine.TEXT_COLOR_NORMAL, CardMaster.BatttleCardMasterId);
		}
		StartCoroutine(RepositionTable());
	}

	public IEnumerator RepositionTable()
	{
		yield return null;
		_table.Reposition();
		ScrollView.verticalScrollBar.value = 0f;
		ScrollView.ResetPosition();
		if (ResetTableFinishAction != null)
		{
			ResetTableFinishAction();
		}
	}

	public void AddKeywordInfo(string keyword, string desc, Color32 keywordColor, CardMaster.CardMasterId cardMasterId, bool isClassEffect = false, Action onClickCardName = null, bool isKeyWordDisp = true)
	{
		NguiObjs nguiObjs = UnityEngine.Object.Instantiate(SkillInfo);
		keyword = Regex.Replace(keyword, KEYWORD_TITLE_REMOVE_PATTERN, "");
		if (isClassEffect)
		{
			nguiObjs.labels[0].fontSize = 21;
			nguiObjs.labels[1].fontSize = 21;
		}
		nguiObjs.transform.parent = _table.transform;
		nguiObjs.labels[0].text = keyword;
		nguiObjs.labels[0].color = keywordColor;
		nguiObjs.labels[1].SetWrapText(desc);
		nguiObjs.transform.localScale = new Vector3(1f, 1f, 1f);
		nguiObjs.gameObject.name = SkillInfoList.Count.ToString();
		nguiObjs.gameObject.SetActive(value: true);
		if (isClassEffect)
		{
			GameObject gameObject = new GameObject("paddingObject");
			gameObject.transform.SetParent(nguiObjs.labels[0].gameObject.transform);
			gameObject.transform.localPosition = Vector3.zero;
			gameObject.transform.localScale = Vector3.one;
			UISprite uISprite = gameObject.AddComponent<UISprite>();
			uISprite.alpha = 0f;
			uISprite.height = 30;
			gameObject.transform.localPosition = new Vector3(nguiObjs.labels[0].width / 2, gameObject.transform.localPosition.y);
			nguiObjs.objs.Add(gameObject);
			if (SkillInfoList.Count == 0)
			{
				gameObject.SetActive(value: false);
			}
			BoxCollider boxCollider = nguiObjs.labels[1].gameObject.AddComponent<BoxCollider>();
			boxCollider.size = new Vector3(nguiObjs.labels[1].width, nguiObjs.labels[1].height);
			boxCollider.center = new Vector3(nguiObjs.labels[1].width / 2, -nguiObjs.labels[1].height / 2);
			List<string> keyWordList = BattlePlayerView.GetKeyWordList(nguiObjs.labels[1].text);
			UIEventListener uIEventListener = UIEventListener.Get(nguiObjs.labels[1].gameObject);
			uIEventListener.onClick = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener.onClick, (UIEventListener.VoidDelegate)delegate
			{
				if (keyWordList.Count == 0)
				{
					onClickCardName();
				}
				else
				{
					BattlePlayerView.CreateClassEffectPanel(keyWordList, cardMasterId);
				}
			});
			BattlePlayerView.SetKeyWordColor(nguiObjs.labels[1].gameObject, nguiObjs.labels[1]);
			BoxCollider boxCollider2 = nguiObjs.labels[0].gameObject.AddComponent<BoxCollider>();
			boxCollider2.size = new Vector3(nguiObjs.labels[0].width, nguiObjs.labels[0].height);
			boxCollider2.center = new Vector3(nguiObjs.labels[0].width / 2, -nguiObjs.labels[0].height / 2);
			BattlePlayerView.SetLabelColorEvent(nguiObjs.labels[0]);
			UIEventListener uIEventListener2 = UIEventListener.Get(nguiObjs.labels[0].gameObject);
			uIEventListener2.onClick = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener2.onClick, (UIEventListener.VoidDelegate)delegate
			{
				onClickCardName();
			});
			if (!isKeyWordDisp)
			{
				nguiObjs.labels[0].gameObject.SetActive(value: false);
				nguiObjs.labels[1].transform.localPosition = new Vector3(nguiObjs.labels[1].transform.localPosition.x, nguiObjs.labels[0].transform.localPosition.y);
				SkillInfoList[SkillInfoList.Count - 1].objs[0].SetActive(value: false);
			}
			boxCollider.gameObject.AddComponent<UIDragScrollView>().scrollView = ScrollView;
			boxCollider2.gameObject.AddComponent<UIDragScrollView>().scrollView = ScrollView;
			UILabel uILabel = nguiObjs.labels[1];
			uILabel.topAnchor.target = null;
			uILabel.bottomAnchor.target = null;
			uILabel.leftAnchor.target = null;
			uILabel.rightAnchor.target = null;
			UISprite component = nguiObjs.objs[0].GetComponent<UISprite>();
			component.topAnchor.target = null;
			component.bottomAnchor.target = null;
			component.leftAnchor.target = null;
			component.rightAnchor.target = null;
			component.transform.localPosition = new Vector3(component.transform.localPosition.x, uILabel.transform.localPosition.y - (float)uILabel.height + -12f);
		}
		SkillInfoList.Add(nguiObjs);
	}

	public void RemoveKeyWord(int index)
	{
		bool activeSelf = SkillInfoList[index].labels[0].gameObject.activeSelf;
		UnityEngine.Object.Destroy(SkillInfoList[index].gameObject);
		if (SkillInfoList.Count > index + 1)
		{
			if (!SkillInfoList[index + 1].labels[0].gameObject.activeSelf)
			{
				Vector3 localPosition = SkillInfoList[index + 1].labels[1].transform.localPosition;
				if (activeSelf)
				{
					SkillInfoList[index + 1].labels[0].gameObject.SetActive(value: true);
				}
				SkillInfoList[index + 1].labels[1].transform.localPosition = new Vector3(localPosition.x, SkillInfoList[index + 1].labels[0].transform.localPosition.y - (float)SkillInfoList[index + 1].labels[0].height);
				SkillInfoList[index + 1].objs[0].transform.localPosition = new Vector3(SkillInfoList[index + 1].objs[0].transform.localPosition.x, -72 - SkillInfoList[index + 1].labels[1].height + -12);
			}
		}
		else if (index - 1 > 0)
		{
			bool flag = false;
			bool flag2 = false;
			if (SkillInfoList[index - 1].labels[0].gameObject.activeSelf)
			{
				flag = true;
				if (index + 1 < SkillInfoList.Count && !SkillInfoList[index + 1].labels[0].gameObject.activeSelf)
				{
					flag2 = true;
				}
			}
			if (flag && !flag2)
			{
				SkillInfoList[index - 1].objs[0].SetActive(value: true);
			}
		}
		SkillInfoList.RemoveAt(index);
		if (SkillInfoList.Count != 0)
		{
			SkillInfoList[0].objs[1].SetActive(value: false);
		}
		if (base.gameObject.activeInHierarchy)
		{
			StartCoroutine(RepositionTable());
		}
	}

	public void AddKeywordInfo(BattleCardBase card, string keyword, string desc, Color32 keywordColor)
	{
		NguiObjs nguiObjs = UnityEngine.Object.Instantiate(SkillInfo);
		nguiObjs.transform.parent = _table.transform;
		nguiObjs.labels[0].text = keyword;
		nguiObjs.labels[0].color = keywordColor;
		nguiObjs.labels[1].SetWrapText(desc);
		nguiObjs.transform.localScale = new Vector3(1f, 1f, 1f);
		nguiObjs.gameObject.name = SkillInfoList.Count.ToString();
		nguiObjs.gameObject.SetActive(value: true);
		UIRect[] componentsInChildren = nguiObjs.gameObject.GetComponentsInChildren<UIRect>();
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			componentsInChildren[i].ResetAndUpdateAnchors();
		}
		SkillInfoList.Add(nguiObjs);
	}

	public void SetScrollView(DialogBase dia)
	{
		UIPanel component = ScrollView.GetComponent<UIPanel>();
		GameObject gameObject = dia.WindowSprite.gameObject;
		GameObject gameObject2 = dia.titleLine.gameObject;
		component.topAnchor.target = gameObject2.transform;
		component.topAnchor.relative = 0f;
		component.topAnchor.absolute = -10;
		component.bottomAnchor.target = gameObject.transform;
		component.bottomAnchor.relative = 0f;
		component.bottomAnchor.absolute = 25;
		component.leftAnchor.target = gameObject.transform;
		component.leftAnchor.relative = 0f;
		component.leftAnchor.absolute = 25;
		component.rightAnchor.target = gameObject.transform;
		component.rightAnchor.relative = 1f;
		component.rightAnchor.absolute = -25;
		gameObject.GetComponent<UIDragScrollView>().scrollView = ScrollView;
		component.UpdateAnchors();
	}

	private bool GetKeywordData(string skill, CardMaster.CardMasterId cardMasterId, out string keywordTitle, out string skillinfo)
	{
		keywordTitle = string.Empty;
		skillinfo = string.Empty;
		if (Data.Master.BattleKeyWordDic.ContainsKey(skill))
		{
			string skillToLower = skill.ToLower();
			KeyValuePair<string, string> keyValuePair = Data.Master.BattleKeyWordDic.First((KeyValuePair<string, string> data) => data.Key.ToLower() == skillToLower);
			keywordTitle = keyValuePair.Key;
			skillinfo = keyValuePair.Value;
			List<int> cardIdsInDesc = GetCardIdsInDesc(skillinfo);
			if (cardIdsInDesc.Count > 0)
			{
				skillinfo = string.Empty;
				for (int num = 0; num < cardIdsInDesc.Count; num++)
				{
					CardParameter cardParameterFromId = CardMaster.GetInstance(cardMasterId).GetCardParameterFromId(cardIdsInDesc[num]);
					if (num > 0)
					{
						skillinfo += "\n";
						skillinfo += Data.SystemText.Get("Card_0198_BattleKeyWord_01", cardParameterFromId.CardName);
						skillinfo += "\n";
					}
					skillinfo += GetCardDescText(cardParameterFromId);
				}
			}
			skillinfo = skillinfo.Trim();
			return true;
		}
		return false;
	}

	public static string GetCardDescText(CardParameter param)
	{
		string empty = string.Empty;
		string clanNameByKey = GameMgr.GetIns().GetDataMgr().GetClanNameByKey((int)param.Clan);
		string cardTypeName = CardBasePrm.GetCardTypeName(param.CharType);
		empty = ((!(param.TribeName == "ALL")) ? (Data.SystemText.Get("Card_0200_BattleKeyWord_03", param.Cost.ToString(), clanNameByKey, param.TribeName, cardTypeName) + "\n") : (Data.SystemText.Get("Card_0199_BattleKeyWord_02", param.Cost.ToString(), clanNameByKey, cardTypeName) + "\n"));
		if (param.CharType == CardBasePrm.CharaType.NORMAL || param.CharType == CardBasePrm.CharaType.EVOLUTION)
		{
			if (!param.IsEvolveChoiceCard)
			{
				empty = empty + Data.SystemText.Get("Card_0201_BattleKeyWord_04", param.Atk.ToString(), param.Life.ToString()) + "\n";
				if (param.ConvertedSkillDescription != string.Empty)
				{
					empty = empty + ConvertKeywordsInText(param.ConvertedSkillDescription) + "\n";
				}
			}
			empty = empty + Data.SystemText.Get("Card_0202_BattleKeyWord_05") + "\n";
			empty = empty + Data.SystemText.Get("Card_0201_BattleKeyWord_04", param.EvoAtk.ToString(), param.EvoLife.ToString()) + "\n";
			if (param.ConvertedEvoSkillDescription != string.Empty)
			{
				empty = empty + ConvertKeywordsInText(param.ConvertedEvoSkillDescription) + "\n";
			}
		}
		else
		{
			empty = empty + ConvertKeywordsInText(param.ConvertedSkillDescription) + "\n";
		}
		return empty;
	}

	private static string ConvertKeywordsInText(string skilldisc)
	{
		List<string> list = new List<string>();
		List<string> list2 = new List<string>();
		for (int i = 0; i < KEYWORD_PATTERNS.Length; i++)
		{
			foreach (Match item2 in Regex.Matches(skilldisc, KEYWORD_PATTERNS[i]))
			{
				string item = Regex.Escape(item2.Value);
				if (list.Contains(item))
				{
					continue;
				}
				string value = item2.Groups["KEYWORD"].Value;
				if (Data.Master.BattleKeyWordDic.ContainsKey(value))
				{
					List<int> cardIdsInDesc = GetCardIdsInDesc(Data.Master.BattleKeyWordDic[value]);
					list.Add(item);
					if (cardIdsInDesc.Count > 0)
					{
						list2.Add(Data.SystemText.Get("Card_0203_BattleKeyWord_06", value));
					}
					else
					{
						list2.Add(value);
					}
				}
			}
		}
		for (int j = 0; j < list.Count; j++)
		{
			skilldisc = Regex.Replace(skilldisc, list[j], list2[j]);
		}
		return Global.ConvertToWithoutBBCode(skilldisc);
	}

	public static List<int> GetCardIdsInDesc(string desc)
	{
		string text = "KEYWORD";
		string pattern = "\\[card\\](?<" + text + ">.*?)\\[/card\\]";
		List<int> list = new List<int>();
		foreach (Match item2 in Regex.Matches(desc, pattern))
		{
			int item = int.Parse(item2.Groups[text].Value);
			if (!list.Contains(item))
			{
				list.Add(item);
			}
		}
		return list;
	}

	public List<string> GetKeyWordNameList()
	{
		List<string> list = new List<string>(SkillInfoList.Count);
		for (int i = 0; i < SkillInfoList.Count; i++)
		{
			list.Add(SkillInfoList[i].labels[0].text);
		}
		return list;
	}

	public IList<NguiObjs> GetSkillInfoList()
	{
		return SkillInfoList;
	}

	public float GetScrollViewSizeY()
	{
		return ScrollView.GetComponent<UIPanel>().GetViewSize().y;
	}

	public void SetResetTableFinishAction(Action inAction)
	{
		ResetTableFinishAction = inAction;
	}

	public void SetKeyWordFocus(float inScrollBarValue)
	{
		ScrollView.verticalScrollBar.value = inScrollBarValue;
	}

	public void SetPanelDepth(int depth)
	{
		_panel.depth = depth;
	}
}
