using System;
using System.Collections.Generic;
using UnityEngine;
using wf;

public class SubtitleMovieManager : MonoBehaviour
{
	public bool isPlaying { get; private set; }

	public static SubtitleMovieManager GetGlobalInstance(bool casinoType)
	{
		GameObject childObject = UTY.GetChildObject(GameObject.Find("__GameMain__"), "SystemUI Root", false);
		GameObject gameObject;
		if (!casinoType)
		{
			gameObject = UTY.GetChildObject(childObject, "GlobalSubtitleMovieManager", true);
			if (gameObject == null)
			{
				gameObject = Utility.CreatePrefab(childObject, "System/Prefab/SubtitleMovieManager", true);
				gameObject.name = "GlobalSubtitleMovieManager";
			}
			gameObject.transform.localPosition = new Vector3(-459f, -247f, 0f);
		}
		else
		{
			gameObject = UTY.GetChildObject(childObject, "GlobalSubtitleMovieManagerCasinoType", true);
			if (gameObject == null)
			{
				gameObject = Utility.CreatePrefab(childObject, "System/Prefab/SubtitleMovieManagerCasino", true);
				gameObject.name = "GlobalSubtitleMovieManagerCasinoType";
			}
			gameObject.transform.localPosition = new Vector3(228f, 375f, 0f);
		}
		return gameObject.GetComponent<SubtitleMovieManager>();
	}

	public static void DestroyGlobalInstance()
	{
		GameObject childObject = UTY.GetChildObject(GameObject.Find("__GameMain__"), "SystemUI Root", false);
		GameObject childObject2 = UTY.GetChildObject(childObject, "GlobalSubtitleMovieManager", true);
		if (childObject2 != null)
		{
			UnityEngine.Object.DestroyImmediate(childObject2);
		}
		childObject2 = UTY.GetChildObject(childObject, "GlobalSubtitleMovieManagerCasinoType", true);
		if (childObject2 != null)
		{
			UnityEngine.Object.DestroyImmediate(childObject2);
		}
	}

	public void Clear()
	{
		this.displayDatas.Clear();
		this.isPlaying = false;
	}

	public bool AddData(string text, int displayStartTime, int displayTime)
	{
		if (this.isPlaying)
		{
			return false;
		}
		SubtitleMovieManager.DisplayData displayData = new SubtitleMovieManager.DisplayData();
		displayData.displayStartTime = displayStartTime;
		displayData.displayEndTime = displayStartTime + displayTime;
		displayData.text = text;
		this.displayDatas.Add(displayData);
		return true;
	}

	public void LoadSubtitleScriptFile(string fileName)
	{
		if (!GameUty.FileSystem.IsExistentFile(fileName))
		{
			return;
		}
		byte[] array = null;
		try
		{
			using (AFileBase afileBase = GameUty.FileOpen(fileName, null))
			{
				if (!afileBase.IsValid())
				{
					return;
				}
				array = new byte[afileBase.GetSize()];
				afileBase.Read(ref array, afileBase.GetSize());
			}
		}
		catch (Exception)
		{
			return;
		}
		if (array == null)
		{
			return;
		}
		string[] array2 = NUty.SjisToUnicode(array).Split(new string[]
		{
			"\r\n",
			"\r",
			"\n"
		}, StringSplitOptions.RemoveEmptyEntries);
		int num = 0;
		int num2 = 0;
		bool flag = false;
		string text = string.Empty;
		foreach (string text2 in array2)
		{
			if (text2.IndexOf("@talk") == 0)
			{
				int num3 = text2.IndexOf("[") + 1;
				string[] array4 = text2.Substring(num3, text2.Length - num3 - 1).Split(new string[]
				{
					"-"
				}, StringSplitOptions.RemoveEmptyEntries);
				num = int.Parse(array4[0]);
				num2 = int.Parse(array4[1]);
				flag = true;
				text = string.Empty;
			}
			else if (text2.IndexOf("@hitret") == 0)
			{
				this.AddData(text, num, num2 - num);
				flag = false;
			}
			else if (flag)
			{
				text += text2;
			}
		}
	}

	public void Play(string text, int displayTime)
	{
		this.Clear();
		this.AddData(text, 0, displayTime);
		this.Play();
	}

	public void Play()
	{
		this.timeElapsed = 0;
		this.isPlaying = true;
		this.subtitleMgr.visible = false;
	}

	public void Stop()
	{
		if (this.subtitleMgr != null)
		{
			this.subtitleMgr.visible = false;
		}
		this.isPlaying = false;
	}

	public void Update()
	{
		if (!this.isPlaying || this.displayDatas.Count == 0 || this.subtitleMgr == null)
		{
			this.Stop();
			return;
		}
		this.timeElapsed += (int)(Time.deltaTime * 1000f);
		bool visible = false;
		List<SubtitleMovieManager.DisplayData> list = new List<SubtitleMovieManager.DisplayData>();
		for (int i = 0; i < this.displayDatas.Count; i++)
		{
			SubtitleMovieManager.DisplayData displayData = this.displayDatas[i];
			if (displayData.displayStartTime <= this.timeElapsed && this.timeElapsed <= displayData.displayEndTime)
			{
				visible = true;
				this.subtitleMgr.SetTextFromScriptStyle(displayData.text);
			}
			else if (displayData.displayEndTime < this.timeElapsed)
			{
				list.Add(displayData);
			}
		}
		this.subtitleMgr.visible = visible;
		for (int j = 0; j < list.Count; j++)
		{
			for (int k = 0; k < this.displayDatas.Count; k++)
			{
				if (list[j] == this.displayDatas[k])
				{
					this.displayDatas.RemoveAt(k);
					break;
				}
			}
		}
		if (this.displayDatas.Count == 0 && this.autoDestroy)
		{
			UnityEngine.Object.DestroyImmediate(base.gameObject);
		}
	}

	[SerializeField]
	private SubtitleDisplayManager subtitleMgr;

	public bool autoDestroy;

	private int timeElapsed;

	private List<SubtitleMovieManager.DisplayData> displayDatas = new List<SubtitleMovieManager.DisplayData>();

	private class DisplayData
	{
		public int displayStartTime;

		public int displayEndTime;

		public string text;
	}
}
