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

public class PopupAndButtonList : MonoBehaviour
{
	public virtual void Awake()
	{
		this.is_select_func_call_ = true;
		this.PopUpList.onChangePopUpListValue.Add(new Action<KeyValuePair<string, UnityEngine.Object>>(this.OnChangePopUpList));
	}

	public virtual void Start()
	{
	}

	public void ResaveButton(int element)
	{
		NDebug.Assert(this.Grid.gameObject.transform.childCount == 0, "eorro");
		for (int i = 0; i < element; i++)
		{
			GameObject gameObject = Utility.CreatePrefab(this.Grid.gameObject, "ScenePhotoMode/WindowListItemNormalButton", true);
			gameObject.name = i.ToString();
			gameObject.GetComponentInChildren<UILabel>().text = gameObject.name;
			EventDelegate.Add(gameObject.GetComponent<UIButton>().onClick, new EventDelegate.Callback(this.OnClickButton));
		}
		Transform transform = base.transform;
		BasePhotoWindow component;
		do
		{
			component = transform.GetComponent<BasePhotoWindow>();
			transform = transform.parent;
		}
		while (component == null);
		component.UpdateChildren();
		for (int j = 0; j < this.Grid.transform.childCount; j++)
		{
			this.Grid.transform.GetChild(j).gameObject.SetActive(false);
		}
	}

	public void ResaveButton<T>(Dictionary<string, T> popup_and_button_name_list, int magnification) where T : ICollection
	{
		int num = 0;
		foreach (KeyValuePair<string, T> keyValuePair in popup_and_button_name_list)
		{
			int num2 = num;
			T value = keyValuePair.Value;
			if (num2 < value.Count)
			{
				T value2 = keyValuePair.Value;
				num = value2.Count;
			}
		}
		num *= magnification;
		for (int i = 0; i < num; i++)
		{
			GameObject gameObject = Utility.CreatePrefab(this.Grid.gameObject, "ScenePhotoMode/WindowListItem", true);
			gameObject.name = i.ToString();
			gameObject.GetComponentInChildren<UILabel>().text = gameObject.name;
			EventDelegate.Add(gameObject.GetComponent<UIButton>().onClick, new EventDelegate.Callback(this.OnClickButton));
		}
		Transform transform = base.transform;
		BasePhotoWindow component;
		do
		{
			component = transform.GetComponent<BasePhotoWindow>();
			transform = transform.parent;
		}
		while (component == null);
		component.UpdateChildren();
		for (int j = 0; j < this.Grid.transform.childCount; j++)
		{
			this.Grid.transform.GetChild(j).gameObject.SetActive(false);
		}
	}

	public void SetData(Dictionary<string, List<KeyValuePair<string, object>>> popup_and_button_name_list, bool create_margin = false)
	{
		if (popup_and_button_name_list != null && this.Grid.gameObject.transform.childCount == 0)
		{
			this.ResaveButton<List<KeyValuePair<string, object>>>(popup_and_button_name_list, create_margin ? 3 : 1);
		}
		this.popup_and_button_name_list_ = new Dictionary<string, List<PopupAndButtonList.ElementData>>();
		if (popup_and_button_name_list == null)
		{
			return;
		}
		foreach (KeyValuePair<string, List<KeyValuePair<string, object>>> keyValuePair in popup_and_button_name_list)
		{
			List<PopupAndButtonList.ElementData> list = new List<PopupAndButtonList.ElementData>();
			for (int i = 0; i < keyValuePair.Value.Count; i++)
			{
				list.Add(new PopupAndButtonList.ElementData
				{
					draw_title = keyValuePair.Value[i].Key,
					guid = Guid.NewGuid(),
					value = keyValuePair.Value[i].Value
				});
			}
			this.popup_and_button_name_list_.Add(keyValuePair.Key, list);
		}
	}

	public void AddData(string popup_category_name, KeyValuePair<string, object> value_data)
	{
		List<PopupAndButtonList.ElementData> list = this.popup_and_button_name_list_[popup_category_name];
		list.Add(new PopupAndButtonList.ElementData
		{
			draw_title = value_data.Key,
			guid = Guid.NewGuid(),
			value = value_data.Value
		});
	}

	protected void OnChangePopUpList(KeyValuePair<string, UnityEngine.Object> popup_val)
	{
		for (int i = 0; i < this.Grid.transform.childCount; i++)
		{
			this.Grid.transform.GetChild(i).gameObject.name = string.Empty;
			this.Grid.transform.GetChild(i).gameObject.SetActive(false);
		}
		if (this.popup_and_button_name_list_ == null || !this.popup_and_button_name_list_.ContainsKey(popup_val.Key))
		{
			Utility.ResetNGUI(this.Grid);
			Utility.ResetNGUI(this.ScrollView);
			for (int j = 0; j < this.onChangePopUpListValue.Count; j++)
			{
				this.onChangePopUpListValue[j](popup_val);
			}
			return;
		}
		List<PopupAndButtonList.ElementData> list = this.popup_and_button_name_list_[popup_val.Key];
		for (int k = 0; k < list.Count; k++)
		{
			GameObject gameObject = this.Grid.transform.GetChild(k).gameObject;
			gameObject.SetActive(true);
			gameObject.name = list[k].guid.ToString();
			gameObject.GetComponentInChildren<UILabel>().text = list[k].draw_title;
		}
		Utility.ResetNGUI(this.Grid);
		Utility.ResetNGUI(this.ScrollView);
		for (int l = 0; l < this.onChangePopUpListValue.Count; l++)
		{
			this.onChangePopUpListValue[l](popup_val);
		}
	}

	private void OnClickButton()
	{
		string name = UIButton.current.gameObject.name;
		foreach (KeyValuePair<string, List<PopupAndButtonList.ElementData>> keyValuePair in this.popup_and_button_name_list_)
		{
			for (int i = 0; i < keyValuePair.Value.Count; i++)
			{
				if (name == keyValuePair.Value[i].guid.ToString())
				{
					for (int j = 0; j < this.onClickEventList.Count; j++)
					{
						this.onClickEventList[j](keyValuePair.Value[i].value);
					}
				}
			}
		}
	}

	public virtual bool SetPopupValue(string name)
	{
		return this.PopUpList.SetPopupValue(name);
	}

	public virtual string GetCategoryNameFromSelectButton(object value_object)
	{
		if (value_object == null)
		{
			return string.Empty;
		}
		foreach (KeyValuePair<string, List<PopupAndButtonList.ElementData>> keyValuePair in this.popup_and_button_name_list_)
		{
			for (int i = 0; i < keyValuePair.Value.Count; i++)
			{
				if (keyValuePair.Value[i].value == value_object)
				{
					return keyValuePair.Key;
				}
			}
		}
		return string.Empty;
	}

	public List<KeyValuePair<string, UnityEngine.Object>> popup_value_list
	{
		get
		{
			return this.PopUpList.popup_value_list;
		}
		set
		{
			this.PopUpList.popup_value_list = value;
		}
	}

	public WindowPartsPopUpList PopUpList;

	public UIGrid Grid;

	public UIScrollView ScrollView;

	public List<Action<KeyValuePair<string, UnityEngine.Object>>> onChangePopUpListValue = new List<Action<KeyValuePair<string, UnityEngine.Object>>>();

	public List<Action<object>> onClickEventList = new List<Action<object>>();

	private Dictionary<string, List<PopupAndButtonList.ElementData>> popup_and_button_name_list_;

	private bool is_select_func_call_;

	private class ElementData
	{
		public Guid guid;

		public string draw_title;

		public object value;
	}
}
