using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class VRPhotoMenu : MonoBehaviour
{
	private void Update()
	{
		this.m_TotalTime += Time.deltaTime;
		if (this.m_RefreshTime < this.m_TotalTime)
		{
			this.m_TotalTime = 0f;
			if (this.m_ScreenShotCount != this.m_BeforeScreenShotCount)
			{
				this.Refresh();
			}
			this.m_BeforeScreenShotCount = this.m_ScreenShotCount;
		}
	}

	public static int CompareLastWriteTime(FileInfo file1, FileInfo file2)
	{
		return DateTime.Compare(file2.LastWriteTime, file1.LastWriteTime);
	}

	public void Refresh()
	{
		this.m_ScreenShotPath = Path.GetFullPath(".\\") + "ScreenShot\\VRPhotoThumb\\";
		string screenShotPath = this.m_ScreenShotPath;
		DirectoryInfo directoryInfo = new DirectoryInfo(screenShotPath);
		if (!directoryInfo.Exists)
		{
			this.m_ImagePhoto.gameObject.SetActive(false);
			this.m_ImagePhoto.sprite = null;
			return;
		}
		FileInfo[] files = directoryInfo.GetFiles("*", SearchOption.TopDirectoryOnly);
		FileInfo[] array = files;
		if (VRPhotoMenu.<>f__mg$cache0 == null)
		{
			VRPhotoMenu.<>f__mg$cache0 = new Comparison<FileInfo>(VRPhotoMenu.CompareLastWriteTime);
		}
		Array.Sort<FileInfo>(array, VRPhotoMenu.<>f__mg$cache0);
		this.m_FileInfoArray.Clear();
		for (int i = 0; i < files.Length; i++)
		{
			this.m_FileInfoArray.Add(files[i]);
		}
		int screenShotCount = this.m_ScreenShotCount;
		this.m_ScreenShotCount = files.Length;
		this.ButtonEvent_ChangePhoto(this.m_ScreenShotCount - screenShotCount);
	}

	public void ButtonEvent_ChangePhoto(int index)
	{
		if (this.m_FileInfoArray.Count <= 0)
		{
			this.m_ImagePhoto.sprite = null;
			this.m_ImagePhoto.gameObject.SetActive(false);
			this.m_TextPageNumber.text = "000/000";
			return;
		}
		this.m_ScreenShotIndex += index;
		this.m_ScreenShotIndex = ((this.m_ScreenShotIndex >= 0) ? this.m_ScreenShotIndex : (this.m_ScreenShotCount - 1));
		this.m_ScreenShotIndex = ((this.m_ScreenShotIndex < this.m_ScreenShotCount) ? this.m_ScreenShotIndex : 0);
		this.m_TextPageNumber.text = (this.m_ScreenShotIndex + 1).ToString("000") + " / " + this.m_ScreenShotCount.ToString("000");
		FileInfo fileInfo = this.m_FileInfoArray[this.m_ScreenShotIndex];
		base.StopAllCoroutines();
		base.StartCoroutine(this.Coroutine_LoadSprite("File://" + fileInfo.FullName, delegate(WWW www)
		{
			if (!string.IsNullOrEmpty(www.error))
			{
				this.m_ImagePhoto.sprite = null;
				this.m_ImagePhoto.gameObject.SetActive(false);
				return;
			}
			www.Dispose();
			www = null;
			this.m_ImagePhoto.gameObject.SetActive(true);
		}));
	}

	private IEnumerator Coroutine_LoadSprite(string url, UnityAction<WWW> callback)
	{
		WWW www = new WWW(url);
		yield return www;
		Texture2D tex = www.textureNonReadable;
		string rawFileName = Path.GetFileNameWithoutExtension(url);
		char[] fileNameCharArray = rawFileName.ToCharArray();
		char angle = fileNameCharArray[fileNameCharArray.Length - 1];
		RectTransform trans = this.m_ImagePhoto.GetComponent<RectTransform>();
		Rect rect = trans.rect;
		if (angle == 'd')
		{
			trans.localEulerAngles = Vector3.zero;
			trans.localScale = Vector3.one * 0.6f;
		}
		else if (angle == 'l')
		{
			trans.localEulerAngles = Vector3.forward * 90f;
			trans.localScale = Vector3.one;
		}
		else if (angle == 't')
		{
			trans.localEulerAngles = Vector3.forward * 180f;
			trans.localScale = Vector3.one * 0.6f;
		}
		else if (angle == 'r')
		{
			trans.localEulerAngles = Vector3.forward * 270f;
			trans.localScale = Vector3.one;
		}
		else
		{
			trans.localEulerAngles = Vector3.forward * 90f;
			trans.localScale = Vector3.one;
		}
		this.m_ImagePhoto.sprite = Sprite.Create(tex, new Rect(0f, 0f, (float)tex.width, (float)tex.height), Vector2.one * 0.5f, 1f, 0u, SpriteMeshType.FullRect);
		callback(www);
		yield break;
	}

	private void OnEnable()
	{
		this.Refresh();
		this.ButtonEvent_ChangePhoto(0);
	}

	private void OnDisable()
	{
		this.m_FileInfoArray.Clear();
	}

	private string m_ScreenShotPath = string.Empty;

	[SerializeField]
	private Image m_ImagePhoto;

	[SerializeField]
	private Text m_TextPageNumber;

	private int m_ScreenShotIndex;

	private int m_ScreenShotCount;

	private int m_BeforeScreenShotCount;

	private List<FileInfo> m_FileInfoArray = new List<FileInfo>();

	private float m_RefreshTime = 4f;

	private float m_TotalTime;

	[CompilerGenerated]
	private static Comparison<FileInfo> <>f__mg$cache0;
}
