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

[ExecuteInEditMode]
public class HyperText : HyperTextBase
{
	protected override void OnEnable()
	{
		base.OnEnable();
		string regexPattern = "<a\\s+[^>]*href\\s*=\\s*[\"'](?<href>[^\"']*)[\"'][^>]*>(?<text>.*?)</a>";
		this.SetClickableByRegex(regexPattern, new Action<string>(this.OnClickHyperLink), new Action<HyperTextBase.ClickableEntry>(this.OnEnterHyperLink), new Action<HyperTextBase.ClickableEntry>(this.OnExitHyperLink));
	}

	public void SetClickableByRegex(string regexPattern, Action<string> onClick, Action<HyperTextBase.ClickableEntry> onEnter, Action<HyperTextBase.ClickableEntry> onExit)
	{
		this.SetClickableByRegex(regexPattern, Color.cyan, onClick, onEnter, onExit);
	}

	public void SetClickableByRegex(string regexPattern, Color color, Action<string> onClick, Action<HyperTextBase.ClickableEntry> onEnter, Action<HyperTextBase.ClickableEntry> onExit)
	{
		if (string.IsNullOrEmpty(regexPattern) || onClick == null)
		{
			return;
		}
		this._entryTable[regexPattern] = new HyperText.Entry(regexPattern, color, onClick, onEnter, onExit);
	}

	public override void RemoveClickable()
	{
		base.RemoveClickable();
		this._entryTable.Clear();
	}

	protected override void RegisterClickable()
	{
		foreach (HyperText.Entry entry in this._entryTable.Values)
		{
			Regex regex = new Regex(entry.RegexPattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
			IEnumerator enumerator2 = regex.Matches(base.text.text).GetEnumerator();
			try
			{
				while (enumerator2.MoveNext())
				{
					object obj = enumerator2.Current;
					Match match = (Match)obj;
					Group group = match.Groups["text"];
					Group group2 = match.Groups["href"];
					base.RegisterClickable(group.Index, group.Value.Length, group2.Value, entry.Color, entry.OnClick, entry.OnEnter, entry.OnExit);
				}
			}
			finally
			{
				IDisposable disposable;
				if ((disposable = (enumerator2 as IDisposable)) != null)
				{
					disposable.Dispose();
				}
			}
		}
	}

	private void OnClickHyperLink(string href)
	{
		if (GameMain.Instance.CMSystem.NetUse)
		{
			Application.OpenURL(href);
		}
		else
		{
			Debug.Log("リンク先：" + href);
		}
	}

	private void OnEnterHyperLink(HyperTextBase.ClickableEntry entry)
	{
	}

	private void OnExitHyperLink(HyperTextBase.ClickableEntry entry)
	{
	}

	private readonly Dictionary<string, HyperText.Entry> _entryTable = new Dictionary<string, HyperText.Entry>();

	private struct Entry
	{
		public Entry(string regexPattern, Color color, Action<string> onClick, Action<HyperTextBase.ClickableEntry> onEnter, Action<HyperTextBase.ClickableEntry> onExit)
		{
			this.RegexPattern = regexPattern;
			this.Color = color;
			this.OnClick = onClick;
			this.OnEnter = onEnter;
			this.OnExit = onExit;
		}

		public string RegexPattern;

		public Color Color;

		public Action<string> OnClick;

		public Action<HyperTextBase.ClickableEntry> OnEnter;

		public Action<HyperTextBase.ClickableEntry> OnExit;
	}
}
