using System;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;

public class MessageClass
{
	public MessageClass(GameObject message_window_panel, MessageWindowMgr message_mgr)
	{
		this.script_mgr_ = GameMain.Instance.ScriptMgr;
		this.message_mgr_ = message_mgr;
		this.exit_wait_count_ = -1;
		this.message_window_panel_ = message_window_panel;
		this.message_label_ = UTY.GetChildObject(this.message_window_panel_, "MessageViewer/MsgParent/Message", false).GetComponent<UILabel>();
		this.name_label_ = UTY.GetChildObject(this.message_window_panel_, "MessageViewer/MsgParent/SpeakerName/Name", false).GetComponent<UILabel>();
		this.subtitles_manager_ = UTY.GetChildObject(this.message_window_panel_, "MessageViewer/MsgParent/SubtitlesDisplayPanel", false).GetComponent<SubtitleDisplayManager>();
		this.hitret_sprite_ = UTY.GetChildObject(this.message_window_panel_, "MessageViewer/MsgParent/Hitret", false).GetComponent<UISprite>();
		this.hitret_sprite_.alpha = 0f;
		this.mainTextAnimation = new MessageClass.TextAnimation();
		this.subTextAnimation = new MessageClass.TextAnimation();
		this.subtitles_manager_.visible = true;
		this.message_label_.gameObject.SetActive(false);
		this.name_label_.gameObject.SetActive(false);
		UTY.GetChildObject(this.message_window_panel_, "MessageViewer/MsgParent/MessageBox", false).SetActive(false);
		this.message_label_ = (this.name_label_ = null);
	}

	public static KeyValuePair<string, string> GetTranslationText(string baseText)
	{
		if (string.IsNullOrEmpty(baseText))
		{
			return new KeyValuePair<string, string>(string.Empty, string.Empty);
		}
		int num = baseText.IndexOf("<E>");
		if (num < 0)
		{
			num = baseText.IndexOf("<e>");
			if (num < 0)
			{
				return new KeyValuePair<string, string>(baseText, string.Empty);
			}
		}
		return new KeyValuePair<string, string>(baseText.Substring(0, num), baseText.Substring(num + 3, baseText.Length - (num + 3)));
	}

	public static string GetWrapString(UILabel draw_target, string text)
	{
		string text2 = string.Empty;
		draw_target.Wrap(text, out text2);
		if (text2.IndexOf('\n') == -1)
		{
			return text2;
		}
		string a = text2;
		string[] collection = text2.Split(new char[]
		{
			'\n'
		});
		List<string> list = new List<string>();
		list.AddRange(collection);
		text2 = string.Empty;
		for (int i = 0; i < list.Count - 1; i++)
		{
			string text3 = list[i];
			string text4 = list[i + 1];
			StringInfo stringInfo = new StringInfo(text3);
			int lengthInTextElements = stringInfo.LengthInTextElements;
			StringInfo stringInfo2 = new StringInfo(text4);
			int lengthInTextElements2 = stringInfo2.LengthInTextElements;
			while (0 < lengthInTextElements2 && 0 < lengthInTextElements)
			{
				string value = stringInfo2.SubstringByTextElements(0, 1);
				if (0 > MessageClass.kInsokuString.IndexOf(value))
				{
					break;
				}
				text4 = stringInfo.SubstringByTextElements(lengthInTextElements - 1, 1) + text4;
				text3 = stringInfo.SubstringByTextElements(0, lengthInTextElements - 1);
				stringInfo = new StringInfo(text3);
				lengthInTextElements = stringInfo.LengthInTextElements;
				stringInfo2 = new StringInfo(text4);
				lengthInTextElements2 = stringInfo2.LengthInTextElements;
			}
			text2 = text2 + text3 + '\n';
			list[i] = string.Empty;
			list[i + 1] = text4;
			string text5 = string.Empty;
			foreach (string text6 in list)
			{
				if (!(text6 == string.Empty) && !(text6 == "\n"))
				{
					text5 += text6;
				}
			}
			string empty = string.Empty;
			draw_target.Wrap(text5, out empty);
			text5 = empty;
			list.Clear();
			list.AddRange(text5.Split(new char[]
			{
				'\n'
			}));
			i = -1;
			if (a == text5)
			{
				break;
			}
			a = text5;
		}
		if (1 <= list.Count)
		{
			text2 += list[list.Count - 1];
		}
		return text2;
	}

	public void SetText(string name, string text, string voice_file, int voice_pitch)
	{
		this.exit_wait_count_ = -1;
		this.hitret_sprite_.alpha = 0f;
		this.charaNameText = name;
		KeyValuePair<string, string> translationText = MessageClass.GetTranslationText(text);
		this.mainTextAnimation.StartAnimation(translationText.Key);
		this.subTextAnimation.StartAnimation(translationText.Value);
		this.message_mgr_.AddBackLog(name, this.mainTextAnimation.text, this.subTextAnimation.text, voice_file, voice_pitch);
		this.Update();
	}

