using System;
using System.Collections.Generic;
using I2.Loc;
using UnityEngine;

public class DanceSubtitleMgr : PartsMgrBase
{
	protected override void Start()
	{
		base.Start();
		base.IsActive &= (DanceMain.SelectDanceData != null);
		base.IsActive &= DanceSetting.Settings.IsSubtitleOn;
		base.IsActive &= !Product.isJapan;
		if (!base.IsActive)
		{
			base.gameObject.SetActive(false);
			return;
		}
		this.m_SubtitleUI.SetActive(false);
		string csv_path = RhythmAction_Mgr.Instance.MusicCSV_Path + "dance_subtitle.nei";
		KasaiUtility.CsvReadY(csv_path, delegate(CsvParser csv, int cy)
		{
			int num = 0;
			DanceSubtitleMgr.SubtitleData subtitleData = new DanceSubtitleMgr.SubtitleData();
			subtitleData.ID = csv.GetCellAsInteger(num++, cy);
			subtitleData.StartTime = csv.GetCellAsReal(num++, cy);
			subtitleData.EndTime = csv.GetCellAsReal(num++, cy);
			subtitleData.TranslationKey = csv.GetCellAsString(num++, cy);
			this.m_SubtitleDataList.Add(subtitleData);
		}, 1, delegate
		{
			this.IsActive = false;
			Debug.Log(csv_path + "が見つからなかったので、ダンスの字幕を非表示にします。");
			this.gameObject.SetActive(false);
		});
	}

	private void DrawSubtitle()
	{
		float danceTimer = RhythmAction_Mgr.Instance.DanceTimer;
		string sheet_name = DanceMain.SelectDanceData.SubtitleSheetName + "/";
		Product.Language systemLanguage = Product.systemLanguage;
		using (List<DanceSubtitleMgr.SubtitleData>.Enumerator enumerator = this.m_SubtitleDataList.GetEnumerator())
		{
			while (enumerator.MoveNext())
			{
				DanceSubtitleMgr.SubtitleData data = enumerator.Current;
				if (data.EndTime <= danceTimer)
				{
					if (data.IsExistUIObj)
					{
						data.DestroyUIObj();
						this.m_SubtitleUIGrid.UpdateChildUI();
					}
				}
				else if (data.StartTime <= danceTimer && !data.IsExistUIObj)
				{
					bool create_success = false;
					Action<Product.Language> action = delegate(Product.Language langage)
					{
						string term = sheet_name + data.TranslationKey;
						string translation = LocalizationManager.GetTranslation(term, true, 0, true, false, null, Product.EnumConvert.ToI2LocalizeLanguageName(langage));
						if (string.IsNullOrEmpty(translation))
						{
							return;
						}
						GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.m_SubtitleUI, this.m_SubtitleUIGrid.transform, false);
						gameObject.SetActive(true);
						data.SetUIObj(langage, gameObject);
						UILabel component = gameObject.GetComponent<UILabel>();
						component.text = translation;
						if (component.width > this.m_NewLineWidthSize)
						{
							component.overflowMethod = UILabel.Overflow.ResizeHeight;
							component.width = this.m_NewLineWidthSize;
							component.ProcessText();
						}
						create_success |= true;
					};
					if (Product.supportMultiLanguage)
					{
						switch (DanceSetting.Settings.SubtitleType)
						{
						case SubtitleDisplayManager.DisplayType.OriginalAndSubtitle:
							action(Product.Language.Japanese);
							action(systemLanguage);
							break;
						case SubtitleDisplayManager.DisplayType.Subtitle:
							action(systemLanguage);
							break;
						case SubtitleDisplayManager.DisplayType.Original:
							action(Product.Language.Japanese);
							break;
						}
					}
					else
					{
						action(Product.Language.Japanese);
					}
					if (create_success)
					{
						this.m_SubtitleUIGrid.UpdateChildUI();
					}
				}
			}
		}
	}

	public override void StartAction()
	{
		RhythmAction_Mgr.Instance.StartTimeCroutine(new Action(this.DrawSubtitle), null);
	}

	private List<DanceSubtitleMgr.SubtitleData> m_SubtitleDataList = new List<DanceSubtitleMgr.SubtitleData>();

	[SerializeField]
	[Header("使い回し用オブジェクト")]
	private GameObject m_SubtitleUI;

	[SerializeField]
	[Header("字幕UIの親")]
	private DynamicUIGrid m_SubtitleUIGrid;

	[SerializeField]
	[Header("表示領域の横サイズがこれ以上になったら改行")]
	private int m_NewLineWidthSize = 930;

	private class SubtitleData
	{
		public bool IsExistUIObj
		{
			get
			{
				return this.OrijinalUI != null || this.SubtitleUI != null;
			}
		}

		public void SetUIObj(Product.Language langage_code, GameObject ui_obj)
		{
			if (langage_code == Product.Language.Japanese)
			{
				this.OrijinalUI = ui_obj;
			}
			else
			{
				this.SubtitleUI = ui_obj;
			}
		}

		public void DestroyUIObj()
		{
			if (this.OrijinalUI)
			{
				UnityEngine.Object.DestroyImmediate(this.OrijinalUI);
			}
			if (this.SubtitleUI)
			{
				UnityEngine.Object.DestroyImmediate(this.SubtitleUI);
			}
		}

		public int ID;

		public float StartTime;

		public float EndTime;

		public string TranslationKey;

		public GameObject OrijinalUI;

		public GameObject SubtitleUI;
	}
}
