using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace TriLib.Samples
{
	public class FileOpenDialog : MonoBehaviour
	{
		public static FileOpenDialog Instance { get; private set; }

		public string Title
		{
			get
			{
				return this._headerText.text;
			}
			set
			{
				this._headerText.text = value;
			}
		}

		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		private event FileOpenEventHandle OnFileOpen;

		public void ShowFileOpenDialog(FileOpenEventHandle onFileOpen)
		{
			this.OnFileOpen = onFileOpen;
			this.ReloadItemNames();
			this._fileLoaderRenderer.SetActive(true);
		}

		public void HideFileOpenDialog()
		{
			this.DestroyItems();
			this._fileLoaderRenderer.SetActive(false);
		}

		public void HandleEvent(ItemType itemType, string filename)
		{
			if (itemType != ItemType.ParentDirectory)
			{
				if (itemType != ItemType.Directory)
				{
					this.OnFileOpen(Path.Combine(this._directory, filename));
					this.HideFileOpenDialog();
				}
				else
				{
					this._directory = filename;
					this.ReloadItemNames();
				}
			}
			else
			{
				DirectoryInfo parent = Directory.GetParent(this._directory);
				if (parent != null)
				{
					this._directory = parent.FullName;
					this.ReloadItemNames();
				}
				else
				{
					this.ShowDirectoryNames();
				}
			}
		}

		public void DestroyItems()
		{
			IEnumerator enumerator = this._containerTransform.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					object obj = enumerator.Current;
					Transform transform = (Transform)obj;
					UnityEngine.Object.Destroy(transform.gameObject);
				}
			}
			finally
			{
				IDisposable disposable;
				if ((disposable = (enumerator as IDisposable)) != null)
				{
					disposable.Dispose();
				}
			}
		}

		protected void Awake()
		{
			this._directory = Path.GetDirectoryName(Application.dataPath);
			this._closeButton.onClick.AddListener(new UnityAction(this.HideFileOpenDialog));
			FileOpenDialog.Instance = this;
		}

		private void ReloadItemNames()
		{
			this.DestroyItems();
			this.CreateItem(ItemType.ParentDirectory, "[Parent Directory]");
			string[] directories = Directory.GetDirectories(this._directory);
			foreach (string text in directories)
			{
				this.CreateItem(ItemType.Directory, text);
			}
			string[] array2 = Directory.GetFiles(this._directory, "*.*");
			if (!string.IsNullOrEmpty(this.Filter) && this.Filter != "*.*")
			{
				array2 = (from x in array2
				where this.Filter.Contains(Path.GetExtension(x).ToLower())
				select x).ToArray<string>();
			}
			foreach (string path in array2)
			{
				this.CreateItem(ItemType.File, Path.GetFileName(path));
			}
		}

		private void ShowDirectoryNames()
		{
			this.DestroyItems();
			string[] logicalDrives = Directory.GetLogicalDrives();
			foreach (string text in logicalDrives)
			{
				this.CreateItem(ItemType.Directory, text);
			}
		}

		private void CreateItem(ItemType itemType, string text)
		{
			FileText fileText = UnityEngine.Object.Instantiate<FileText>(this._fileTextPrefab, this._containerTransform);
			fileText.ItemType = itemType;
			fileText.Text = text;
		}

		public string Filter = "*.*";

		[SerializeField]
		private Transform _containerTransform;

		[SerializeField]
		private FileText _fileTextPrefab;

		[SerializeField]
		private GameObject _fileLoaderRenderer;

		[SerializeField]
		private Button _closeButton;

		[SerializeField]
		private Text _headerText;

		private string _directory;
	}
}