	public void Clear()
	{
		this.exit_wait_count_ = -1;
		string text = string.Empty;
		this.charaNameText = text;
		text = text;
		this.messageText = text;
		this.subtitlesText = text;
		this.mainTextAnimation.Clear();
		this.subTextAnimation.Clear();
		this.hitret_sprite_.alpha = 0f;
	}

	public void FinishChAnime()
	{
		if (!this.ch_anime_enabled)
		{
			return;
		}
		this.messageText = this.mainTextAnimation.text;
		this.mainTextAnimation.Clear();
		this.subtitlesText = this.subTextAnimation.text;
		this.subTextAnimation.Clear();
		this.hitret_sprite_.alpha = 255f;
		this.exit_wait_count_ = GameMain.tick_count;
	}

	public void Update()
	{
		if (!this.ch_anime_enabled)
		{
			return;
		}
		KeyValuePair<bool, string> keyValuePair = this.mainTextAnimation.UodateAnimation();
		KeyValuePair<bool, string> keyValuePair2 = this.subTextAnimation.UodateAnimation();
		if (this.skip_mode || keyValuePair.Key)
		{
			this.FinishChAnime();
		}
		else
		{
			this.messageText = keyValuePair.Value;
			this.subtitlesText = (keyValuePair2.Key ? this.subTextAnimation.text : keyValuePair2.Value);
		}
	}

	public int GetExitWaitCount()
	{
		return (this.exit_wait_count_ >= 0) ? (GameMain.tick_count - this.exit_wait_count_) : this.exit_wait_count_;
	}

	public bool ch_anime_enabled
	{
		get
		{
			return this.mainTextAnimation.isPlay;
		}
	}

	private string messageText
	{
		set
		{
			if (this.message_label_ != null)
			{
				this.message_label_.text = value;
			}
			if (this.subtitles_manager_ != null)
			{
				this.subtitles_manager_.originalText = value;
			}
		}
	}

	private string subtitlesText
	{
		set
		{
			if (this.subtitles_manager_ != null)
			{
				this.subtitles_manager_.subtitlesText = value;
			}
		}
	}

	private string charaNameText
	{
		set
		{
			if (this.name_label_ != null)
			{
				this.name_label_.text = value;
			}
			if (this.subtitles_manager_ != null)
			{
				this.subtitles_manager_.charaNameText = value;
			}
		}
	}

	private bool skip_mode
	{
		get
		{
			return this.script_mgr_.adv_kag.skip_mode;
		}
	}

	public static readonly string kInsokuString = "。、・？！゛゜ヽヾゝゞ々ー）］｝」』!),.:;?]}｡｣､･ｰﾟ";

	private GameObject message_window_panel_;

	private readonly MessageWindowMgr message_mgr_;

	private readonly ScriptManager script_mgr_;

	private UISprite hitret_sprite_;

	private int exit_wait_count_;

	private MessageClass.TextAnimation mainTextAnimation;

	private MessageClass.TextAnimation subTextAnimation;

	private UILabel message_label_;

	private UILabel name_label_;

	public SubtitleDisplayManager subtitles_manager_;

	private class TextAnimation
	{
		private int ch_wait
		{
			get
			{
				if (this.text_info == null || this.text_info.LengthInTextElements == 0)
				{
					return 0;
				}
				return (100 - GameMain.Instance.CMSystem.MsgTextSpeed) * 20 / this.text_info.LengthInTextElements;
			}
		}

		public bool isPlay
		{
			get
			{
				return this.type != MessageClass.TextAnimation.Type.Null;
			}
		}

		public string text
		{
			get
			{
				return (this.text_info == null) ? string.Empty : this.text_info.String;
			}
		}

		public void StartAnimation(string text)
		{
			this.Clear();
			this.text_info = new StringInfo(text);
			this.type = MessageClass.TextAnimation.Type.Execution;
			this.start_time = GameMain.tick_count - this.ch_wait;
		}

		public KeyValuePair<bool, string> UodateAnimation()
		{
			if (this.type == MessageClass.TextAnimation.Type.Null || this.ch_wait <= 0)
			{
				return new KeyValuePair<bool, string>(true, null);
			}
			int num = Mathf.Max(0, Mathf.Min((GameMain.tick_count - this.start_time) / this.ch_wait, this.text_info.LengthInTextElements));
			if (num == this.text_info.LengthInTextElements)
			{
				return new KeyValuePair<bool, string>(true, null);
			}
			return new KeyValuePair<bool, string>(false, this.text_info.SubstringByTextElements(0, num));
		}

		public void Clear()
		{
			this.type = MessageClass.TextAnimation.Type.Null;
			this.start_time = 0;
			this.text_info = null;
		}

		private MessageClass.TextAnimation.Type type;

		private int start_time;

		private StringInfo text_info;

		private enum Type
		{
			Null,
			Execution
		}
	}
}
