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

public class PopupAndTabList : 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.TabPanel.gameObject.transform.childCount == 0, "eorro");
		for (int i = 0; i < element; i++)
		{
			GameObject gameObject = Utility.CreatePrefab(this.TabPanel.gameObject, "ScenePhotoMode/WindowListItem", true);
			gameObject.name = i.ToString();
			gameObject.GetComponentInChildren<UILabel>().text = gameObject.name;
			EventDelegate.Add(gameObject.GetComponent<UIWFTabButton>().onSelect, new EventDelegate.Callback(this.OnSelectItem));
		}
		Transform transform = base.transform;
		BasePhotoWindow component;
		do
		{
			component = transform.GetComponent<BasePhotoWindow>();
			transform = transform.parent;
		}
		while (component == null);
		component.UpdateChildren();
		this.TabPanel.UpdateChildren();
		for (int j = 0; j < this.TabPanel.transform.childCount; j++)
		{
			this.TabPanel.transform.GetChild(j).gameObject.SetActive(false);
		}
	}

	public void ResaveButton<T>(Dictionary<string, T> popup_and_button_name_list, int magnification) where T : ICollection
	{
		NDebug.Assert(this.TabPanel.gameObject.transform.childCount == 0, "eorro");
		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.TabPanel.gameObject, "ScenePhotoMode/WindowListItem", true);
			gameObject.name = i.ToString();
			gameObject.GetComponentInChildren<UILabel>().text = gameObject.name;
			EventDelegate.Add(gameObject.GetComponent<UIWFTabButton>().onSelect, new EventDelegate.Callback(this.OnSelectItem));
		}
		Transform transform = base.transform;
		BasePhotoWindow component;
		do
		{
			component = transform.GetComponent<BasePhotoWindow>();
			transform = transform.parent;
		}
		while (component == null);
		component.UpdateChildren();
		this.TabPanel.UpdateChildren();
		for (int j = 0; j < this.TabPanel.transform.childCount; j++)
		{
			this.TabPanel.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.TabPanel.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<PopupAndTabList.ElementData>>();
		if (popup_and_button_name_list == null)
		{
			this.ResetSelect();
			this.cur_select_btn_name_ = string.Empty;
			return;
		}
		foreach (KeyValuePair<string, List<KeyValuePair<string, object>>> keyValuePair in popup_and_button_name_list)
		{
			List<PopupAndTabList.ElementData> list = new List<PopupAndTabList.ElementData>();
			for (int i = 0; i < keyValuePair.Value.Count; i++)
			{
				list.Add(new PopupAndTabList.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<PopupAndTabList.ElementData> list = this.popup_and_button_name_list_[popup_category_name];
		list.Add(new PopupAndTabList.ElementData
		{
			draw_title = value_data.Key,
			guid = Guid.NewGuid(),
			value = value_data.Value
		});
	}

	public void ResetSelect()
	{
		if (this.TabPanel.GetSelectButtonObject() != null)
		{
			this.TabPanel.GetSelectButtonObject().SetSelect(false);
		}
		this.TabPanel.ResetSelect();
	}

	protected void OnChangePopUpList(KeyValuePair<string, UnityEngine.Object> popup_val)
	{
		if (this.TabPanel.GetSelectButtonObject() != null)
		{
			this.cur_select_btn_name_ = this.TabPanel.GetSelectButtonObject().name;
		}
		this.TabPanel.ResetSelect();
		UIGrid component = this.TabPanel.GetComponent<UIGrid>();
		for (int i = 0; i < this.TabPanel.transform.childCount; i++)
		{
			this.TabPanel.transform.GetChild(i).GetComponent<UIWFTabButton>().SetSelect(false);
			this.TabPanel.transform.GetChild(i).gameObject.name = string.Empty;
			this.TabPanel.transform.GetChild(i).gameObject.SetActive(false);
		}
		if (this.popup_and_button_name_list_ == null || !this.popup_and_button_name_list_.ContainsKey(popup_val.Key))
		{
			component.Reposition();
			this.ScrollView.ResetPosition();
			this.cur_select_btn_name_ = string.Empty;
			for (int j = 0; j < this.onChangePopUpListValue.Count; j++)
			{
				this.onChangePopUpListValue[j](popup_val);
			}
			return;
		}
		UIWFTabButton uiwftabButton = null;
		List<PopupAndTabList.ElementData> list = this.popup_and_button_name_list_[popup_val.Key];
		for (int k = 0; k < list.Count; k++)
		{
			GameObject gameObject = this.TabPanel.transform.GetChild(k).gameObject;
			gameObject.SetActive(true);
			gameObject.name = list[k].guid.ToString();
			gameObject.GetComponentInChildren<UILabel>().text = list[k].draw_title;
			if (gameObject.name == this.cur_select_btn_name_)
			{
				uiwftabButton = gameObject.GetComponent<UIWFTabButton>();
			}
		}
		component.Reposition();
		this.ScrollView.ResetPosition();
		if (uiwftabButton != null)
		{
			this.is_select_func_call_ = false;
			this.TabPanel.Select(uiwftabButton);
			this.is_select_func_call_ = true;
		}
		for (int l = 0; l < this.onChangePopUpListValue.Count; l++)
		{
			this.onChangePopUpListValue[l](popup_val);
		}
	}

	private void OnSelectItem()
	{
		if (!UIWFSelectButton.current.isSelected)
		{
			return;
		}
		this.cur_select_btn_name_ = UIWFSelectButton.current.gameObject.name;
		if (this.is_select_func_call_)
		{
			foreach (KeyValuePair<string, List<PopupAndTabList.ElementData>> keyValuePair in this.popup_and_button_name_list_)
			{
				for (int i = 0; i < keyValuePair.Value.Count; i++)
				{
					if (this.cur_select_btn_name_ == keyValuePair.Value[i].guid.ToString())
					{
						for (int j = 0; j < this.onSelect.Count; j++)
						{
							this.onSelect[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<PopupAndTabList.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 virtual bool SetSelectButton(object value_object, bool fire_event)
	{
		if (value_object == null)
		{
			return false;
		}
		PopupAndTabList.ElementData elementData = null;
		string categoryNameFromSelectButton = this.GetCategoryNameFromSelectButton(value_object);
		if (!string.IsNullOrEmpty(categoryNameFromSelectButton))
		{
			int num = 0;
			while (num < this.popup_and_button_name_list_[categoryNameFromSelectButton].Count && elementData == null)
			{
				if (this.popup_and_button_name_list_[categoryNameFromSelectButton][num].value == value_object)
				{
					elementData = this.popup_and_button_name_list_[categoryNameFromSelectButton][num];
				}
				num++;
			}
		}
		if (elementData == null)
		{
			return false;
		}
		string key = this.PopUpList.popup_select_value.Key;
		if (key != categoryNameFromSelectButton)
		{
			this.SetPopupValue(categoryNameFromSelectButton);
		}
		bool result = false;
		Transform transform = this.TabPanel.transform;
		for (int i = 0; i < transform.childCount; i++)
		{
			if (transform.GetChild(i).gameObject.name == elementData.guid.ToString())
			{
				this.is_select_func_call_ = fire_event;
				this.TabPanel.Select(transform.GetChild(i).GetComponent<UIWFTabButton>());
				this.is_select_func_call_ = true;
				result = true;
				break;
			}
		}
		if (key != categoryNameFromSelectButton)
		{
			this.SetPopupValue(key);
		}
		return result;
	}

	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 UIWFTabPanel TabPanel;

	public UIScrollView ScrollView;

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

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

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

	private string cur_select_btn_name_;

	private bool is_select_func_call_;

	private class ElementData
	{
		public Guid guid;

		public string draw_title;

		public object value;
	}
}
