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

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

	[SerializeField]
	private CardDetailFilterCategory _categoryOriginal;

	[SerializeField]
	private CardDetailFilterKeyWord _keywordOriginal;

	[SerializeField]
	private FlexibleGrid _grid;

	[SerializeField]
	private UIButton _resetButton;

	private List<CardDetailFilterCategory> _allCategory = new List<CardDetailFilterCategory>();

	private List<string> _saveList;

	private bool _initializeFinish;

	private bool _isChanged;

	private Dictionary<string, bool> _existKeyWordDictionary;

	public Action OnChange { get; set; }

	public DialogBase Dialog { get; private set; }

	public static CardDetailFilterDialog Create(GameObject prefab, List<string> saveList, Dictionary<string, bool> existKeyWordDictionary)
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetSize(DialogBase.Size.M);
		dialogBase.SetTitleLabel(Data.SystemText.Get("Card_0227"));
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.CloseBtn);
		dialogBase.SetPanelDepth(15);
		GameObject gameObject = UnityEngine.Object.Instantiate(prefab);
		dialogBase.SetObj(gameObject);
		CardDetailFilterDialog component = gameObject.GetComponent<CardDetailFilterDialog>();
		component.Dialog = dialogBase;
		component._saveList = saveList;
		component._existKeyWordDictionary = existKeyWordDictionary;
		return component;
	}

	private bool IsCheckedInSaveList(string keyword)
	{
		if (_saveList != null)
		{
			return _saveList.Contains(keyword);
		}
		return false;
	}

	private void Start()
	{
		_categoryOriginal.gameObject.SetActive(value: false);
		_keywordOriginal.gameObject.SetActive(value: false);
		foreach (string category in Data.Master.CardFilterKeyWord.CategoryList)
		{
			AddCategory(Data.SystemText.Get(category), Data.Master.CardFilterKeyWord.GetCategory(category));
		}
		_grid.Reposition();
		UIEventListener.Get(_resetButton.gameObject).onClick = delegate
		{
			OnClickResetButton();
		};
		_initializeFinish = true;
	}

	private void AddCategory(string categoryName, List<string> keyList)
	{
		List<string> list = new List<string>();
		foreach (string key2 in keyList)
		{
			string key = Data.Master.BattleKeyWordTitleDic[key2];
			if (_existKeyWordDictionary == null || _existKeyWordDictionary.ContainsKey(key))
			{
				list.Add(key2);
			}
		}
		if (list.Count == 0)
		{
			return;
		}
		CardDetailFilterCategory component = NGUITools.AddChild(_grid.gameObject, _categoryOriginal.gameObject).GetComponent<CardDetailFilterCategory>();
		_allCategory.Add(component);
		component.name = "category_" + categoryName;
		component.gameObject.SetActive(value: true);
		component.Initialize(categoryName);
		foreach (string item in list)
		{
			string keyword = Data.Master.BattleKeyWordTitleDic[item];
			CardDetailFilterKeyWord component2 = UnityEngine.Object.Instantiate(_keywordOriginal.gameObject).GetComponent<CardDetailFilterKeyWord>();
			component2.gameObject.SetActive(value: true);
			component2.Initialize(keyword);
			component2.OnValueChange = delegate
			{
				if (_initializeFinish)
				{
					_isChanged = true;
				}
			};
			component.AddChild(component2);
			if (IsCheckedInSaveList(keyword))
			{
				component2.SetChecked();
			}
		}
	}

	private void Update()
	{
		if (_isChanged)
		{
			_isChanged = false;
			List<string> filterList = GetFilterList();
			if (!IsSameList(_saveList, filterList))
			{
				_saveList = filterList;
				OnChange.Call();
			}
		}
	}

	private int GetListCount(List<string> list)
	{
		return list?.Count ?? 0;
	}

	private bool IsSameList(List<string> list1, List<string> list2)
	{
		if (GetListCount(list1) == 0 && GetListCount(list2) == 0)
		{
			return true;
		}
		if (GetListCount(list1) != GetListCount(list2))
		{
			return false;
		}
		foreach (string item in list1)
		{
			if (!list2.Contains(item))
			{
				return false;
			}
		}
		foreach (string item2 in list2)
		{
			if (!list1.Contains(item2))
			{
				return false;
			}
		}
		return true;
	}

	private void OnClickResetButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
		foreach (CardDetailFilterCategory item in _allCategory)
		{
			item.Reset();
		}
	}

	public List<string> GetFilterList()
	{
		List<string> list = null;
		foreach (CardDetailFilterCategory item in _allCategory)
		{
			foreach (CardDetailFilterKeyWord item2 in item.AllKeyWord)
			{
				if (item2.IsEnableKeyWord)
				{
					if (list == null)
					{
						list = new List<string>();
					}
					list.Add(item2.KeyWord);
				}
			}
		}
		return list;
	}
}
