using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine.EventSystems;

namespace UnityEngine.UI
{
	[ExecuteInEditMode]
	public class RichtextAlphaAdapter : UIBehaviour
	{
		private Text targetText
		{
			get
			{
				if (this.m_TargetText == null)
				{
					this.m_TargetText = base.GetComponent<Text>();
				}
				return this.m_TargetText;
			}
		}

		public string inputText
		{
			get
			{
				return this.m_InputText;
			}
			set
			{
				this.m_InputText = value;
				this.UpdateAlpha();
			}
		}

		protected override void OnRectTransformDimensionsChange()
		{
			base.OnRectTransformDimensionsChange();
			if (this.targetText == null)
			{
				return;
			}
			this.UpdateAlpha();
		}

		private void UpdateAlpha()
		{
			float a = this.targetText.color.a;
			string str = this.ConvertTo16(a);
			string replacement = "${1}${3}" + str + "${5}${6}</color>";
			string text = this.inputText;
			text = RichtextAlphaAdapter.regexIndention.Replace(text, string.Empty);
			text = RichtextAlphaAdapter.regexBrTag.Replace(text, Environment.NewLine);
			text = RichtextAlphaAdapter.regexColor.Replace(text, replacement);
			text = RichtextAlphaAdapter.regexSpecialCharacter.Replace(text, new MatchEvaluator(RichtextAlphaAdapter.ReplaceSpecialCharacter));
			text = Regex.Replace(text, "</?u>", string.Empty, RegexOptions.IgnoreCase | RegexOptions.Singleline);
			this.targetText.text = text;
		}

		private string ConvertTo16(float a)
		{
			int num = (int)(a * 255f);
			string text = Convert.ToString(num, 16);
			if (num < 16)
			{
				text = "0" + text;
			}
			return text;
		}

		private static string GetSpecialCharacter(string str)
		{
			RichtextAlphaAdapter.CreateEntities();
			if (!RichtextAlphaAdapter.Entities.ContainsKey(str))
			{
				Debug.LogWarning(str + "に対応する特殊文字が見つからない");
				return string.Empty;
			}
			return RichtextAlphaAdapter.Entities[str];
		}

		private static string ReplaceSpecialCharacter(Match m)
		{
			return RichtextAlphaAdapter.GetSpecialCharacter(m.Value);
		}

		private static Regex regexColor
		{
			get
			{
				if (RichtextAlphaAdapter.m_RegexColor == null)
				{
					RichtextAlphaAdapter.CreateRegex();
				}
				return RichtextAlphaAdapter.m_RegexColor;
			}
		}

		private static Regex regexIndention
		{
			get
			{
				if (RichtextAlphaAdapter.m_RegexIndention == null)
				{
					RichtextAlphaAdapter.CreateRegex();
				}
				return RichtextAlphaAdapter.m_RegexIndention;
			}
		}

		private static Regex regexBrTag
		{
			get
			{
				if (RichtextAlphaAdapter.m_RegexBrTag == null)
				{
					RichtextAlphaAdapter.CreateRegex();
				}
				return RichtextAlphaAdapter.m_RegexBrTag;
			}
		}

		private static Regex regexSpecialCharacter
		{
			get
			{
				if (RichtextAlphaAdapter.m_RegexSpecialCharacter == null)
				{
					RichtextAlphaAdapter.CreateRegex();
				}
				return RichtextAlphaAdapter.m_RegexSpecialCharacter;
			}
		}

		private static void CreateRegex()
		{
			RichtextAlphaAdapter.m_RegexColor = new Regex(RichtextAlphaAdapter.patternColor, RegexOptions.IgnoreCase | RegexOptions.Singleline);
			RichtextAlphaAdapter.m_RegexIndention = new Regex(RichtextAlphaAdapter.patternIndention, RegexOptions.Singleline);
			RichtextAlphaAdapter.m_RegexBrTag = new Regex(RichtextAlphaAdapter.patternBrTag, RegexOptions.Singleline);
			RichtextAlphaAdapter.m_RegexSpecialCharacter = new Regex(RichtextAlphaAdapter.patternSpecialCharacter, RegexOptions.Singleline);
		}

		private static void CreateEntities()
		{
			if (RichtextAlphaAdapter.Entities != null)
			{
				return;
			}
			RichtextAlphaAdapter.Entities = new SortedDictionary<string, string>();
			RichtextAlphaAdapter.Entities.Add("&copy;", "©");
			RichtextAlphaAdapter.Entities.Add("&lt;", "<");
			RichtextAlphaAdapter.Entities.Add("&gt;", ">");
		}

		[SerializeField]
		private Text m_TargetText;

		[SerializeField]
		[TextArea(3, 10)]
		private string m_InputText = string.Empty;

		private static SortedDictionary<string, string> Entities;

		private static readonly string patternColor = "(<)(FONT\\s?)?(color='?\"?#[a-f0-9]{6})([a-f0-9]{2})?('?\"?>)(.*?)(</FONT>|</color>)";

		private static readonly string patternIndention = "\n|\r";

		private static readonly string patternBrTag = "<br>";

		private static readonly string patternSpecialCharacter = "(&[a-zA-Z]+?;)";

		private static Regex m_RegexColor;

		private static Regex m_RegexIndention;

		private static Regex m_RegexBrTag;

		private static Regex m_RegexSpecialCharacter;
	}
}
