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

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

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

		public override void Awake()
		{
			BgIconData.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 bgId)
		{
			if (!this.tabButtonDic.ContainsKey(bgId))
			{
				Debug.LogError("背景番号[" + bgId.ToString() + "]の背景が見つかりませんでした");
				return;
			}
			this.tabPanel.Select(this.tabButtonDic[bgId]);
		}

		public void NextBG()
		{
			if (this.createItemList.Count <= 1)
			{
				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 PrevBG()
		{
			if (this.createItemList.Count <= 1)
			{
				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);
			}
		}

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

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

		protected override List<int> CreateItemIdList()
		{
			List<int> list = new List<int>();
			foreach (BgIconData.ItemData itemData in BgIconData.itemList)
			{
				if (!SceneEdit.compatibilityMode || itemData.isLegacyBG)
				{
					if (SceneEdit.compatibilityMode || !itemData.isLegacyBG)
					{
						if (itemData.enabled)
						{
							list.Add(itemData.id);
						}
					}
				}
			}
			return list;
		}

		public Action<BgIconData.ItemData> onSelectBgEvent;

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

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