using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using wf;

public class TextureSelectSubWindow : BasePhotoSubWindow
{
	public static string folder_path
	{
		get
		{
			string text = PhotoWindowManager.path_photo_folder + "Texture";
			if (!Directory.Exists(text))
			{
				Directory.CreateDirectory(text);
			}
			return text;
		}
	}

	public void Awake()
	{
	}

	public override void Initizalize(BasePhotoWindow parentWindow)
	{
		this.Awake();
		base.Initizalize(parentWindow);
		this.ReLoad();
	}

	public void ReLoad()
	{
		List<Transform> childList = this.gridObject.GetChildList();
		for (int i = 0; i < childList.Count; i++)
		{
			UnityEngine.Object.DestroyImmediate(childList[i].gameObject);
		}
		this.imageFilePaths.Clear();
		if (Directory.Exists(TextureSelectSubWindow.folder_path))
		{
			List<string> list = new List<string>();
			foreach (string searchPattern in new string[]
			{
				"*.png",
				"*jpg"
			})
			{
				list.AddRange(Directory.GetFiles(TextureSelectSubWindow.folder_path, searchPattern));
			}
			foreach (string text in list)
			{
				Sprite sprite = Utility.CreateTextureSpriteFromImageFile(text);
				if (!(sprite == null))
				{
					GameObject gameObject = Utility.CreatePrefab(this.gridObject.gameObject, "ScenePhotoMode/TexSelectBtn", true);
					UI2DSprite componentInChildren = gameObject.GetComponentInChildren<UI2DSprite>();
					componentInChildren.sprite2D = sprite;
					UIButton componentInChildren2 = gameObject.GetComponentInChildren<UIButton>();
					EventDelegate.Add(componentInChildren2.onClick, new EventDelegate.Callback(this.OnClickItem));
					UIEventTrigger componentInChildren3 = gameObject.GetComponentInChildren<UIEventTrigger>();
					componentInChildren3.onPress = this.copyEventTrigger.onPress;
					this.imageFilePaths.Add(componentInChildren2, text);
				}
			}
		}
		this.warningTextObject.SetActive(this.imageFilePaths.Count == 0);
		Utility.ResetNGUI(this.gridObject);
		Utility.ResetNGUI(this.gridObject.transform.parent.gameObject.GetComponent<UIScrollView>());
	}

	public void OnDestroy()
	{
		foreach (KeyValuePair<UIButton, string> keyValuePair in this.imageFilePaths)
		{
			UI2DSprite componentInChildren = keyValuePair.Key.GetComponentInChildren<UI2DSprite>();
			if (componentInChildren != null && componentInChildren.sprite2D != null && componentInChildren.sprite2D.texture != null)
			{
				UnityEngine.Object.Destroy(componentInChildren.sprite2D.texture);
			}
		}
	}

	private void OnClickItem()
	{
		if (UIButton.current == null || !this.imageFilePaths.ContainsKey(UIButton.current))
		{
			return;
		}
		this.objectCreateWindow.OnChangeSelectTexture(this.imageFilePaths[UIButton.current]);
	}

	public new PhotoWindowManager mgr
	{
		get
		{
			return base.mgr as PhotoWindowManager;
		}
	}

	[SerializeField]
	private ObjectCreateWindow objectCreateWindow;

	[SerializeField]
	private UIGrid gridObject;

	[SerializeField]
	private GameObject warningTextObject;

	[SerializeField]
	private UIEventTrigger copyEventTrigger;

	private Dictionary<UIButton, string> imageFilePaths = new Dictionary<UIButton, string>();
}
