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

namespace SceneEditWindow
{
	[AddComponentMenu("SceneEditWindow/PauseIconWindow")]
	public class PoseIconWindow : BaseIconWindow
	{
		public int selectedIconId { get; private set; }

		public override string windowName
		{
			get
			{
				return "PauseIconWindow";
			}
		}

		public override void Awake()
		{
			PoseIconData.Create();
			base.Awake();
		}

		protected virtual void OnDestroy()
		{
			foreach (KeyValuePair<int, Texture> keyValuePair in this.texDic)
			{
				UnityEngine.Object.DestroyImmediate(keyValuePair.Value);
			}
			this.texDic.Clear();
		}

		public void Exec(int poseId)
		{
			if (!this.tabButtonDic.ContainsKey(poseId))
			{
				Debug.LogError("ポーズ番号[" + poseId.ToString() + "]のポーズが見つかりませんでした");
				return;
			}
			this.tabPanel.Select(this.tabButtonDic[poseId]);
		}

		public void NextPose()
		{
			if (this.createItemList.Count <= 1 || this.selectedIconId < 0)
			{
				return;
			}
			int num = int.MinValue;
			for (int i = 0; i < this.createItemList.Count; i++)
			{
				if (this.createItemList[i].id == this.selectedIconId)
				{
					num = this.createItemList[0].id;
					if (i + 1 < this.createItemList.Count)
					{
						num = this.createItemList[i + 1].id;
					}
					break;
				}
			}
			if (num != -2147483648)
			{
				this.Exec(num);
			}
		}

		public void PrevPose()
		{
			if (this.createItemList.Count <= 1 || this.selectedIconId < 0)
			{
				return;
			}
			int num = int.MinValue;
			for (int i = 0; i < this.createItemList.Count; i++)
			{
				if (this.createItemList[i].id == this.selectedIconId)
				{
					num = this.createItemList[this.createItemList.Count - 1].id;
					if (0 <= i - 1)
					{
						num = this.createItemList[i - 1].id;
					}
					break;
				}
			}
			if (num != -2147483648)
			{
				this.Exec(num);
			}
		}

		public void ReleaseSelect()
		{
			UIWFTabButton selectButtonObject = this.tabPanel.GetSelectButtonObject();
			if (selectButtonObject != null)
			{
				selectButtonObject.SetSelect(false);
				this.tabPanel.ResetSelect();
			}
		}

		protected override void OnSelectItem(int selectItemId)
		{
			PoseIconData.ItemData itemData = PoseIconData.GetItemData(selectItemId);
			this.selectedIconId = itemData.id;
			if (this.onSelectPauseEvent != null)
			{
				this.onSelectPauseEvent(itemData);
			}
		}

		protected override UIWFTabButton CreateItemObject(GameObject parent, int createItemId)
		{
			GameObject gameObject = Utility.CreatePrefab(parent, "SceneEdit/WindowParts/PoseIconItem", true);
			if (!this.texDic.ContainsKey(createItemId))
			{
				PoseIconData.ItemData itemData = PoseIconData.GetItemData(createItemId);
				this.texDic.Add(createItemId, ImportCM.CreateTexture(itemData.fileSystem, itemData.iconTexName));
			}
			UITexture component = gameObject.GetComponent<UITexture>();
			component.mainTexture = this.texDic[createItemId];
			this.createItemList.Add(PoseIconData.GetItemData(createItemId));
			return gameObject.GetComponent<UIWFTabButton>();
		}

		protected override List<int> CreateItemIdList()
		{
			List<int> list = new List<int>();
			foreach (PoseIconData.ItemData itemData in PoseIconData.itemList)
			{
				list.Add(itemData.id);
			}
			return list;
		}

		public Action<PoseIconData.ItemData> onSelectPauseEvent;

		private Dictionary<int, Texture> texDic = new Dictionary<int, Texture>();

		private List<PoseIconData.ItemData> createItemList = new List<PoseIconData.ItemData>();
	}
}
