using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using RenderHeads.Media.AVProVideo;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class VRExternalFileLoader : MonoBehaviour
{
	private void Update()
	{
		if (SceneVRCommunication.Instance == null)
		{
			return;
		}
		bool isFreeMode = SceneVRCommunication.Instance.IsFreeMode;
		if (!isFreeMode && this.m_BeforeFreeMode)
		{
			VRExternalFileLoader.Event_CloseMovie();
			if (SceneVRCommunication.Instance.GetNowTime() == SceneVRCommunication.VR_TIME.NIGHT)
			{
				GameMain.Instance.SoundMgr.PlayBGM("BGM_vr0001.ogg", 1f, true);
			}
			else
			{
				GameMain.Instance.SoundMgr.PlayBGM("BGM007.ogg", 1f, true);
			}
		}
		this.m_BeforeFreeMode = isFreeMode;
	}

	public void LoadAllMovieFile(string dataPath, params string[] extensions)
	{
		this.m_MediaPlayer.m_VideoPath = dataPath;
		DirectoryInfo directoryInfo = new DirectoryInfo(dataPath);
		if (!directoryInfo.Exists)
		{
			Directory.CreateDirectory(dataPath);
		}
		foreach (string searchPattern in extensions)
		{
			FileInfo[] files = directoryInfo.GetFiles(searchPattern);
			foreach (FileInfo fileInfo in files)
			{
				this.CreateMediaInfoButton(this.m_UIPrefabMovieButton.gameObject, fileInfo.Name, fileInfo.FullName, 0, MediaPlayerEvent.EventType.FirstFrameReady);
				VRExternalFileLoader.m_LoadingCount++;
			}
		}
	}

	public void LoadAllMusicFile(string dataPath, params string[] extensions)
	{
		this.m_MediaPlayer.m_VideoPath = dataPath;
		DirectoryInfo directoryInfo = new DirectoryInfo(dataPath);
		if (!directoryInfo.Exists)
		{
			Directory.CreateDirectory(dataPath);
		}
		foreach (string searchPattern in extensions)
		{
			FileInfo[] files = directoryInfo.GetFiles(searchPattern);
			foreach (FileInfo fileInfo in files)
			{
				this.CreateMediaInfoButton(this.m_UIPrefabMusicButton.gameObject, fileInfo.Name, fileInfo.FullName, 0, MediaPlayerEvent.EventType.ReadyToPlay);
				VRExternalFileLoader.m_LoadingCount++;
			}
		}
	}

	private IEnumerator Coroutine_GetMovieThumbnail(UIButtonElement element, string moviePath, GameObject elementObject)
	{
		while (!VRExternalFileLoader.m_IsEndLoad)
		{
			yield return null;
		}
		VRExternalFileLoader.m_IsEndLoad = false;
		if (this.m_MovieOutputer)
		{
			this.m_MovieOutputer.enabled = false;
		}
		if (!File.Exists(moviePath))
		{
			VRExternalFileLoader.m_IsEndLoad = true;
			VRExternalFileLoader.m_LoadingCount--;
			this.m_MediaPlayer.CloseVideo();
			this.m_MovieOutputer.enabled = true;
			yield break;
		}
		this.m_MediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.AbsolutePathOrURL, moviePath, false);
		int isOpenReady = 1;
		this.m_MediaPlayer.Events.AddListener(delegate(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
		{
			if (et != MediaPlayerEvent.EventType.Error)
			{
				if (et == MediaPlayerEvent.EventType.FirstFrameReady)
				{
					isOpenReady = 0;
				}
			}
			else
			{
				isOpenReady = -1;
			}
		});
		while (isOpenReady == 1)
		{
			yield return null;
		}
		if (isOpenReady == -1)
		{
			UnityEngine.Object.Destroy(elementObject);
			yield return null;
			VRExternalFileLoader.m_IsEndLoad = true;
			VRExternalFileLoader.m_LoadingCount--;
			this.m_MediaPlayer.CloseVideo();
			this.m_MovieOutputer.enabled = true;
			yield break;
		}
		IMediaInfo info = this.m_MediaPlayer.Info;
		Texture2D tex2D = new Texture2D(info.GetVideoWidth(), info.GetVideoHeight(), TextureFormat.ARGB32, false);
		this.m_MediaPlayer.ExtractFrame(tex2D, 0.5f, false, 50);
		if (tex2D)
		{
			Rect rect = new Rect(0f, 0f, (float)tex2D.width, (float)tex2D.height);
			element.imageThumbnail.sprite = Sprite.Create(tex2D, rect, Vector2.zero, 100f, 0u, SpriteMeshType.FullRect);
		}
		yield return null;
		float tim = info.GetDurationMs();
		tim *= 0.001f;
		string str = "再生時間 " + Mathf.Floor(tim / 60f).ToString("00") + ":" + Mathf.Repeat(tim, 60f).ToString("00");
		element.textContentTime.text = str;
		VRExternalFileLoader.m_IsEndLoad = true;
		VRExternalFileLoader.m_LoadingCount--;
		this.m_MediaPlayer.CloseVideo();
		this.m_MovieOutputer.enabled = true;
		yield break;
	}

	private IEnumerator Coroutine_GetMovieInfo(string moviePath, MediaPlayerEvent.EventType successType, UnityAction<bool, IMediaInfo> callback)
	{
		while (!VRExternalFileLoader.m_IsEndLoad)
		{
			yield return null;
		}
		VRExternalFileLoader.m_IsEndLoad = false;
		if (this.m_MovieOutputer)
		{
			this.m_MovieOutputer.enabled = false;
		}
		if (!File.Exists(moviePath))
		{
			VRExternalFileLoader.m_IsEndLoad = true;
			VRExternalFileLoader.m_LoadingCount--;
			this.m_MediaPlayer.CloseVideo();
			this.m_MovieOutputer.enabled = true;
			callback(false, null);
			yield break;
		}
		this.m_MediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.AbsolutePathOrURL, moviePath, false);
		int isOpenReady = 1;
		this.m_MediaPlayer.Events.AddListener(delegate(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
		{
			if (et == MediaPlayerEvent.EventType.Error)
			{
				isOpenReady = -1;
			}
			else if (et == successType)
			{
				isOpenReady = 0;
			}
		});
		while (isOpenReady == 1)
		{
			yield return null;
		}
		if (isOpenReady == -1)
		{
			VRExternalFileLoader.m_IsEndLoad = true;
			VRExternalFileLoader.m_LoadingCount--;
			this.m_MediaPlayer.CloseVideo();
			this.m_MovieOutputer.enabled = true;
			callback(false, null);
			yield break;
		}
		IMediaInfo info = this.m_MediaPlayer.Info;
		callback(true, info);
		yield break;
	}

	private IEnumerator Coroutine_GetMusicPlaybackTime(UIButtonElement element, string musicPath)
	{
		while (!VRExternalFileLoader.m_IsEndLoad)
		{
			yield return null;
		}
		VRExternalFileLoader.m_IsEndLoad = false;
		this.m_MediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.AbsolutePathOrURL, musicPath, false);
		bool isOpenReady = false;
		this.m_MediaPlayer.Events.AddListener(delegate(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
		{
			if (et == MediaPlayerEvent.EventType.ReadyToPlay)
			{
				isOpenReady = true;
			}
		});
		while (!isOpenReady)
		{
			yield return null;
		}
		IMediaInfo info = this.m_MediaPlayer.Info;
		float tim = info.GetDurationMs();
		tim *= 0.001f;
		for (int i = 0; i < 3; i++)
		{
			yield return null;
		}
		string str = "再生時間 " + Mathf.Floor(tim / 60f).ToString("00") + ":" + Mathf.Repeat(tim, 60f).ToString("00");
		element.textContentTime.text = str;
		VRExternalFileLoader.m_IsEndLoad = true;
		VRExternalFileLoader.m_LoadingCount--;
		this.m_MediaPlayer.CloseVideo();
		yield break;
	}

	private IEnumerator Coroutine_LoadMovieAsset(string LocalFilePath, string FileName, string AssetBundleURL)
	{
		string FullPath = LocalFilePath + FileName;
		if (File.Exists(FullPath))
		{
			yield break;
		}
		WWW www = new WWW(AssetBundleURL);
		yield return www;
		AssetBundle assetbundle = www.assetBundle;
		AssetBundleRequest resultObject = assetbundle.LoadAssetAsync<TextAsset>(FileName);
		yield return new WaitWhile(() => !resultObject.isDone);
		TextAsset movie = resultObject.asset as TextAsset;
		byte[] byteData = movie.bytes;
		File.WriteAllBytes(FullPath, byteData);
		assetbundle.Unload(false);
		yield break;
	}

	private IEnumerator Coroutine_Refresh()
	{
		VRExternalFileLoader.m_LoadingCount = 0;
		VRExternalFileLoader.m_IsEndLoad = true;
		this.m_MediaFilePath = Path.GetFullPath(".\\");
		if (VRExternalFileLoader.m_PtrMediaPlayerObject == null)
		{
			VRExternalFileLoader.m_PtrMediaPlayerObject = (UnityEngine.Object.Instantiate(Resources.Load("SceneVRCommunication\\Tablet\\Media Outputer (Music And Movie)")) as GameObject);
		}
		GameObject[] MediaPlayers = GameObject.FindGameObjectsWithTag("MediaPlayerObj");
		for (int i = 0; i < MediaPlayers.Length; i++)
		{
			if (this.m_MediaPlayer != null && this.m_MovieOutputer != null)
			{
				break;
			}
			if (this.m_MediaPlayer == null)
			{
				this.m_MediaPlayer = MediaPlayers[i].GetComponent<MediaPlayer>();
			}
			if (this.m_MovieOutputer == null)
			{
				this.m_MovieOutputer = MediaPlayers[i].GetComponent<ApplyToMaterial>();
			}
		}
		this.m_MediaPlayer.m_AutoOpen = false;
		this.m_MediaPlayer.m_AutoStart = false;
		this.m_MovieOutputer.GetComponent<MeshRenderer>().enabled = false;
		this.m_UIContent.anchoredPosition = Vector2.zero;
		for (int j = 0; j < this.m_UIContent.childCount; j++)
		{
			this.m_UIContent.GetChild(j).gameObject.SetActive(false);
		}
		if (this.m_TypeMedia == VRExternalFileLoader.TYPE_MEDIA.Movie)
		{
			string fileName = "vrcom_movie.nei";
			if (!GameUty.FileSystem.IsExistentFile(fileName))
			{
				NDebug.Assert("表がありません。" + fileName, false);
			}
			using (AFileBase file = GameUty.FileSystem.FileOpen(fileName))
			{
				using (CsvParser csv = new CsvParser())
				{
					bool result = csv.Open(file);
					NDebug.Assert(result, fileName + "\nopen failed.");
					for (int y = 1; y < csv.max_cell_y; y++)
					{
						if (csv.IsCellToExistData(0, y))
						{
							int x = 0;
							ACsvParser acsvParser = csv;
							int cell_x;
							x = (cell_x = x) + 1;
							int id = acsvParser.GetCellAsInteger(cell_x, y);
							ACsvParser acsvParser2 = csv;
							x = (cell_x = x) + 1;
							string thumbnailPath = acsvParser2.GetCellAsString(cell_x, y);
							ACsvParser acsvParser3 = csv;
							x = (cell_x = x) + 1;
							string movieName = acsvParser3.GetCellAsString(cell_x, y);
							ACsvParser acsvParser4 = csv;
							x = (cell_x = x) + 1;
							string movieTitle = acsvParser4.GetCellAsString(cell_x, y);
							ACsvParser acsvParser5 = csv;
							x = (cell_x = x) + 1;
							string getFlag = acsvParser5.GetCellAsString(cell_x, y);
							ACsvParser acsvParser6 = csv;
							x = (cell_x = x) + 1;
							string defaultFace = acsvParser6.GetCellAsString(cell_x, y);
							ACsvParser acsvParser7 = csv;
							x = (cell_x = x) + 1;
							string faceEvent = acsvParser7.GetCellAsString(cell_x, y);
							ACsvParser acsvParser8 = csv;
							x = (cell_x = x) + 1;
							string faceEvent2 = acsvParser8.GetCellAsString(cell_x, y);
							ACsvParser acsvParser9 = csv;
							x = (cell_x = x) + 1;
							string faceEvent3 = acsvParser9.GetCellAsString(cell_x, y);
							ACsvParser acsvParser10 = csv;
							x = (cell_x = x) + 1;
							string faceEvent4 = acsvParser10.GetCellAsString(cell_x, y);
							ACsvParser acsvParser11 = csv;
							x = (cell_x = x) + 1;
							string endEvent = acsvParser11.GetCellAsString(cell_x, y);
							int flag = 0;
							if (GameMain.Instance.CharacterMgr.GetMaid(0) != null)
							{
								flag = GameMain.Instance.CharacterMgr.GetMaid(0).status.GetFlag(getFlag);
							}
							if (flag == 1)
							{
								int faceIndex = 0;
								if (!string.IsNullOrEmpty(defaultFace))
								{
									if (defaultFace == "鑑賞_喜び")
									{
										faceIndex = 0;
									}
									else if (defaultFace == "鑑賞_怒り")
									{
										faceIndex = 1;
									}
									else if (defaultFace == "鑑賞_哀しみ")
									{
										faceIndex = 2;
									}
									else if (defaultFace == "鑑賞_楽しい")
									{
										faceIndex = 3;
									}
								}
								this.CreateMediaInfoButton3(this.m_UIPrefabMovieButton.gameObject, movieTitle, this.m_MediaFilePath + "GameData/movie_vrcom/" + movieName + ".ine", thumbnailPath, faceIndex, MediaPlayerEvent.EventType.FirstFrameReady);
								yield return null;
							}
						}
					}
				}
			}
			this.LoadAllMovieFile(this.m_MediaFilePath + "movie_vrcom/", new string[]
			{
				"*.mp4",
				"*.mov",
				"*.avi"
			});
		}
		else if (this.m_TypeMedia == VRExternalFileLoader.TYPE_MEDIA.Music)
		{
			string fileName2 = "vrcom_music.nei";
			if (!GameUty.FileSystem.IsExistentFile(fileName2))
			{
				NDebug.Assert("表がありません。" + fileName2, false);
			}
			using (AFileBase file2 = GameUty.FileSystem.FileOpen(fileName2))
			{
				using (CsvParser csv2 = new CsvParser())
				{
					bool result2 = csv2.Open(file2);
					NDebug.Assert(result2, fileName2 + "\nopen failed.");
					for (int y2 = 1; y2 < csv2.max_cell_y; y2++)
					{
						if (csv2.IsCellToExistData(0, y2))
						{
							int x2 = 0;
							ACsvParser acsvParser12 = csv2;
							int cell_x;
							x2 = (cell_x = x2) + 1;
							int id2 = acsvParser12.GetCellAsInteger(cell_x, y2);
							ACsvParser acsvParser13 = csv2;
							x2 = (cell_x = x2) + 1;
							string defaultFace2 = acsvParser13.GetCellAsString(cell_x, y2);
							ACsvParser acsvParser14 = csv2;
							x2 = (cell_x = x2) + 1;
							string faceEvent5 = acsvParser14.GetCellAsString(cell_x, y2);
							ACsvParser acsvParser15 = csv2;
							x2 = (cell_x = x2) + 1;
							string faceEvent6 = acsvParser15.GetCellAsString(cell_x, y2);
							ACsvParser acsvParser16 = csv2;
							x2 = (cell_x = x2) + 1;
							string faceEvent7 = acsvParser16.GetCellAsString(cell_x, y2);
							ACsvParser acsvParser17 = csv2;
							x2 = (cell_x = x2) + 1;
							string faceEvent8 = acsvParser17.GetCellAsString(cell_x, y2);
							ACsvParser acsvParser18 = csv2;
							x2 = (cell_x = x2) + 1;
							string endEvent2 = acsvParser18.GetCellAsString(cell_x, y2);
							string musicTitle = "none";
							string musicName = "none.ogg";
							this.GetBGMName(id2, ref musicTitle, ref musicName);
							if (GameUty.FileSystem.IsExistentFile(musicName))
							{
								int faceIndex2 = 0;
								if (!string.IsNullOrEmpty(defaultFace2))
								{
									if (defaultFace2 == "鑑賞_喜び")
									{
										faceIndex2 = 0;
									}
									else if (defaultFace2 == "鑑賞_怒り")
									{
										faceIndex2 = 1;
									}
									else if (defaultFace2 == "鑑賞_哀しみ")
									{
										faceIndex2 = 2;
									}
									else if (defaultFace2 == "鑑賞_楽しい")
									{
										faceIndex2 = 3;
									}
								}
								UIButtonElement element = this.CreateMediaInfoButton2(this.m_UIPrefabMusicButton.gameObject, musicTitle, musicName, faceIndex2);
								for (int k = 0; k < element.buttonFaces.Length; k++)
								{
									element.buttonFaces[k].GetComponent<Graphic>().raycastTarget = false;
									if (k != faceIndex2)
									{
										element.buttonFaces[k].gameObject.SetActive(false);
									}
								}
								yield return null;
							}
						}
					}
				}
			}
			this.LoadAllMusicFile(this.m_MediaFilePath + "music_vrcom/", new string[]
			{
				"*.wav",
				"*.mp3"
			});
		}
		Resources.UnloadUnusedAssets();
		yield break;
	}

	public UIButtonElement CreateMediaInfoButton(GameObject buttonPrefab, string buttonName, string mediaPath, int faceIndex, MediaPlayerEvent.EventType successType)
	{
		UIButtonElement data = null;
		base.StartCoroutine(this.Coroutine_GetMovieInfo(mediaPath, successType, delegate(bool isSuccess, IMediaInfo info)
		{
			if (!isSuccess)
			{
				return;
			}
			Transform transform = null;
			for (int i = 0; i < this.m_UIContent.childCount; i++)
			{
				if (!this.m_UIContent.GetChild(i).gameObject.activeSelf)
				{
					transform = this.m_UIContent.GetChild(i);
					break;
				}
			}
			if (transform == null)
			{
				transform = UnityEngine.Object.Instantiate<GameObject>(buttonPrefab).transform;
			}
			transform.gameObject.SetActive(true);
			UIButtonElement element = transform.GetComponent<UIButtonElement>();
			transform.SetParent(this.m_UIContent, false);
			int num = 0;
			for (int j = 0; j < this.m_UIContent.childCount; j++)
			{
				if (this.m_UIContent.GetChild(j).gameObject.activeSelf)
				{
					num++;
				}
			}
			element.textContentNumber.text = num.ToString("000");
			Text textContentName = element.textContentName;
			TextGenerator textGenerator = new TextGenerator();
			TextGenerationSettings generationSettings = textContentName.GetGenerationSettings(textContentName.rectTransform.rect.size);
			generationSettings.resizeTextMinSize = textContentName.resizeTextMinSize;
			generationSettings.resizeTextMaxSize = textContentName.resizeTextMaxSize;
			generationSettings.fontSize = textContentName.fontSize;
			generationSettings.font = textContentName.font;
			buttonName = buttonName.Replace(' ', '\u00a0');
			textGenerator.Populate(buttonName, generationSettings);
			int characterCountVisible = textGenerator.characterCountVisible;
			string text = buttonName;
			if (buttonName.Length > characterCountVisible && characterCountVisible > 3)
			{
				Debug.Log(characterCountVisible + "\n" + buttonName.Length);
				text = buttonName.Substring(0, characterCountVisible - 3);
				text += "...";
			}
			textContentName.text = text;
			element.ButtonEvent_ChangeFaceType(faceIndex);
			for (int k = 0; k < element.buttonFaces.Length; k++)
			{
				element.buttonFaces[k].GetComponent<Graphic>().raycastTarget = true;
				element.buttonFaces[k].gameObject.SetActive(true);
			}
			element.buttonStart.onClick.RemoveAllListeners();
			element.buttonStart.onClick.AddListener(delegate()
			{
				if (VRExternalFileLoader.m_LoadingCount > 0)
				{
					return;
				}
				if (!File.Exists(mediaPath))
				{
					return;
				}
				if (<CreateMediaInfoButton>c__AnonStoreyA.m_MediaPlayer == null)
				{
					if (VRExternalFileLoader.m_PtrMediaPlayerObject == null)
					{
						VRExternalFileLoader.m_PtrMediaPlayerObject = (UnityEngine.Object.Instantiate(Resources.Load("SceneVRCommunication\\Tablet\\Media Outputer (Music And Movie)")) as GameObject);
					}
					GameObject[] array = GameObject.FindGameObjectsWithTag("MediaPlayerObj");
					for (int l = 0; l < array.Length; l++)
					{
						if (<CreateMediaInfoButton>c__AnonStoreyA.m_MediaPlayer != null && <CreateMediaInfoButton>c__AnonStoreyA.m_MovieOutputer != null)
						{
							break;
						}
						if (<CreateMediaInfoButton>c__AnonStoreyA.m_MediaPlayer == null)
						{
							<CreateMediaInfoButton>c__AnonStoreyA.m_MediaPlayer = array[l].GetComponent<MediaPlayer>();
						}
						if (<CreateMediaInfoButton>c__AnonStoreyA.m_MovieOutputer == null)
						{
							<CreateMediaInfoButton>c__AnonStoreyA.m_MovieOutputer = array[l].GetComponent<ApplyToMaterial>();
						}
					}
				}
				GameMain.Instance.SoundMgr.StopBGM(0.5f);
				<CreateMediaInfoButton>c__AnonStoreyA.m_MediaPlayer.CloseVideo();
				<CreateMediaInfoButton>c__AnonStoreyA.m_MediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.AbsolutePathOrURL, mediaPath, true);
				if (element.imageThumbnail)
				{
					<CreateMediaInfoButton>c__AnonStoreyA.m_MovieOutputer.GetComponent<MeshRenderer>().enabled = true;
				}
				string text3 = "*鑑賞_";
				if (element.faceIndex == 0)
				{
					text3 += "喜び";
				}
				else if (element.faceIndex == 1)
				{
					text3 += "怒り";
				}
				else if (element.faceIndex == 2)
				{
					text3 += "哀しみ";
				}
				else if (element.faceIndex == 3)
				{
					text3 += "楽しい";
				}
				Maid maid = GameMain.Instance.CharacterMgr.GetMaid(0);
				GameMain.Instance.ScriptMgr.LoadAdvScenarioScript(((!(maid == null)) ? ScriptManager.ReplacePersonal(maid, "?_movieface_main") : "?_movieface_main.ks") + ".ks", text3);
				GameMain.Instance.ScriptMgr.adv_kag.Exec();
			});
			if (element.imageThumbnail)
			{
				Texture2D texture2D = new Texture2D(info.GetVideoWidth(), info.GetVideoHeight(), TextureFormat.ARGB32, false);
				this.m_MediaPlayer.ExtractFrame(texture2D, 0.5f, false, 50);
				if (texture2D)
				{
					Rect rect = new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height);
					element.imageThumbnail.sprite = Sprite.Create(texture2D, rect, Vector2.zero, 100f, 0u, SpriteMeshType.FullRect);
				}
			}
			float num2 = info.GetDurationMs();
			num2 *= 0.001f;
			string text2 = "再生時間 " + Mathf.Floor(num2 / 60f).ToString("00") + ":" + Mathf.Repeat(num2, 60f).ToString("00");
			element.textContentTime.text = text2;
			VRExternalFileLoader.m_IsEndLoad = true;
			VRExternalFileLoader.m_LoadingCount--;
			this.m_MediaPlayer.CloseVideo();
			this.m_MovieOutputer.enabled = true;
			data = element;
		}));
		return data;
	}

	public void CreateMediaInfoButton3(GameObject buttonPrefab, string buttonName, string mediaPath, string thumbnailPath, int faceIndex, MediaPlayerEvent.EventType successType)
	{
		base.StartCoroutine(this.Coroutine_GetMovieInfo(mediaPath, successType, delegate(bool isSuccess, IMediaInfo info)
		{
			if (!isSuccess)
			{
				return;
			}
			Transform transform = null;
			for (int i = 0; i < this.m_UIContent.childCount; i++)
			{
				if (!this.m_UIContent.GetChild(i).gameObject.activeSelf)
				{
					transform = this.m_UIContent.GetChild(i);
					break;
				}
			}
			if (transform == null)
			{
				transform = UnityEngine.Object.Instantiate<GameObject>(buttonPrefab).transform;
			}
			transform.gameObject.SetActive(true);
			UIButtonElement element = transform.GetComponent<UIButtonElement>();
			transform.SetParent(this.m_UIContent, false);
			int num = 0;
			for (int j = 0; j < this.m_UIContent.childCount; j++)
			{
				if (this.m_UIContent.GetChild(j).gameObject.activeSelf)
				{
					num++;
				}
			}
			element.textContentNumber.text = num.ToString("000");
			element.textContentName.text = buttonName;
			element.ButtonEvent_ChangeFaceType(faceIndex);
			for (int k = 0; k < element.buttonFaces.Length; k++)
			{
				element.buttonFaces[k].GetComponent<Graphic>().raycastTarget = false;
				if (k != faceIndex)
				{
					element.buttonFaces[k].gameObject.SetActive(false);
				}
			}
			element.buttonStart.onClick.RemoveAllListeners();
			element.buttonStart.onClick.AddListener(delegate()
			{
				if (VRExternalFileLoader.m_LoadingCount > 0)
				{
					return;
				}
				if (!File.Exists(mediaPath))
				{
					return;
				}
				if (<CreateMediaInfoButton3>c__AnonStoreyC.m_MediaPlayer == null)
				{
					if (VRExternalFileLoader.m_PtrMediaPlayerObject == null)
					{
						VRExternalFileLoader.m_PtrMediaPlayerObject = (UnityEngine.Object.Instantiate(Resources.Load("SceneVRCommunication\\Tablet\\Media Outputer (Music And Movie)")) as GameObject);
					}
					GameObject[] array = GameObject.FindGameObjectsWithTag("MediaPlayerObj");
					for (int l = 0; l < array.Length; l++)
					{
						if (<CreateMediaInfoButton3>c__AnonStoreyC.m_MediaPlayer != null && <CreateMediaInfoButton3>c__AnonStoreyC.m_MovieOutputer != null)
						{
							break;
						}
						if (<CreateMediaInfoButton3>c__AnonStoreyC.m_MediaPlayer == null)
						{
							<CreateMediaInfoButton3>c__AnonStoreyC.m_MediaPlayer = array[l].GetComponent<MediaPlayer>();
						}
						if (<CreateMediaInfoButton3>c__AnonStoreyC.m_MovieOutputer == null)
						{
							<CreateMediaInfoButton3>c__AnonStoreyC.m_MovieOutputer = array[l].GetComponent<ApplyToMaterial>();
						}
					}
				}
				GameMain.Instance.SoundMgr.StopBGM(0.5f);
				<CreateMediaInfoButton3>c__AnonStoreyC.m_MediaPlayer.CloseVideo();
				<CreateMediaInfoButton3>c__AnonStoreyC.m_MediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.AbsolutePathOrURL, mediaPath, true);
				if (element.imageThumbnail)
				{
					<CreateMediaInfoButton3>c__AnonStoreyC.m_MovieOutputer.GetComponent<MeshRenderer>().enabled = true;
				}
				string text2 = "*鑑賞_";
				if (element.faceIndex == 0)
				{
					text2 += "喜び";
				}
				else if (element.faceIndex == 1)
				{
					text2 += "怒り";
				}
				else if (element.faceIndex == 2)
				{
					text2 += "哀しみ";
				}
				else if (element.faceIndex == 3)
				{
					text2 += "楽しい";
				}
				Maid maid = GameMain.Instance.CharacterMgr.GetMaid(0);
				GameMain.Instance.ScriptMgr.LoadAdvScenarioScript(((!(maid == null)) ? ScriptManager.ReplacePersonal(maid, "?_movieface_main") : "?_movieface_main.ks") + ".ks", text2);
				GameMain.Instance.ScriptMgr.adv_kag.Exec();
			});
			if (element.imageThumbnail)
			{
				this.StartCoroutine(this.Coroutine_Load<Sprite>("SceneVRCommunication/Tablet/Sprite/" + thumbnailPath, delegate(ResourceRequest req)
				{
					element.imageThumbnail.sprite = (req.asset as Sprite);
				}));
			}
			float num2 = info.GetDurationMs();
			num2 *= 0.001f;
			string text = "再生時間 " + Mathf.Floor(num2 / 60f).ToString("00") + ":" + Mathf.Repeat(num2, 60f).ToString("00");
			element.textContentTime.text = text;
			VRExternalFileLoader.m_IsEndLoad = true;
			VRExternalFileLoader.m_LoadingCount--;
			this.m_MediaPlayer.CloseVideo();
			this.m_MovieOutputer.enabled = true;
		}));
	}

	public UIButtonElement CreateMediaInfoButton2(GameObject buttonPrefab, string buttonName, string mediaName, int faceIndex)
	{
		Transform transform = null;
		for (int i = 0; i < this.m_UIContent.childCount; i++)
		{
			if (!this.m_UIContent.GetChild(i).gameObject.activeSelf)
			{
				transform = this.m_UIContent.GetChild(i);
				break;
			}
		}
		if (transform == null)
		{
			transform = UnityEngine.Object.Instantiate<GameObject>(buttonPrefab).transform;
		}
		transform.gameObject.SetActive(true);
		UIButtonElement element = transform.GetComponent<UIButtonElement>();
		int num = 0;
		for (int j = 0; j < this.m_UIContent.childCount; j++)
		{
			if (this.m_UIContent.GetChild(j).gameObject.activeSelf)
			{
				num++;
			}
		}
		element.textContentNumber.text = num.ToString("000");
		element.textContentName.text = buttonName;
		Button buttonStart = element.buttonStart;
		if (buttonStart)
		{
			buttonStart.onClick.RemoveAllListeners();
			buttonStart.onClick.AddListener(delegate()
			{
				if (this.m_MovieOutputer)
				{
					this.m_MovieOutputer.GetComponent<MeshRenderer>().enabled = false;
				}
				this.m_MediaPlayer.CloseVideo();
				GameMain.Instance.SoundMgr.PlayBGM(mediaName, 0f, true);
				string text = "*鑑賞_";
				if (element.faceIndex == 0)
				{
					text += "喜び";
				}
				else if (element.faceIndex == 1)
				{
					text += "怒り";
				}
				else if (element.faceIndex == 2)
				{
					text += "哀しみ";
				}
				else if (element.faceIndex == 3)
				{
					text += "楽しい";
				}
				Debug.Log("外部スクリプト名：?_movieface_main.ks\nラベル文字列：" + text);
				Maid maid = GameMain.Instance.CharacterMgr.GetMaid(0);
				GameMain.Instance.ScriptMgr.LoadAdvScenarioScript(((!(maid == null)) ? ScriptManager.ReplacePersonal(maid, "?_movieface_main") : "?_movieface_main.ks") + ".ks", text);
				GameMain.Instance.ScriptMgr.adv_kag.Exec();
			});
		}
		element.ButtonEvent_ChangeFaceType(faceIndex);
		for (int k = 0; k < element.buttonFaces.Length; k++)
		{
			element.buttonFaces[k].GetComponent<Graphic>().raycastTarget = true;
			element.buttonFaces[k].gameObject.SetActive(true);
		}
		transform.SetParent(this.m_UIContent, false);
		return element;
	}

	private void GetBGMName(int id, ref string BGMTitle, ref string BGMFileName)
	{
		string text = "phot_sound_list.nei";
		if (!GameUty.FileSystem.IsExistentFile(text))
		{
			NDebug.Assert("表がありません。" + text, false);
		}
		using (AFileBase afileBase = GameUty.FileSystem.FileOpen(text))
		{
			using (CsvParser csvParser = new CsvParser())
			{
				bool condition = csvParser.Open(afileBase);
				NDebug.Assert(condition, text + "\nopen failed.");
				for (int i = 1; i < csvParser.max_cell_y; i++)
				{
					if (csvParser.IsCellToExistData(0, i))
					{
						if (csvParser.GetCellAsInteger(0, i) == id)
						{
							BGMTitle = csvParser.GetCellAsString(1, i);
							BGMFileName = csvParser.GetCellAsString(2, i);
							break;
						}
					}
				}
			}
		}
	}

	private void SaveAndPlay(string MediaName, UnityAction callback)
	{
		string mediaFilePath = this.m_MediaFilePath;
		if (GameUty.FileSystem.IsExistentFile(MediaName))
		{
			using (AFileBase afileBase = GameUty.FileOpen(MediaName, null))
			{
				if (afileBase != null && afileBase.IsValid())
				{
					byte[] bytes = afileBase.ReadAll();
					File.WriteAllBytes(mediaFilePath + "\\b.bgm", bytes);
					callback();
				}
			}
		}
	}

	public static void Event_PlayMovie(string movieName)
	{
		if (VRExternalFileLoader.m_PtrMediaPlayerObject == null)
		{
			VRExternalFileLoader.m_PtrMediaPlayerObject = (UnityEngine.Object.Instantiate(Resources.Load("SceneVRCommunication\\Tablet\\Media Outputer (Music And Movie)")) as GameObject);
		}
		GameObject[] array = GameObject.FindGameObjectsWithTag("MediaPlayerObj");
		MediaPlayer mediaPlayer = null;
		ApplyToMaterial applyToMaterial = null;
		for (int i = 0; i < array.Length; i++)
		{
			if (mediaPlayer != null && applyToMaterial != null)
			{
				break;
			}
			if (mediaPlayer == null)
			{
				mediaPlayer = array[i].GetComponent<MediaPlayer>();
			}
			if (applyToMaterial == null)
			{
				applyToMaterial = array[i].GetComponent<ApplyToMaterial>();
			}
		}
		if (mediaPlayer == null)
		{
			Debug.LogError("[ＶＲ動画再生]\u3000MediaPlayer を持ったオブジェクトが見つかりません。");
			return;
		}
		if (applyToMaterial == null)
		{
			Debug.LogError("[ＶＲ動画再生]\u3000ApplyToMaterial を持ったオブジェクトが見つかりません。");
			return;
		}
		mediaPlayer.CloseVideo();
		applyToMaterial.GetComponent<MeshRenderer>().enabled = true;
		string path = Path.GetFullPath(".\\") + "GameData/movie_vrcom/" + Path.ChangeExtension(movieName, ".ine");
		NDebug.Assert(File.Exists(path), "動画「" + movieName + "」が見つかりません");
		mediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.AbsolutePathOrURL, path, true);
	}

	public static void Event_CloseMovie()
	{
		GameObject[] array = GameObject.FindGameObjectsWithTag("MediaPlayerObj");
		for (int i = 0; i < array.Length; i++)
		{
			ApplyToMaterial component = array[i].GetComponent<ApplyToMaterial>();
			if (component)
			{
				MeshRenderer component2 = component.GetComponent<MeshRenderer>();
				if (component2)
				{
					component2.enabled = false;
				}
			}
			MediaPlayer component3 = array[i].GetComponent<MediaPlayer>();
			if (component3)
			{
				component3.CloseVideo();
			}
		}
		if (VRExternalFileLoader.m_PtrMediaPlayerObject)
		{
			UnityEngine.Object.Destroy(VRExternalFileLoader.m_PtrMediaPlayerObject);
		}
	}

	public static void Event_OpenSelect()
	{
		VRCanvasManager.Instance.ClearCanvasStack();
		VRDialogMenu vrdialogMenu = VRDialogMenu.CreateDialog();
		List<VRDialogMenu.DialogButton> list = new List<VRDialogMenu.DialogButton>();
		list.Add(new VRDialogMenu.DialogButton(true, "vr_dialog_cd", delegate()
		{
			VRExternalFileLoader.m_MediaNowType = VRExternalFileLoader.TYPE_MEDIA.Music;
			VRCanvasManager.Instance.OpenVRCanvas(VRCanvasManager.VRCanvasType.MusicPlayer);
		}, new Vector3(250f, -270f, 0f), new Vector2(300f, 300f)));
		list.Add(new VRDialogMenu.DialogButton(true, "vr_dialog_tv", delegate()
		{
			VRExternalFileLoader.m_MediaNowType = VRExternalFileLoader.TYPE_MEDIA.Movie;
			VRCanvasManager.Instance.OpenVRCanvas(VRCanvasManager.VRCanvasType.MoviePlayer);
		}, new Vector3(-250f, -270f, 0f), new Vector2(300f, 300f)));
		list.Add(new VRDialogMenu.DialogButton(true, "vr_bottom_system_return", delegate()
		{
			string label_name = "*鑑賞_終了";
			Maid maid = GameMain.Instance.CharacterMgr.GetMaid(0);
			GameMain.Instance.ScriptMgr.LoadAdvScenarioScript(((!(maid == null)) ? ScriptManager.ReplacePersonal(maid, "?_movieface_main") : "?_movieface_main.ks") + ".ks", label_name);
			GameMain.Instance.ScriptMgr.adv_kag.Exec();
			VRCanvasManager.Instance.OpenVRCanvas(VRCanvasManager.VRCanvasType.MainMenu);
		}, new Vector3(-778f, -365f, 0f), new Vector2(200f, 200f)));
		vrdialogMenu.OpenDialog("再生機器を選んでください。", list.ToArray());
		if (SceneVRCommunication.Instance.GetNowTime() == SceneVRCommunication.VR_TIME.NIGHT)
		{
			GameMain.Instance.SoundMgr.PlayBGM("BGM_vr0001.ogg", 1f, true);
		}
		else
		{
			GameMain.Instance.SoundMgr.PlayBGM("BGM007.ogg", 1f, true);
		}
	}

	public void ScrollEvent_ContentScroll(Vector2 normalizedPos)
	{
		this.m_SliderBar.value = normalizedPos.y;
	}

	public void ButtonEvent_CloseVideo()
	{
		if (this.m_MovieOutputer)
		{
			this.m_MovieOutputer.GetComponent<MeshRenderer>().enabled = false;
		}
		if (this.m_MediaPlayer)
		{
			this.m_MediaPlayer.CloseVideo();
		}
		GameMain.Instance.SoundMgr.StopBGM(0f);
	}

	public void ButtonEvent_CloseCanvas()
	{
		base.StopAllCoroutines();
		UICanvasFade component = base.GetComponent<UICanvasFade>();
		if (component)
		{
			component.FadeOut();
		}
		if (VRExternalFileLoader.m_PtrMediaPlayerObject)
		{
			UnityEngine.Object.Destroy(VRExternalFileLoader.m_PtrMediaPlayerObject);
		}
		VRExternalFileLoader.Event_OpenSelect();
	}

	private void OnEnable()
	{
		if (VRExternalFileLoader.m_MediaNowType != this.m_TypeMedia)
		{
			return;
		}
		base.StartCoroutine(this.Coroutine_Refresh());
	}

	private void OnDisable()
	{
		for (int i = 0; i < this.m_UIContent.childCount; i++)
		{
			UnityEngine.Object.Destroy(this.m_UIContent.GetChild(i).gameObject);
		}
	}

	private IEnumerator Coroutine_Load<T>(string path, UnityAction<ResourceRequest> callback) where T : UnityEngine.Object
	{
		ResourceRequest res = Resources.LoadAsync<T>(path);
		while (!res.isDone)
		{
			yield return null;
		}
		callback(res);
		yield break;
	}

	[SerializeField]
	private RectTransform m_UIContent;

	[SerializeField]
	private Slider m_SliderBar;

	[SerializeField]
	private RectTransform m_UIPrefabMovieButton;

	[SerializeField]
	private RectTransform m_UIPrefabMusicButton;

	[SerializeField]
	private MediaPlayer m_MediaPlayer;

	[SerializeField]
	private ApplyToMaterial m_MovieOutputer;

	[SerializeField]
	[Tooltip("外部ファイルへのパス。「～/Movie/」など、\nファイルが置いてあるパスを指定する。")]
	private string m_MediaFilePath;

	[SerializeField]
	[Space(16f)]
	private VRExternalFileLoader.TYPE_MEDIA m_TypeMedia;

	public static GameObject m_PtrMediaPlayerObject;

	private bool m_BeforeFreeMode;

	private static bool m_IsEndLoad = true;

	private static int m_LoadingCount;

	private static VRExternalFileLoader.TYPE_MEDIA m_MediaNowType = VRExternalFileLoader.TYPE_MEDIA.Movie;

	private enum TYPE_MEDIA
	{
		Music,
		Movie
	}
}
