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

namespace Kasizuki
{
	public class ClubRoomSelectCtrl : NGUIWindow
	{
		private void Start()
		{
		}

		public void SetCallbackOnClick(Action<RoomData.Data> callback)
		{
			this.m_CallbackOnClick = callback;
		}

		public void SetData()
		{
			this.UpdateUI();
		}

		private void UpdateUI()
		{
			FacilityManager facilityMgr = GameMain.Instance.FacilityMgr;
			Facility[] facilityArray = facilityMgr.GetFacilityArray();
			List<RoomData.Data> enableFacilityArray = this.GetFilteringKasizukiFacilityArray(facilityArray);
			this.GetFilteringRoomData(ref enableFacilityArray, delegate(RoomData.Data room)
			{
				if (room.isExistUpwardRoom && enableFacilityArray.Contains(room.GetUpwardRoomData()))
				{
					return false;
				}
				if (room.strConditionsArraySystemFlag != null && !GameMain.Instance.KasizukiMgr.IsMeetConditions(room.strConditionsArraySystemFlag))
				{
					return false;
				}
				ManDataType nowManType = (ManDataType)GameMain.Instance.KasizukiMgr.GetNowManType();
				if (!room.enableManTypeDic[nowManType])
				{
					return false;
				}
				List<PlayData.Data> playDatas = room.GetPlayDatas();
				List<PlayData.Data> list = new List<PlayData.Data>();
				foreach (PlayData.Data data in playDatas)
				{
					if (data.enableManTypeDic[nowManType])
					{
						if (data.GetFilteringPlayDataMaidArray(nowManType).Count > 0)
						{
							list.Add(data);
						}
					}
				}
				return list.Count > 0;
			});
			this.listViewer.Show<Transform>(enableFacilityArray.Count, delegate(int index, Transform trans)
			{
				RoomData.Data data = enableFacilityArray[index];
				this.OnCreateItemList(index, trans, data);
			});
			UIGrid component = this.listViewer.parentItemArea.GetComponent<UIGrid>();
			component.Reposition();
		}

		private void OnCreateItemList(int index, Transform trans, RoomData.Data data)
		{
			int facilityTypeID = data.facilityTypeID;
			Sprite facilityThumbnail = FacilityDataTable.GetFacilityThumbnail(facilityTypeID, true);
			if (facilityThumbnail != null)
			{
				UITexture componentInChildren = trans.GetComponentInChildren<UITexture>();
				componentInChildren.mainTexture = facilityThumbnail.texture;
			}
			UILabel component = UTY.GetChildObject(trans.gameObject, "Label Name", false).GetComponent<UILabel>();
			component.text = data.drawName;
			UILabel component2 = UTY.GetChildObject(trans.gameObject, "Comment/Value", false).GetComponent<UILabel>();
			component2.text = data.explanatoryText;
			UIButton uiButton = trans.GetComponent<UIButton>();
			if (uiButton)
			{
				EventDelegate.Add(uiButton.onClick, delegate()
				{
					this.OnClickItem(uiButton, data);
				});
			}
			else
			{
				Debug.LogWarningFormat("項目には「{0}」コンポーネントがありません", new object[]
				{
					typeof(UIButton)
				});
			}
		}

		private void OnClickItem(UIButton item, RoomData.Data roomData)
		{
			foreach (GameObject f_goParent in this.listViewer.ItemArray)
			{
				UTY.GetChildObject(f_goParent, "mask", false).SetActive(true);
			}
			UTY.GetChildObject(item.gameObject, "mask", false).SetActive(false);
			if (this.m_CallbackOnClick != null)
			{
				this.m_CallbackOnClick(roomData);
			}
		}

		private List<RoomData.Data> GetFilteringKasizukiFacilityArray(Facility[] nowFacilityArray)
		{
			List<RoomData.Data> list = new List<RoomData.Data>();
			List<RoomData.Data> allDatas = RoomData.GetAllDatas(true);
			foreach (RoomData.Data data in allDatas)
			{
				if (GameMain.Instance.FacilityMgr.IsExistTypeFacility(data.facilityTypeID))
				{
					list.Add(data);
				}
			}
			return list;
		}

		private void GetFilteringRoomData(ref List<RoomData.Data> roomList, Func<RoomData.Data, bool> filteringFunc)
		{
			List<RoomData.Data> list = new List<RoomData.Data>(roomList);
			for (int i = 0; i < list.Count; i++)
			{
				RoomData.Data data = list[i];
				if (!filteringFunc(data))
				{
					roomList.Remove(data);
				}
			}
		}

		private RoomData.Data GetKasizukiRoomData(int facilityTypeID)
		{
			List<RoomData.Data> allDatas = RoomData.GetAllDatas(true);
			foreach (RoomData.Data data in allDatas)
			{
				if (data.facilityTypeID == facilityTypeID)
				{
					return data;
				}
			}
			return null;
		}

		private uGUIListViewer listViewer
		{
			get
			{
				if (!this.m_ListViewerRoom)
				{
					this.m_ListViewerRoom = base.GetComponent<uGUIListViewer>();
				}
				return this.m_ListViewerRoom;
			}
		}

		private uGUIListViewer m_ListViewerRoom;

		private Action<RoomData.Data> m_CallbackOnClick;
	}
}
