using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using HyperlinkText;

namespace UnityEngine.UI
{
	[ExecuteInEditMode]
	[AddComponentMenu("UI/Effects/Tag Support Text", 64)]
	public class TagSupportText : BaseMeshEffect
	{
		protected TagSupportText()
		{
		}

		private Text text
		{
			get
			{
				Text result;
				if ((result = this.m_CachedText) == null)
				{
					result = (this.m_CachedText = base.GetComponent<Text>());
				}
				return result;
			}
		}

		public override void ModifyMesh(VertexHelper vertexHelper)
		{
			if (!this.IsActive())
			{
				return;
			}
			List<UIVertex> list = TagSupportText._verticesPool.Get();
			vertexHelper.GetUIVertexStream(list);
			this.Modify(ref list);
			vertexHelper.Clear();
			vertexHelper.AddUIVertexTriangleStream(list);
			TagSupportText._verticesPool.Release(list);
		}

		public void Modify(ref List<UIVertex> verts)
		{
			if (!this.IsActive())
			{
				return;
			}
			if (this.text == null)
			{
				Debug.LogWarning("TagSupportText：Text コンポーネントが見つかりません");
				return;
			}
			string[] array = this.text.text.Split(new char[]
			{
				'\n'
			});
			switch (this.text.alignment)
			{
			}
			float num = (float)this.text.fontSize / 100f;
			int num2 = 0;
			for (int i = 0; i < array.Length; i++)
			{
				float num3 = 0f;
				string text = array[i];
				int length = text.Length;
				IEnumerator regexMatchedTagCollection = this.GetRegexMatchedTagCollection(text, out length);
				Match match = null;
				if (regexMatchedTagCollection.MoveNext())
				{
					match = (Match)regexMatchedTagCollection.Current;
				}
				int j = 0;
				int num4 = 0;
				while (j < text.Length)
				{
					int num5 = num2 * 6;
					int num6 = num2 * 6 + 1;
					int num7 = num2 * 6 + 2;
					int num8 = num2 * 6 + 3;
					int num9 = num2 * 6 + 4;
					int num10 = num2 * 6 + 5;
					if (num10 > verts.Count - 1)
					{
						return;
					}
					UIVertex value = verts[num5];
					UIVertex value2 = verts[num6];
					UIVertex value3 = verts[num7];
					UIVertex value4 = verts[num8];
					UIVertex value5 = verts[num9];
					UIVertex value6 = verts[num10];
					if (match != null && match.Index == j)
					{
						string value7 = match.Value;
						TextGenerationSettings generationSettings = this.text.GetGenerationSettings(this.text.rectTransform.rect.size);
						float preferredWidth = this.text.cachedTextGenerator.GetPreferredWidth(value7, generationSettings);
						num3 += -preferredWidth;
						for (int k = 0; k < value7.Length; k++)
						{
							value = verts[num5 + k * 6];
							value2 = verts[num6 + k * 6];
							value3 = verts[num7 + k * 6];
							value4 = verts[num8 + k * 6];
							value5 = verts[num9 + k * 6];
							value6 = verts[num10 + k * 6];
							value.position = Vector3.zero;
							value2.position = Vector3.zero;
							value3.position = Vector3.zero;
							value4.position = Vector3.zero;
							value5.position = Vector3.zero;
							value6.position = Vector3.zero;
							verts[num5 + k * 6] = value;
							verts[num6 + k * 6] = value2;
							verts[num7 + k * 6] = value3;
							verts[num8 + k * 6] = value4;
							verts[num9 + k * 6] = value5;
							verts[num10 + k * 6] = value6;
						}
						num4--;
						num2 += value7.Length;
						j += value7.Length - 1;
						match = null;
						if (regexMatchedTagCollection.MoveNext())
						{
							match = (Match)regexMatchedTagCollection.Current;
						}
					}
					else
					{
						Vector3 b = Vector3.right * num3;
						value.position += b;
						value2.position += b;
						value3.position += b;
						value4.position += b;
						value5.position += b;
						value6.position += b;
						verts[num5] = value;
						verts[num6] = value2;
						verts[num7] = value3;
						verts[num8] = value4;
						verts[num9] = value5;
						verts[num10] = value6;
						num2++;
					}
					j++;
					num4++;
				}
				num2++;
			}
		}

		private IEnumerator GetRegexMatchedTagCollection(string line, out int lineLengthWithoutTags)
		{
			MatchCollection matchCollection = TagSupportText.REG_SUPPORTED_TAG.Matches(line);
			lineLengthWithoutTags = 0;
			int num = 0;
			if (matchCollection.Count > 0)
			{
				IEnumerator enumerator = matchCollection.GetEnumerator();
				try
				{
					while (enumerator.MoveNext())
					{
						object obj = enumerator.Current;
						Match match = (Match)obj;
						num += match.Length;
					}
				}
				finally
				{
					IDisposable disposable;
					if ((disposable = (enumerator as IDisposable)) != null)
					{
						disposable.Dispose();
					}
				}
			}
			lineLengthWithoutTags = line.Length - num;
			return matchCollection.GetEnumerator();
		}

		private static string SupportedTagRegexPatterns = "(<a href\\s*=\\s*.*?>)|(</a>)|(<u>)|(</u>)";

		private static Regex REG_SUPPORTED_TAG = new Regex(TagSupportText.SupportedTagRegexPatterns, RegexOptions.CultureInvariant);

		private static readonly ObjectPool<List<UIVertex>> _verticesPool = new ObjectPool<List<UIVertex>>(null, delegate(List<UIVertex> l)
		{
			l.Clear();
		});

		private Text m_CachedText;
	}
}
