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

namespace SceneEditWindow
{
	[AddComponentMenu("SceneEditWindow/CustomViewWindow")]
	public class CustomViewWindow : BasePhotoWindow
	{
		public override string windowName
		{
			get
			{
				return "CustomViewWindow";
			}
		}

		public override void Awake()
		{
			base.Awake();
			CustomViewItemData.Create();
			UIWFTabButton[] componentsInChildren = this.tabPanel.GetComponentsInChildren<UIWFTabButton>();
			foreach (UIWFTabButton uiwftabButton in componentsInChildren)
			{
				EventDelegate.Add(uiwftabButton.onSelect, new EventDelegate.Callback(this.OnClickSelectCategory));
			}
			this.tabPanel.UpdateChildren();
		}

		private void OnDestroy()
		{
			foreach (KeyValuePair<string, Texture2D> keyValuePair in this.texDic)
			{
				UnityEngine.Object.DestroyImmediate(keyValuePair.Value);
			}
			this.texDic.Clear();
		}

		public void Create()
		{
			if (this.itemList.Count != 0)
			{
				return;
			}
			foreach (CustomViewItemData.ItemData itemData in CustomViewItemData.itemList)
			{
				if (!this.texDic.ContainsKey(itemData.iconTexName))
				{
					this.texDic.Add(itemData.iconTexName, ImportCM.CreateTexture(itemData.iconTexName));
				}
				CustomViewItem componentInChildren = Utility.CreatePrefab(this.pageItemGrits[itemData.page].gameObject, "SceneEdit/WindowParts/CustomItem", true).GetComponentInChildren<CustomViewItem>();
				componentInChildren.sceneEdit = this.sceneEdit;
				componentInChildren.mpn = itemData.mpn;
				componentInChildren.defaultIconTexture = this.texDic[itemData.iconTexName];
				componentInChildren.GetComponentInChildren<UIWidget>().depth = 15;
				this.itemList.Add(componentInChildren);
			}
			foreach (UIGrid grid in this.pageItemGrits)
			{
				Utility.ResetNGUI(grid);
			}
			this.UpdateAllItem();
			this.tabPanel.Select(this.tabPanel.GetComponentInChildren<UIWFTabButton>());
			this.UpdateChildren();
		}

		public bool UpdateAllItem()
		{
			bool flag = false;
			foreach (CustomViewItem customViewItem in this.itemList)
			{
				flag |= customViewItem.UpdateIcon(null);
			}
			return flag;
		}

		private void OnClickSelectCategory()
		{
			if (!UIWFSelectButton.selected)
			{
				return;
			}
			UIWFTabButton[] componentsInChildren = this.tabPanel.GetComponentsInChildren<UIWFTabButton>();
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				if (componentsInChildren[i] == UIWFSelectButton.current)
				{
					for (int j = 0; j < this.pageItemGrits.Length; j++)
					{
						this.pageItemGrits[j].transform.localScale = ((j != i) ? Vector3.zero : Vector3.one);
					}
					break;
				}
			}
		}

		[SerializeField]
		public SceneEdit sceneEdit;

		[SerializeField]
		private UIWFTabPanel tabPanel;

		[SerializeField]
		private UIGrid[] pageItemGrits;

		private List<CustomViewItem> itemList = new List<CustomViewItem>();

		private Dictionary<string, Texture2D> texDic = new Dictionary<string, Texture2D>();
	}
}
