using System;
using UnityEngine;
using wf;

namespace SceneNPCEdit
{
	public class NPCCharaSelect : MonoBehaviour
	{
		public EditCharacterDatabase.Data selectNPC { get; private set; }

		private UIProgressBar scrollBar
		{
			get
			{
				return this.scrollView.verticalScrollBar;
			}
		}

		private void Awake()
		{
			NDebug.AssertNull(this.MaidPlateParentGrid != null);
			this.plateTabPanel = this.MaidPlateParentGrid.gameObject.GetComponent<UIWFTabPanel>();
			this.scrollView = UTY.GetChildObject(base.gameObject, "Contents", false).GetComponent<UIScrollView>();
			this.Create();
		}

		public void Create()
		{
			foreach (EditCharacterDatabase.Data data in EditCharacterDatabase.GetAllDatas(true))
			{
				GameObject gameObject = Utility.CreatePrefab(this.MaidPlateParentGrid.gameObject, "CharacterSelect/Prefab/MaidPlateSimple", true);
				gameObject.name = data.id.ToString();
				NPCEditPlate npceditPlate = gameObject.AddComponent<NPCEditPlate>();
				npceditPlate.onSelectEvent = new Action<NPCEditPlate>(this.OnSelectPlate);
				npceditPlate.lastName = data.npcData.lastName;
				npceditPlate.firstName = data.npcData.firstName;
				Texture2D thumbnail = (!SaveData.Contains(data.id)) ? data.GetThumbnail() : SaveData.GetPreset(data.id).texThum;
				npceditPlate.thumbnail = thumbnail;
			}
			this.plateTabPanel.UpdateChildren();
			Utility.ResetNGUI(this.MaidPlateParentGrid);
			Utility.ResetNGUI(this.scrollView);
			if (this.MaidPlateParentGrid.GetChildList().Count != 0)
			{
				if (NPCCharaSelect.editTargetNPCData != null)
				{
					for (int i = 0; i < this.MaidPlateParentGrid.transform.childCount; i++)
					{
						int num = int.Parse(this.MaidPlateParentGrid.GetChild(i).name);
						if (num == NPCCharaSelect.editTargetNPCData.id)
						{
							UIWFTabButton component = UTY.GetChildObject(this.MaidPlateParentGrid.GetChild(i).gameObject, "Button", false).GetComponent<UIWFTabButton>();
							if (component != null)
							{
								this.plateTabPanel.Select(component);
							}
							break;
						}
					}
				}
				else
				{
					UIWFTabButton component2 = UTY.GetChildObject(this.MaidPlateParentGrid.GetChild(0).gameObject, "Button", false).GetComponent<UIWFTabButton>();
					if (component2 != null)
					{
						this.plateTabPanel.Select(component2);
					}
				}
			}
			NPCCharaSelect.editTargetNPCData = null;
		}

		public void UpdateSelectData()
		{
			for (int i = 0; i < this.MaidPlateParentGrid.transform.childCount; i++)
			{
				int num = int.Parse(this.MaidPlateParentGrid.GetChild(i).name);
				if (num == this.selectNPC.id)
				{
					NPCEditPlate component = this.MaidPlateParentGrid.GetChild(i).GetComponent<NPCEditPlate>();
					Texture2D thumbnail = (!SaveData.Contains(num)) ? this.selectNPC.GetThumbnail() : SaveData.GetPreset(num).texThum;
					component.thumbnail = thumbnail;
					this.statusPanel.Apply(component, this.selectNPC.additionalInformationText);
				}
			}
		}

		public void OnSelectPlate(NPCEditPlate selectPlate)
		{
			this.selectNPC = EditCharacterDatabase.GetData(int.Parse(selectPlate.name));
			this.statusPanel.Apply(selectPlate, this.selectNPC.additionalInformationText);
		}

		public static EditCharacterDatabase.Data editTargetNPCData;

		[SerializeField]
		private UIGrid MaidPlateParentGrid;

		[SerializeField]
		private NPCEditStatus statusPanel;

		private UIWFTabPanel plateTabPanel;

		private UIScrollView scrollView;
	}
}
