using System.Collections;
using System.Collections.Generic;
using Cute;
using UnityEngine;

namespace Wizard;

public class MovieSubtitles : MonoBehaviour
{
	private class SubtitlesData
	{
		public string Text { get; private set; }

		public int StartPositionTime { get; private set; }

		public int EndPositionTime { get; private set; }

		public SubtitlesData(string text, int startPositionTime, int endPositionTime)
		{
			Text = text;
			StartPositionTime = startPositionTime;
			EndPositionTime = endPositionTime;
		}
	}

	private enum eStateSubtitles
	{
		READY,
		WAIT_DISPLAY_START,
		WAIT_DISPLAY_END,
		FINISH
	}

	[SerializeField]
	private UILabel _labelMovieSubtitles;

	public void PlaySubtitles(string subtitlesCSV)
	{
		if (!PlayerPrefsWrapper.GetBool(PlayerPrefsWrapper.MOVIE_SUBTITLES))
		{
			Finish();
			return;
		}
		List<SubtitlesData> subtitlesDataList = ParseSubtitlesData(subtitlesCSV);
		base.gameObject.SetLayer(LayerMask.NameToLayer("SystemUI"), isSetChildren: true);
		_labelMovieSubtitles.gameObject.SetActive(value: true);
		StartCoroutine(ISubtitlesOnPlayMovie(subtitlesDataList));
	}

	public void Finish()
	{
		Object.Destroy(base.gameObject);
	}

	private List<SubtitlesData> ParseSubtitlesData(string subtitlesCSV)
	{
		List<SubtitlesData> list = new List<SubtitlesData>();
		foreach (ArrayList item in Utility.ConvertCSV(subtitlesCSV))
		{
			string[] obj = (string[])item.ToArray(typeof(string));
			string text = obj[0];
			int startPositionTime = int.Parse(obj[1]);
			int endPositionTime = int.Parse(obj[2]);
			list.Add(new SubtitlesData(text, startPositionTime, endPositionTime));
		}
		return list;
	}

	private IEnumerator ISubtitlesOnPlayMovie(List<SubtitlesData> subtitlesDataList)
	{
		int index = 0;
		SubtitlesData data = null;
		eStateSubtitles state = eStateSubtitles.READY;
		_labelMovieSubtitles.text = string.Empty;
		while (true)
		{
			switch (state)
			{
			case eStateSubtitles.READY:
				if (index < subtitlesDataList.Count)
				{
					data = subtitlesDataList[index];
					state = eStateSubtitles.WAIT_DISPLAY_START;
				}
				else
				{
					state = eStateSubtitles.FINISH;
				}
				continue;
			case eStateSubtitles.WAIT_DISPLAY_START:
				if (Toolbox.MovieManager.GetSeekPosition() > data.StartPositionTime)
				{
					_labelMovieSubtitles.text = data.Text;
					state = eStateSubtitles.WAIT_DISPLAY_END;
					continue;
				}
				break;
			case eStateSubtitles.WAIT_DISPLAY_END:
				if (Toolbox.MovieManager.GetSeekPosition() > data.EndPositionTime)
				{
					_labelMovieSubtitles.text = string.Empty;
					state = eStateSubtitles.READY;
					index++;
					continue;
				}
				break;
			}
			if (state == eStateSubtitles.FINISH)
			{
				break;
			}
			yield return null;
		}
		Finish();
	}
}
