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;
		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
		{
			Debug.Log(csv_path + "が見つからなかったので、ダンスの字幕を非表示にします。");
			this.gameObject.SetActive(false);
		});
	}

	private void DrawSubtitle()
	{
		float danceTimer = RhythmAction_Mgr.Instance.DanceTimer;
		string str = DanceMain.SelectDanceData.SubtitleSheetName + "/";
		string currentLanguageCode = LocalizationManager.CurrentLanguageCode;
		foreach (DanceSubtitleMgr.SubtitleData subtitleData in this.m_SubtitleDataList)
		{
			if (subtitleData.EndTime <= danceTimer)
			{
				if (subtitleData.SubtitleUI)
				{
					UnityEngine.Object.DestroyImmediate(subtitleData.SubtitleUI);
					this.m_SubtitleUIGrid.UpdateChildUI();
				}
			}
			else if (subtitleData.StartTime <= danceTimer && !subtitleData.SubtitleUI)
			{
				string term = str + subtitleData.TranslationKey;
				string translation = LocalizationManager.GetTranslation(term, true, 0, true, false, null, currentLanguageCode);
				if (!string.IsNullOrEmpty(translation))
				{
					subtitleData.SubtitleUI = UnityEngine.Object.Instantiate<GameObject>(this.m_SubtitleUI, this.m_SubtitleUIGrid.transform, false);
					subtitleData.SubtitleUI.SetActive(true);
					UILabel component = subtitleData.SubtitleUI.GetComponent<UILabel>();
					component.text = translation;
					if (component.width > this.m_NewLineWidthSize)
					{
						component.overflowMethod = UILabel.Overflow.ResizeHeight;
						component.width = this.m_NewLineWidthSize;
						component.ProcessText();
					}
					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 int ID;

		public float StartTime;

		public float EndTime;

		public string TranslationKey;

		public GameObject SubtitleUI;
	}
}
