using System;
using System.Collections.Generic;
using System.Linq;
using Cute;
using UnityEngine;

namespace Wizard;

public class TypeFilterDialog : MonoBehaviour
{
	private const int DIALOG_PANEL_DEPTH = 15;

	[SerializeField]
	private TypeFilterSingle _singleOriginal;

	[SerializeField]
	private UIGrid _grid;

	[SerializeField]
	private UIButton _resetButton;

	[SerializeField]
	private UILabel _noTypeLabel;

	private List<TypeFilterSingle> _allTypeList = new List<TypeFilterSingle>();

	private List<CardBasePrm.TribeType> _firstSelectType;

	private List<CardBasePrm.TribeType> _displayTypeList;

	public DialogBase Dialog { get; private set; }

	public Action OnChange { get; set; }

	public static TypeFilterDialog Create(GameObject prefab, List<CardBasePrm.TribeType> currentSelectType, List<CardBasePrm.TribeType> displayTypeList)
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetSize(DialogBase.Size.M);
		dialogBase.SetPanelDepth(15);
		dialogBase.SetTitleLabel(Data.SystemText.Get("Card_0057"));
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.CloseBtn);
		GameObject gameObject = UnityEngine.Object.Instantiate(prefab);
		dialogBase.SetObj(gameObject);
		TypeFilterDialog component = gameObject.GetComponent<TypeFilterDialog>();
		component.Dialog = dialogBase;
		component._firstSelectType = currentSelectType;
		component._displayTypeList = displayTypeList;
		return component;
	}

	private void Start()
	{
		foreach (CardBasePrm.TribeType value in Enum.GetValues(typeof(CardBasePrm.TribeType)))
		{
			if (value != CardBasePrm.TribeType.ALL && value != CardBasePrm.TribeType.MAX && (_displayTypeList == null || _displayTypeList.Contains(value)))
			{
				AddType(value);
			}
		}
		_singleOriginal.gameObject.SetActive(value: false);
		UIEventListener.Get(_resetButton.gameObject).onClick = delegate
		{
			OnClickResetButton();
		};
		_noTypeLabel.gameObject.SetActive(!_grid.GetChildList().Any());
	}

	private void AddType(CardBasePrm.TribeType tribe)
	{
		GameObject obj = NGUITools.AddChild(_grid.gameObject, _singleOriginal.gameObject);
		obj.SetActive(value: true);
		TypeFilterSingle component = obj.GetComponent<TypeFilterSingle>();
		component.Initialize(tribe);
		if (_firstSelectType.Contains(tribe))
		{
			component.SetSelected();
		}
		component.OnValueChange = delegate
		{
			OnChange.Call();
		};
		_allTypeList.Add(component);
	}

	public List<CardBasePrm.TribeType> GetAllSelectType()
	{
		List<CardBasePrm.TribeType> list = new List<CardBasePrm.TribeType>();
		foreach (TypeFilterSingle allType in _allTypeList)
		{
			if (allType.IsSelected)
			{
				list.Add(allType.Type);
			}
		}
		return list;
	}

	private void OnClickResetButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
		Action onChange = OnChange;
		OnChange = null;
		foreach (TypeFilterSingle allType in _allTypeList)
		{
			allType.Reset();
		}
		OnChange = onChange;
		OnChange.Call();
	}
}
