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

namespace UnityEngine.UI
{
	[ExecuteInEditMode]
	public class HyphenationJP : UIBehaviour
	{
		private string text
		{
			get
			{
				return this._Text.text;
			}
		}

		private RectTransform _RectTransform
		{
			get
			{
				if (this._rectTransform == null)
				{
					this._rectTransform = base.GetComponent<RectTransform>();
				}
				return this._rectTransform;
			}
		}

		private Text _Text
		{
			get
			{
				if (this._text == null)
				{
					this._text = base.GetComponent<Text>();
				}
				return this._text;
			}
		}

		protected override void OnRectTransformDimensionsChange()
		{
			base.OnRectTransformDimensionsChange();
			if (this._Text == null)
			{
				return;
			}
			this.UpdateText(this.text);
		}

		public void UpdateText()
		{
			this.UpdateText(this.text);
		}

		private void UpdateText(string str)
		{
			this._Text.text = this.GetFormatedText(this._Text, str);
		}

		private float GetTextWidth(Text textComp, string message)
		{
			if (this._Text.supportRichText)
			{
				message = HyphenationJP.REG_RICH_TEXT.Replace(message, string.Empty);
			}
			textComp.text = message;
			return textComp.preferredWidth;
		}

		private string GetFormatedText(Text textComp, string msg)
		{
			if (!base.enabled)
			{
				return msg;
			}
			if (string.IsNullOrEmpty(msg))
			{
				return string.Empty;
			}
			float width = this._RectTransform.rect.width;
			float textWidth = this.GetTextWidth(textComp, " ");
			textComp.horizontalOverflow = HorizontalWrapMode.Overflow;
			StringBuilder stringBuilder = new StringBuilder();
			float num = 0f;
			foreach (string text in this.GetWordList(msg))
			{
				num += this.GetTextWidth(textComp, text);
				if (HyphenationJP.IsNewLine(text))
				{
					num = 0f;
				}
				else
				{
					if (text == " ")
					{
						num += textWidth;
					}
					if (num > width)
					{
						stringBuilder.Append(Environment.NewLine);
						num = this.GetTextWidth(textComp, text);
					}
				}
				stringBuilder.Append(text);
			}
			return stringBuilder.ToString();
		}

		private List<string> GetWordList(string tmpText)
		{
			List<string> list = new List<string>();
			StringBuilder stringBuilder = new StringBuilder();
			char c = '\0';
			bool flag = false;
			for (int i = 0; i < tmpText.Length; i++)
			{
				char c2 = tmpText[i];
				char c3 = (i >= tmpText.Length - 1) ? c : tmpText[i + 1];
				char c4 = (i <= 0) ? c : tmpText[i - 1];
				stringBuilder.Append(c2);
				if (c2 == '<')
				{
					flag = true;
				}
				else if (c2 == '>')
				{
					flag = false;
				}
				if (!flag && (HyphenationJP.IsNewLine(c2.ToString()) || (HyphenationJP.IsLatin(c2) && HyphenationJP.IsLatin(c4) && HyphenationJP.IsLatin(c2) && !HyphenationJP.IsLatin(c4)) || (!HyphenationJP.IsLatin(c2) && HyphenationJP.CHECK_HYP_BACK(c4)) || (!HyphenationJP.IsLatin(c3) && !HyphenationJP.CHECK_HYP_FRONT(c3) && !HyphenationJP.CHECK_HYP_BACK(c2)) || i == tmpText.Length - 1))
				{
					list.Add(stringBuilder.ToString());
					stringBuilder = new StringBuilder();
				}
			}
			return list;
		}

		public float textWidth
		{
			get
			{
				return this._RectTransform.rect.width;
			}
			set
			{
				this._RectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, value);
			}
		}

		public int fontSize
		{
			get
			{
				return this._Text.fontSize;
			}
			set
			{
				this._Text.fontSize = value;
			}
		}

		private static bool CHECK_HYP_FRONT(char c)
		{
			return HyphenationJP.HYP_FRONT_.Contains(c);
		}

		private static bool CHECK_HYP_BACK(char c)
		{
			return HyphenationJP.HYP_BACK_.Contains(c);
		}

		private static bool IsLatin(char c)
		{
			return HyphenationJP.HYP_LATIN_.Contains(c);
		}

		private static bool IsNewLine(string s)
		{
			return HyphenationJP.REG_NEW_LINE.IsMatch(s);
		}

		private RectTransform _rectTransform;

		private Text _text;

		private static readonly string RICH_TEXT_REPLACE = "(<color=.*?>|</color>|<size=.*?>|</size>|<b>|</b>|<i>|</i>|<a href=.*?>|</a>)";

		private static readonly HashSet<char> HYP_FRONT_ = new HashSet<char>(",)]｝、。）〕〉》」』】〙〗〟’”｠»ァィゥェォッャュョヮヵヶっぁぃぅぇぉっゃゅょゎ‐゠–〜ー?!！？‼⁇⁈⁉・:;。.".ToCharArray());

		private static readonly HashSet<char> HYP_BACK_ = new HashSet<char>("(（[｛〔〈《「『【〘〖〝‘“｟«".ToCharArray());

		private static readonly HashSet<char> HYP_LATIN_ = new HashSet<char>("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789<>=/().,#\"'".ToCharArray());

		private static readonly Regex REG_NEW_LINE = new Regex("\r\n|\r|\n");

		private static readonly Regex REG_RICH_TEXT = new Regex(HyphenationJP.RICH_TEXT_REPLACE, RegexOptions.IgnoreCase);
	}
}
