using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class FreeScene_UI : MonoBehaviour
{
	public static void FreeScene_Start(string para, Action end_action = null)
	{
		if (FreeScene_UI.m_Now_UI)
		{
			UnityEngine.Object.Destroy(FreeScene_UI.m_Now_UI);
		}
		GameObject original = Resources.Load<GameObject>("Prefab/FreeScene_UI");
		FreeScene_UI.m_Now_UI = UnityEngine.Object.Instantiate<GameObject>(original);
		FreeScene_UI.m_Now_UI.GetComponent<FreeScene_UI>().Initialize(para, end_action);
	}

	private void Initialize(string para, Action end_action)
	{
		this.m_Use_String = para;
		this.m_End_Action = end_action;
		this.m_All_Text_Rect = new Dictionary<Text, RectTransform>();
		base.StartCoroutine(this.UI_Animation());
	}

	private void Set_Camera_Forward(RectTransform rect)
	{
		Vector3 position = GameMain.Instance.MainCamera.GetRealHeadTransform().position;
		Vector3 a = GameMain.Instance.MainCamera.GetRealHeadTransform().TransformDirection(Vector3.forward);
		rect.position = position + a * this.m_Forward_Length;
		Vector3 eulerAngles = GameMain.Instance.MainCamera.GetRealHeadTransform().eulerAngles;
		rect.eulerAngles = new Vector3(eulerAngles.x, eulerAngles.y, 0f);
		rect.localScale = Vector3.one;
		Vector2 vector = Vector2.one * 0.5f;
		rect.pivot = vector;
		vector = vector;
		rect.anchorMax = vector;
		rect.anchorMin = vector;
		base.GetComponent<CanvasScaler>().dynamicPixelsPerUnit = 5f;
	}

	private void Set_Text_Pos(ref List<Vector3> first_pos, ref List<Vector3> end_pos, RectTransform bace)
	{
		int num = 0;
		int max_count = 0;
		foreach (char c in this.m_Use_String)
		{
			int num2 = num / this.m_Line_Count;
			GameObject gameObject = new GameObject("Text", new Type[]
			{
				typeof(RectTransform),
				typeof(CanvasRenderer),
				typeof(Text),
				typeof(Outline)
			});
			RectTransform component = gameObject.GetComponent<RectTransform>();
			component.SetParent(base.transform);
			component.localEulerAngles = Vector3.zero;
			component.sizeDelta = Vector2.one * 100f;
			RectTransform rectTransform = component;
			Vector2 vector = Vector2.one * 0.5f;
			component.pivot = vector;
			vector = vector;
			component.anchorMax = vector;
			rectTransform.anchorMin = vector;
			Text component2 = gameObject.GetComponent<Text>();
			component2.font = Resources.Load<Font>("Font/FreeScene_UI_Font");
			component2.color = Color.white;
			component2.text = c.ToString();
			component2.alignment = TextAnchor.MiddleCenter;
			Color black = Color.black;
			black.a = 0.5f;
			gameObject.GetComponent<Outline>().effectColor = black;
			Vector3 localPosition;
			Vector3 item;
			if (GameMain.Instance.VRDeviceTypeID == GameMain.VRDeviceType.NON)
			{
				float x = bace.sizeDelta.x / 2f + component.sizeDelta.x / 2f + (float)(this.m_2D_Text_Size * (num % this.m_Line_Count));
				float y = (float)(-(float)num2 * this.m_2D_Text_Size);
				component.localPosition = new Vector3(x, y, 0f);
				component.localScale = Vector3.one;
				component2.fontSize = this.m_2D_Text_Size;
				gameObject.GetComponent<Outline>().effectDistance = Vector2.one * this.m_2D_Outline_Size;
				item = (localPosition = component.localPosition);
				item.x = -(bace.sizeDelta.x / 2f + component.sizeDelta.x / 2f + (float)(this.m_2D_Text_Size * (this.m_Line_Count - num % this.m_Line_Count)));
			}
			else
			{
				float x = this.m_Width_Length + (float)(this.m_Text_Size * (num % this.m_Line_Count)) * 0.01f;
				float y = (float)(-(float)num2 * this.m_Text_Size) * 0.01f;
				component.localScale = Vector3.one * 0.01f;
				component.localPosition = new Vector3(x, y, 0f);
				component2.fontSize = this.m_Text_Size;
				gameObject.GetComponent<Outline>().effectDistance = Vector2.one * this.m_Outline_Size;
				item = (localPosition = component.localPosition);
				item.x = -(this.m_Width_Length + (float)(this.m_Text_Size * (this.m_Line_Count - num % this.m_Line_Count)) * 0.01f);
			}
			first_pos.Add(localPosition);
			end_pos.Add(item);
			this.m_All_Text_Rect.Add(component2, component);
			if (c == '\n')
			{
				num += this.m_Line_Count - num % this.m_Line_Count;
			}
			else
			{
				num++;
			}
			if (num % this.m_Line_Count > max_count)
			{
				max_count = num % this.m_Line_Count;
			}
		}
		List<Vector3> source = new List<Vector3>(first_pos);
		List<Vector3> source2 = new List<Vector3>(end_pos);
		float center = (float)((this.m_Use_String.Length - 1) / max_count) * 0.5f;
		if (GameMain.Instance.VRDeviceTypeID == GameMain.VRDeviceType.NON)
		{
			first_pos = (from e in source
			select e += Vector3.up * center * (float)this.m_2D_Text_Size).ToList<Vector3>();
			source2 = (from e in source2
			select e += Vector3.up * center * (float)this.m_2D_Text_Size).ToList<Vector3>();
			end_pos = (from e in source2
			select e += Vector3.right * (float)(this.m_Line_Count - max_count) * (float)this.m_2D_Text_Size).ToList<Vector3>();
		}
		else
		{
			first_pos = (from e in source
			select e += Vector3.up * center * (float)this.m_Text_Size * 0.01f).ToList<Vector3>();
			source2 = (from e in source2
			select e += Vector3.up * center * (float)this.m_Text_Size * 0.01f).ToList<Vector3>();
			end_pos = (from e in source2
			select e += Vector3.right * (float)(this.m_Line_Count - max_count) * (float)this.m_Text_Size * 0.01f).ToList<Vector3>();
		}
	}

	private IEnumerator UI_Animation()
	{
		RectTransform my_rect = base.GetComponent<RectTransform>();
		if (GameMain.Instance.VRDeviceTypeID != GameMain.VRDeviceType.NON)
		{
			this.Set_Camera_Forward(my_rect);
			base.GetComponent<Canvas>().renderMode = RenderMode.WorldSpace;
			base.GetComponent<Canvas>().sortingOrder = 4;
		}
		List<Vector3> start_pos = new List<Vector3>();
		List<Vector3> end_pos = new List<Vector3>();
		this.Set_Text_Pos(ref start_pos, ref end_pos, my_rect);
		float timer = 0f;
		for (;;)
		{
			timer += Time.deltaTime;
			int count = 0;
			int string_count = 0;
			bool is_end = true;
			foreach (KeyValuePair<Text, RectTransform> keyValuePair in this.m_All_Text_Rect)
			{
				float num = this.m_Anime_Time + this.m_Anime_Lag * (float)(string_count % this.m_Line_Count);
				float num2 = timer;
				if (timer >= num / 2f + this.m_Stop_Time)
				{
					num2 -= this.m_Stop_Time;
				}
				if (num2 <= num)
				{
					is_end = false;
					float num3 = 180f * (num2 / num);
					float num4 = 1f - Mathf.Abs(Mathf.Sin(num3 * 0.0174532924f));
					Vector3 vector = 0.5f * start_pos[count] + 0.5f * end_pos[count];
					Vector3 a = vector;
					if (timer < num / 2f)
					{
						a = start_pos[count];
					}
					else if (timer >= num / 2f + this.m_Stop_Time)
					{
						a = end_pos[count];
					}
					keyValuePair.Value.localPosition = num4 * a + (1f - num4) * vector;
				}
				if (keyValuePair.Key.text == "\n")
				{
					string_count += this.m_Line_Count - string_count % this.m_Line_Count;
				}
				else
				{
					string_count++;
				}
				count++;
			}
			if (is_end)
			{
				break;
			}
			yield return null;
		}
		if (GameMain.Instance.VRDeviceTypeID == GameMain.VRDeviceType.NON)
		{
			if (this.m_End_Action != null)
			{
				this.m_End_Action();
			}
			FreeScene_UI.m_Now_UI = null;
			UnityEngine.Object.Destroy(base.gameObject);
		}
		else
		{
			yield return new WaitForSeconds(this.m_Wait_Time);
			base.StartCoroutine(this.Fade_Out());
		}
		yield break;
		yield break;
	}

	private IEnumerator Fade_Out()
	{
		float timer = 0f;
		Color text_col = Color.white;
		for (;;)
		{
			timer += Time.deltaTime;
			text_col.a = 1f - timer / this.m_Fade_Time;
			foreach (Text text in this.m_All_Text_Rect.Keys)
			{
				text.color = text_col;
			}
			if (timer >= this.m_Fade_Time)
			{
				break;
			}
			yield return null;
		}
		if (this.m_End_Action != null)
		{
			this.m_End_Action();
		}
		FreeScene_UI.m_Now_UI = null;
		UnityEngine.Object.Destroy(base.gameObject);
		yield break;
		yield break;
	}

	private const string m_FreeScene_UI_Path = "Prefab/FreeScene_UI";

	private const string m_Font_Path = "Font/FreeScene_UI_Font";

	private const string m_SE_Name = "SE015.ogg";

	[SerializeField]
	[Header("この文字数で改行")]
	private int m_Line_Count = 15;

	[SerializeField]
	[Header("2D用")]
	[Header("文字サイズ")]
	private int m_2D_Text_Size = 25;

	[SerializeField]
	[Header("外枠サイズ")]
	private float m_2D_Outline_Size = 2f;

	[SerializeField]
	[Header("VR用")]
	[Header("前方距離")]
	private float m_Forward_Length = 1f;

	[SerializeField]
	[Header("横位置")]
	private float m_Width_Length = 0.75f;

	[SerializeField]
	[Header("文字サイズ")]
	private int m_Text_Size = 15;

	[SerializeField]
	[Header("外枠サイズ")]
	private float m_Outline_Size = 1f;

	[SerializeField]
	[Header("アニメーション関連")]
	private float m_Anime_Time = 2f;

	[SerializeField]
	private float m_Anime_Lag = 0.25f;

	[SerializeField]
	private float m_Wait_Time = 2f;

	[SerializeField]
	private float m_Stop_Time = 1f;

	[SerializeField]
	private float m_Fade_Time = 0.5f;

	private string m_Use_String = string.Empty;

	private Dictionary<Text, RectTransform> m_All_Text_Rect;

	private Action m_End_Action;

	private static GameObject m_Now_UI;
}
