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

namespace Wizard.Dialog.Setting;

public class ItemSelect : Item
{
	[SerializeField]
	private UILabel _title;

	[SerializeField]
	private GameObject _state;

	[SerializeField]
	private GameObject _separatorLineObj;

	[SerializeField]
	private UIButton _button;

	[SerializeField]
	private GameObject _list;

	[SerializeField]
	private UITable _table;

	[SerializeField]
	private BoxCollider _listCollider;

	private EventDelegate.Callback _onClick;

	[SerializeField]
	private GameObject _resolutionItem;

	[SerializeField]
	private UIPanel _panel;

	private string _currentValue;

	private const int BOTTOM_PADDING = -10;

	private const int NORMAL_ITEM_SIZE = 188;

	private Color _originalColor;

	private const int PANEL_DEPTH_CLOSE = 30;

	private const int PANEL_DEPTH_OPEN = 50;

	private UIScrollView _parentScrollView;

	private List<string> _possibleValues;

	private List<GameObject> _items;

	private const int DIRECTION_UP_TABLE_POSITION = 14;

	private const int DIRECTION_UP_LIST_POSITION = 12;

	public bool _isOpenDirectionUp;

	public List<string> PossibleValues => _possibleValues;

	private void Awake()
	{
		_button.onClick.Add(new EventDelegate(delegate
		{
			if (_list.activeSelf)
			{
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_CANCEL);
			}
			else
			{
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
			}
			SetListVisible(!_list.activeSelf);
		}));
		UIEventListener.Get(_listCollider.gameObject).onPress = delegate
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_CANCEL);
			SetListVisible(b: false);
		};
	}

	public void SetPossibleResolutionValues(List<string> possibleValues, bool resizeListSprite)
	{
		for (int i = 0; i < possibleValues.Count; i++)
		{
			GameObject o = NGUITools.AddChild(_table.gameObject, _resolutionItem);
			string str = possibleValues[i];
			string[] array = str.Split('×');
			o.transform.Find("width").gameObject.GetComponent<UILabel>().text = array[0];
			o.transform.Find("height").gameObject.GetComponent<UILabel>().text = array[1];
			o.SetActive(value: true);
			if ((bool)_parentScrollView)
			{
				o.AddMissingComponent<UIDragScrollView>().scrollView = _parentScrollView;
			}
			if (resizeListSprite && i == possibleValues.Count - 1)
			{
				UISprite component = _list.GetComponent<UISprite>();
				component.bottomAnchor.target = o.transform;
				component.bottomAnchor.relative = 0f;
				component.bottomAnchor.absolute = -10;
			}
			_originalColor = o.GetComponentInChildren<UISprite>().color;
			UIEventListener.Get(o).onHover = delegate(GameObject g, bool b)
			{
				o.GetComponentInChildren<UISprite>().enabled = b;
			};
			UIEventListener.Get(o).onClick = delegate
			{
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_TOGGLE_ON);
				o.GetComponentInChildren<UISprite>().enabled = false;
				SetListVisible(b: false);
				SetResolutionValue(str);
				if (_onClick != null)
				{
					_onClick();
				}
			};
		}
	}

	public void SetPossibleIdValues(List<string> possibleValues, bool resizeListSprite)
	{
		SetPossibleValues(possibleValues, resizeListSprite, (string s) => Data.SystemText.Get(s));
	}

	public void SetPossibleValues(List<string> possibleValues, bool resizeListSprite, Func<string, string> textChange = null)
	{
		if (_possibleValues != null)
		{
			_possibleValues.Clear();
		}
		else
		{
			_possibleValues = new List<string>();
		}
		if (_items != null)
		{
			foreach (GameObject item in _items)
			{
				UnityEngine.Object.Destroy(item);
			}
			_items.Clear();
		}
		else
		{
			_items = new List<GameObject>();
		}
		List<string> list = new List<string>(possibleValues);
		if (_isOpenDirectionUp)
		{
			list.Reverse();
		}
		for (int i = 0; i < list.Count; i++)
		{
			if (string.IsNullOrEmpty(list[i]))
			{
				continue;
			}
			GameObject o = NGUITools.AddChild(_table.gameObject, _resolutionItem);
			_items.Add(o);
			string str = list[i];
			if (textChange != null)
			{
				str = textChange(str);
			}
			_possibleValues.Add(str);
			o.transform.Find("width").gameObject.SetActive(value: false);
			o.transform.Find("height").gameObject.SetActive(value: false);
			UILabel component = o.transform.Find("center").gameObject.GetComponent<UILabel>();
			component.width = 188;
			component.text = str;
			o.SetActive(value: true);
			if ((bool)_parentScrollView)
			{
				o.AddMissingComponent<UIDragScrollView>().scrollView = _parentScrollView;
			}
			_originalColor = o.GetComponentInChildren<UISprite>().color;
			UIEventListener.Get(o).onHover = delegate(GameObject g, bool b)
			{
				o.GetComponentInChildren<UISprite>().enabled = b;
			};
			UIEventListener.Get(o).onClick = delegate
			{
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_TOGGLE_ON);
				o.GetComponentInChildren<UISprite>().enabled = false;
				SetListVisible(b: false);
				SetValue(str);
				if (_onClick != null)
				{
					_onClick();
				}
			};
		}
		if (_isOpenDirectionUp)
		{
			_possibleValues.Reverse();
		}
		if (resizeListSprite && _items.Count > 0)
		{
			GameObject gameObject = _items[_items.Count - 1];
			UISprite component2 = _list.GetComponent<UISprite>();
			if (_isOpenDirectionUp)
			{
				component2.topAnchor.target = gameObject.transform;
				component2.topAnchor.relative = 1f;
				component2.topAnchor.absolute = 10;
			}
			else
			{
				component2.bottomAnchor.target = gameObject.transform;
				component2.bottomAnchor.relative = 0f;
				component2.bottomAnchor.absolute = -10;
			}
		}
		if (_isOpenDirectionUp)
		{
			_table.GetComponent<UIWidget>().pivot = UIWidget.Pivot.BottomLeft;
			Vector3 localPosition = _table.transform.localPosition;
			localPosition.y = 14f;
			_table.transform.localPosition = localPosition;
			_table.direction = UITable.Direction.Up;
			_list.GetComponent<UIWidget>().pivot = UIWidget.Pivot.Bottom;
			Vector3 localPosition2 = _list.transform.localPosition;
			localPosition2.y = 12f;
			_list.transform.localPosition = localPosition2;
		}
		_table.repositionNow = true;
		_list.GetComponent<UISprite>().ResetAndUpdateAnchors();
	}

	private void SetListVisible(bool b)
	{
		_list.SetActive(b);
		_listCollider.enabled = b;
		_panel.depth = (b ? 50 : 30);
		UISprite component = _list.GetComponent<UISprite>();
		component.ResetAndUpdateAnchors();
		component.gameObject.SetActive(b);
	}

	public void SetResolutionValue(string value)
	{
		_currentValue = value;
		string[] array = value.Split('×');
		_state.transform.Find("width").gameObject.GetComponent<UILabel>().text = array[0];
		_state.transform.Find("height").gameObject.GetComponent<UILabel>().text = array[1];
	}

	public void SetValue(string value)
	{
		_currentValue = value;
		_state.transform.Find("width").gameObject.SetActive(value: false);
		_state.transform.Find("height").gameObject.SetActive(value: false);
		UILabel component = _state.transform.Find("center").gameObject.GetComponent<UILabel>();
		component.width = 188;
		component.text = value;
	}

	public string GetValue()
	{
		return _currentValue;
	}

	public void SetTitleLabel(string title)
	{
		_title.text = title;
	}

	public override void AddChangeCallback(EventDelegate.Callback callback)
	{
		_onClick = (EventDelegate.Callback)Delegate.Combine(_onClick, callback);
	}

	public override void SetActive_SeparatorLine(bool isActive)
	{
		_separatorLineObj.SetActive(isActive);
	}

	public void SetTitleTextLocalPos(Vector3 localPos)
	{
		_title.transform.localPosition = localPos;
	}

	public void SetToGrey(bool b)
	{
		UIManager.SetObjectToGrey(base.gameObject, b);
		UIManager.SetObjectToGrey(_separatorLineObj, b: false);
		UISprite[] componentsInChildren = _table.GetComponentsInChildren<UISprite>(includeInactive: true);
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			componentsInChildren[i].color = _originalColor;
		}
	}

	public void SetScrollView(UIScrollView view)
	{
		_listCollider.gameObject.AddMissingComponent<UIDragScrollView>().scrollView = view;
		_parentScrollView = view;
		_button.gameObject.AddMissingComponent<UIDragScrollView>().scrollView = view;
	}
}
