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

public class FilterController : MonoBehaviour
{
	public enum MyRotationFilterType
	{
		None,
		DECK,
		CARD_POOL_SELECT_ONLY,
		CARD_POOL_ALL_PACK
	}

	private enum eFILTER_TYPE
	{
		COST,
		FAVORITE,
		RARITY,
		TYPE,
		FOIL,
		CLASS,
		FORMAT,
		ATTACK,
		LIFE,
		SPOT,
		MAX
	}

	[Serializable]
	public class ButtonArray
	{
		public UIButton[] array;

		public UIButton this[int index]
		{
			get
			{
				return array[index];
			}
			set
			{
				array[index] = value;
			}
		}

		public int Length => array.Length;
	}

	private const float MARGIN_CLASS_LINE = 50f;

	private const float MARGIN_PACK_LINE = 38f;

	private const int ALL_BTN_LABEL_HEIGHT = 26;

	private const string FILTER_BTN_LEFT_ON = "pilltab_02_left_on";

	private const string FILTER_BTN_LEFT_OFF = "pilltab_02_left_off";

	private const string FILTER_BTN_MIDDLE_ON = "pilltab_02_middle_on";

	private const string FILTER_BTN_MIDDLE_OFF = "pilltab_02_middle_off";

	private const string FILTER_BTN_RIGHT_ON = "pilltab_02_right_on";

	private const string FILTER_BTN_RIGHT_OFF = "pilltab_02_right_off";

	private const string FILTER_BTN_SINGLE_ON = "pilltab_02_single_on";

	private const string FILTER_BTN_SINGLE_OFF = "pilltab_02_single_off";

	private const int ALL_BUTTON_INDEX = 0;

	private IFormatBehavior _formatBehavior;

	private int[] FlagsArray;

	[SerializeField]
	private UIScrollView _scrollView;

	[SerializeField]
	private ButtonArray[] BtnArray;

	[SerializeField]
	private UIButton _packBtnOrigin;

	[SerializeField]
	private UIGrid _gridPack;

	[SerializeField]
	private Transform _linePack;

	private List<UIButton> _packBtnList = new List<UIButton>();

	private List<bool> _packButtonFlagList = new List<bool>();

	[SerializeField]
	private UIButton ResetBtn;

	[SerializeField]
	private UIGrid _gridClass;

	[SerializeField]
	private Transform _lineClass;

	[SerializeField]
	private UITable _tableRotationBtn;

	[SerializeField]
	private UILabel _labelRotationBtn;

	[SerializeField]
	private UISprite _spriteRotationBtn;

	[SerializeField]
	private UITable _tableUnlimitedBtn;

	[SerializeField]
	private UILabel _labelUnlimitedBtn;

	[SerializeField]
	private UISprite _spriteUnlimitedBtn;

	[SerializeField]
	private GameObject _basicFilterRoot;

	[SerializeField]
	private FlexibleGrid _basicGrid;

	[SerializeField]
	private UIButton _detailFilterEnableButton;

	[SerializeField]
	private UIButton _detailFilterDisableButton;

	[SerializeField]
	private GameObject _detailFilterRoot;

	[SerializeField]
	private FlexibleGrid _detailGrid;

	[SerializeField]
	private GameObject _detailFilterPrefab;

	[SerializeField]
	private UIButton _keywordButton;

	[SerializeField]
	private UIButton _typeButton;

	[SerializeField]
	private FlexibleGrid _kewordListGrid;

	[SerializeField]
	private FlexibleGrid _typeListGrid;

	[SerializeField]
	private CardDetailFilterOffButton _filterOffButtonOriginal;

	[SerializeField]
	private TypeFilterDialog _typeFilterDialogPrefab;

	[SerializeField]
	private GameObject _characterVoiceSearchRoot;

	[SerializeField]
	private UIInputWizard _characterVoiceSearchInput;

	private CardDetailFilterDialog _keywordFilter;

	private List<string> _keywordFilterList;

	private List<CardBasePrm.TribeType> _filterTypeList = new List<CardBasePrm.TribeType>();

	private List<GameObject> _currentFilterList = new List<GameObject>();

	private List<GameObject> _currentTypeFilterList = new List<GameObject>();

	private List<CardBasePrm.TribeType> _typeFilterList;

	private Dictionary<string, bool> _existKeyWordList;

	private MyRotationInfo _myRotationInfo;

	private static bool _isEnableDetailFilter;

	private MyRotationFilterType _myRotationFilterType;

	private bool _isMyRotationAllPackVisible;

	public ClassSet ClassSet { get; set; }

	public Format FormatState { get; set; } = Format.Max;

	public bool IsAbleFormatFilter { get; set; } = true;

	public bool IsShow { get; set; }

	private bool EnableCharacterVoiceInput
	{
		get
		{
			if (CustomPreference.GetTextLanguage() == Global.LANG_TYPE.Jpn.ToString() || CustomPreference.GetTextLanguage() == Global.LANG_TYPE.Cht.ToString() || CustomPreference.GetTextLanguage() == Global.LANG_TYPE.Chs.ToString() || CustomPreference.GetTextLanguage() == Global.LANG_TYPE.Kor.ToString())
			{
				return true;
			}
			return false;
		}
	}

	public event Action OnValidate;

	public void Initialize(IFormatBehavior formatBehavior)
	{
		_formatBehavior = formatBehavior;
	}

	private void Awake()
	{
		FlagsArray = new int[10];
		for (int i = 0; i < FlagsArray.Length; i++)
		{
			FlagsArray[i] = 1;
		}
	}

	private void Start()
	{
		UIManager.GetInstance().AttachAtlas(base.gameObject, isTargetChildren: false);
		_filterOffButtonOriginal.gameObject.SetActive(value: false);
		for (int i = 0; i < BtnArray.Length; i++)
		{
			for (int j = 0; j < BtnArray[i].Length; j++)
			{
				SetEventOnClickBtn(BtnArray[i][j].gameObject, (eFILTER_TYPE)i, j);
			}
		}
		UIEventListener.Get(ResetBtn.gameObject).onClick = OnClickResetBtn;
		UIEventListener.Get(BtnArray[6][1].gameObject).onPress = delegate(GameObject g, bool isPress)
		{
			if (isPress)
			{
				_spriteRotationBtn.spriteName = RenameSpriteString(_spriteRotationBtn.spriteName, isPress);
			}
			else
			{
				UpdateFormatSprite(_spriteRotationBtn, BtnArray[6][1]);
			}
		};
		UIEventListener uIEventListener = UIEventListener.Get(BtnArray[6][1].gameObject);
		uIEventListener.onDrag = (UIEventListener.VectorDelegate)Delegate.Combine(uIEventListener.onDrag, (UIEventListener.VectorDelegate)delegate
		{
			UpdateFormatSprite(_spriteRotationBtn, BtnArray[6][1]);
		});
		UIEventListener uIEventListener2 = UIEventListener.Get(BtnArray[6][1].gameObject);
		uIEventListener2.onDragOut = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener2.onDragOut, (UIEventListener.VoidDelegate)delegate
		{
			UpdateFormatSprite(_spriteRotationBtn, BtnArray[6][1]);
		});
		UIEventListener.Get(BtnArray[6][2].gameObject).onPress = delegate(GameObject g, bool isPress)
		{
			if (isPress)
			{
				_spriteUnlimitedBtn.spriteName = RenameSpriteString(_spriteUnlimitedBtn.spriteName, isPress);
			}
			else
			{
				UpdateFormatSprite(_spriteUnlimitedBtn, BtnArray[6][2]);
			}
		};
		UIEventListener uIEventListener3 = UIEventListener.Get(BtnArray[6][2].gameObject);
		uIEventListener3.onDrag = (UIEventListener.VectorDelegate)Delegate.Combine(uIEventListener3.onDrag, (UIEventListener.VectorDelegate)delegate
		{
			UpdateFormatSprite(_spriteUnlimitedBtn, BtnArray[6][2]);
		});
		UIEventListener uIEventListener4 = UIEventListener.Get(BtnArray[6][2].gameObject);
		uIEventListener4.onDragOut = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener4.onDragOut, (UIEventListener.VoidDelegate)delegate
		{
			UpdateFormatSprite(_spriteUnlimitedBtn, BtnArray[6][2]);
		});
		UIEventListener.Get(_keywordButton.gameObject).onClick = delegate
		{
			OnClickKeyWord();
		};
		UIEventListener.Get(_typeButton.gameObject).onClick = delegate
		{
			OnClickTypeFilter();
		};
		_characterVoiceSearchInput.onSubmit.Add(new EventDelegate(delegate
		{
			OnInputCharacterVoiceSearch();
		}));
		_characterVoiceSearchInput.onDeselect.Add(new EventDelegate(delegate
		{
			OnInputCharacterVoiceSearch();
		}));
		RefreshDetailFilterToggleButton(_isEnableDetailFilter);
		UIEventListener.Get(_detailFilterEnableButton.gameObject).onClick = delegate
		{
			OnChangeDetailFilterEnable(isEnableDetailFilter: true);
		};
		UIEventListener.Get(_detailFilterDisableButton.gameObject).onClick = delegate
		{
			OnChangeDetailFilterEnable(isEnableDetailFilter: false);
		};
		_characterVoiceSearchRoot.SetActive(EnableCharacterVoiceInput);
		_detailGrid.Reposition();
	}

	private void UpdateFormatSprite(UISprite iconSprite, UIButton button)
	{
		iconSprite.spriteName = RenameSpriteString(iconSprite.spriteName, button.gameObject.GetComponent<UISprite>().spriteName.EndsWith("on"));
	}

	private void OnDestroy()
	{
		UIManager.GetInstance().getUIBase_CardManager().ClearKeyWordCache();
	}

	private void RefreshDetailFilterToggleButton(bool isEnableDetailFilter)
	{
		RenameBtnSprite(_detailFilterEnableButton, isEnableDetailFilter);
		RenameBtnSprite(_detailFilterDisableButton, !isEnableDetailFilter);
		_detailFilterRoot.SetActive(isEnableDetailFilter);
		_basicFilterRoot.SetActive(!isEnableDetailFilter);
		if (isEnableDetailFilter)
		{
			_detailGrid.Reposition();
		}
		else
		{
			ResetBasicFilterLinePosition();
			_basicGrid.Reposition();
		}
		_scrollView.UpdateScrollbars();
		_scrollView.ResetPosition();
	}

	private void OnChangeDetailFilterEnable(bool isEnableDetailFilter)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_TOGGLE_ON);
		RefreshDetailFilterToggleButton(isEnableDetailFilter);
		_isEnableDetailFilter = isEnableDetailFilter;
	}

	public UIBase_CardManager.FilterParameter GetFilterParameter(UIBase_CardManager.FilterParameter param)
	{
		param.Cost = FlagsArray[0] >> 1;
		param.Favorite = FlagsArray[1] >> 1;
		param.Rarity = FlagsArray[2] >> 1;
		param.Type = FlagsArray[3] >> 1;
		param.Foil = FlagsArray[4] >> 1;
		param.Attack = FlagsArray[7] >> 1;
		param.Life = FlagsArray[8] >> 1;
		param.Spot = FlagsArray[9] >> 1;
		param.FormatState = GetFormatFilterParam();
		param.Class = GetClassFilterParam();
		param.KeyWordList = ((_keywordFilterList == null) ? null : new List<string>(_keywordFilterList));
		param.TypeFilter = ((_filterTypeList == null) ? null : new List<CardBasePrm.TribeType>(_filterTypeList));
		param.CharacterVoice = _characterVoiceSearchInput.value;
		if (_myRotationInfo != null && _myRotationFilterType != MyRotationFilterType.DECK)
		{
			param.DisableCardSetidList = Data.MyRotationAllInfo.DisableCardPackIdList;
		}
		CollectPackFilterParam(param);
		return param;
	}

	private Format GetFormatFilterParam()
	{
		Format format = Format.Rotation;
		if (IsAbleFormatFilter)
		{
			if (FormatState != Format.Max)
			{
				return FormatState;
			}
			return GetFormatBtnState();
		}
		return Format.Max;
	}

	private int GetClassFilterParam()
	{
		if (ClassSet.MainClass == CardBasePrm.ClanType.ALL)
		{
			return FlagsArray[5] >> 1;
		}
		int num = 0;
		if (FlagsArray[5] == 1)
		{
			num = (1 << (int)ClassSet.MainClass) + 1;
			if (_formatBehavior.UseSubClass)
			{
				num += GetClassFilterMask(ClassSet.SubClass);
			}
		}
		else
		{
			bool flag = IsEnableBit(ref FlagsArray[5], 1);
			num = (IsEnableBit(ref FlagsArray[5], GetClassBtnIndex(ClassSet.MainClass)) ? GetClassFilterMask(ClassSet.MainClass) : 0);
			num += (flag ? 1 : 0);
			if (_formatBehavior.UseSubClass && IsEnableBit(ref FlagsArray[5], GetClassBtnIndex(ClassSet.SubClass)))
			{
				num += GetClassFilterMask(ClassSet.SubClass);
			}
		}
		return num;
	}

	private void CollectPackFilterParam(UIBase_CardManager.FilterParameter param)
	{
		if (_packButtonFlagList.Count == 0)
		{
			return;
		}
		int num = 1;
		IFormatBehavior formatBehavior = _formatBehavior;
		param.MyRotationInfoForFormatAvailable = null;
		if (_myRotationInfo != null && _myRotationFilterType == MyRotationFilterType.CARD_POOL_SELECT_ONLY)
		{
			param.MyRotationInfoForFormatAvailable = _myRotationInfo;
		}
		if (_myRotationInfo != null)
		{
			SetMyRotationCardPackFilter(param);
			return;
		}
		List<CardSetName> availableCardSetNameList = formatBehavior.AvailableCardSetNameList;
		List<string> list = new List<string>(availableCardSetNameList.Count);
		foreach (CardSetName item in availableCardSetNameList)
		{
			if (_packButtonFlagList[num])
			{
				list.Add(item.ID);
			}
			num++;
		}
		param.CardSetIdList = list;
		if (formatBehavior.IsShowPrizeCardSetFilter)
		{
			param.IsEnabledPrizeCard = _packButtonFlagList[num];
			num++;
		}
		if (formatBehavior.IsShowPhantomCardSetFilter)
		{
			param.IsEnabledPhantomCard = _packButtonFlagList[num];
			num++;
		}
	}

	private void SetMyRotationCardPackFilter(UIBase_CardManager.FilterParameter param)
	{
		int num = 1;
		IFormatBehavior formatBehavior = _formatBehavior;
		SetMyRotationFilterParam(param, _myRotationFilterType, _myRotationInfo);
		if (_myRotationFilterType == MyRotationFilterType.CARD_POOL_ALL_PACK || _isMyRotationAllPackVisible)
		{
			List<CardSetName> availableCardSetNameList = formatBehavior.AvailableCardSetNameList;
			List<string> list = new List<string>(availableCardSetNameList.Count);
			foreach (CardSetName item in availableCardSetNameList)
			{
				if (!Data.MyRotationAllInfo.DisableCardPackIdList.Contains(item.ID))
				{
					if (!_packButtonFlagList[0] && _packButtonFlagList[num])
					{
						list.Add(item.ID);
					}
					num++;
				}
			}
			param.CardSetIdList = list;
			if (formatBehavior.IsShowPrizeCardSetFilter)
			{
				param.IsEnabledPrizeCard = _packButtonFlagList[num];
				num++;
			}
			if (formatBehavior.IsShowPhantomCardSetFilter)
			{
				param.IsEnabledPhantomCard = _packButtonFlagList[num];
				num++;
			}
			return;
		}
		List<string> list2 = new List<string>(_myRotationInfo.CardPadkIdList.Count);
		foreach (string cardPadkId in _myRotationInfo.CardPadkIdList)
		{
			if (_packButtonFlagList[num])
			{
				list2.Add(cardPadkId);
			}
			num++;
		}
		param.CardSetIdList = list2;
		if (formatBehavior.IsShowPrizeCardSetFilter)
		{
			param.IsEnabledPrizeCard = _packButtonFlagList[num];
			num++;
		}
		if (formatBehavior.IsShowPhantomCardSetFilter)
		{
			param.IsEnabledPhantomCard = _packButtonFlagList[num];
			num++;
		}
	}

	public static int GetClassFilterMask(CardBasePrm.ClanType classType)
	{
		if (classType == CardBasePrm.ClanType.NONE)
		{
			return 1;
		}
		return 1 << (int)classType;
	}

	public void Show()
	{
		base.gameObject.SetActive(value: true);
		IsShow = true;
		_scrollView.ResetPosition();
		IFormatBehavior formatBehavior = _formatBehavior;
		SetFilterVisible(eFILTER_TYPE.FORMAT, formatBehavior.IsShowFormatFilter);
		if (formatBehavior.IsShowFormatFilter)
		{
			_labelRotationBtn.text = Data.SystemText.Get("Common_0154");
			_labelUnlimitedBtn.text = Data.SystemText.Get("Common_0155");
			_tableRotationBtn.Reposition();
			_tableUnlimitedBtn.Reposition();
		}
		SetFilterVisible(eFILTER_TYPE.FAVORITE, formatBehavior.IsShowFavoriteFilter);
		SetFilterVisible(eFILTER_TYPE.SPOT, formatBehavior.IsShowSpotCardFilter);
		if (ClassSet.MainClass != CardBasePrm.ClanType.ALL)
		{
			ButtonArray buttonArray = BtnArray[5];
			if (formatBehavior.UseSubClass)
			{
				for (int i = 2; i < buttonArray.Length; i++)
				{
					buttonArray[i].gameObject.SetActive(i == GetClassBtnIndex(ClassSet.MainClass) || i == GetClassBtnIndex(ClassSet.SubClass));
				}
			}
			else
			{
				for (int j = 2; j < buttonArray.Length; j++)
				{
					buttonArray[j].gameObject.SetActive(j == GetClassBtnIndex(ClassSet.MainClass));
				}
			}
		}
		SetPackButtons();
		ResetBasicFilterLinePosition();
		_basicGrid.Reposition();
		_scrollView.ResetPosition();
		_detailGrid.Reposition();
	}

	private void ResetBasicFilterLinePosition()
	{
		RepositionLine(_linePack, _gridPack, 38f);
		RepositionLine(_lineClass, _gridClass, 50f);
	}

	private void RepositionLine(Transform lineTransform, UIGrid grid, float margin)
	{
		grid.Reposition();
		int num = 0;
		if (grid.maxPerLine > 0 && grid.GetChildList().Count > 0)
		{
			num = (grid.GetChildList().Count - 1) / grid.maxPerLine;
		}
		float num2 = (float)num * grid.cellHeight + margin;
		lineTransform.localPosition = new Vector3(lineTransform.localPosition.x, 0f - num2, lineTransform.localPosition.z);
	}

	public void Hide()
	{
		base.gameObject.SetActive(value: false);
		IsShow = false;
	}

	public void Reset(bool isEnableValidateCall = true)
	{
		for (int i = 0; i < 10; i++)
		{
			FlagsArray[i] = 1;
			RenameSpriteArray((eFILTER_TYPE)i, FlagsArray[i]);
		}
		ResetPackButtonFlags();
		UpdatePackButtonSprites();
		if (_keywordFilterList != null)
		{
			_keywordFilterList.Clear();
		}
		if (_filterTypeList != null)
		{
			_filterTypeList.Clear();
		}
		RemoveCurrentKeyWordList();
		RemoveCurrentTypeFilterList();
		_characterVoiceSearchInput.value = "";
		_detailGrid.Reposition();
		OnUpdateTypeOrKeyWordFilter();
		if (isEnableValidateCall)
		{
			this.OnValidate.Call();
		}
		_spriteRotationBtn.spriteName = RenameSpriteString(_spriteRotationBtn.spriteName, isEnable: false);
		_spriteUnlimitedBtn.spriteName = RenameSpriteString(_spriteUnlimitedBtn.spriteName, isEnable: false);
	}

	private void SetFilterVisible(eFILTER_TYPE filterType, bool isVisible)
	{
		BtnArray[(int)filterType][0].transform.parent.gameObject.SetActive(isVisible);
	}

	private int GetClassBtnIndex(CardBasePrm.ClanType ClassType)
	{
		return (int)(ClassType + 1);
	}

	public Format GetFormatBtnState()
	{
		int target = FlagsArray[6];
		if (IsEnableBit(ref target, GetFormatBtnIndex(Format.Rotation)))
		{
			return Format.Rotation;
		}
		if (IsEnableBit(ref target, GetFormatBtnIndex(Format.Unlimited)))
		{
			return Format.Unlimited;
		}
		return Format.Max;
	}

	private int GetFormatBtnIndex(Format inFormat)
	{
		int result = 0;
		if (inFormat == Format.Rotation || inFormat == Format.Unlimited)
		{
			result = (int)(inFormat + 1);
		}
		return result;
	}

	private void OnClickResetBtn(GameObject obj)
	{
		Reset();
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
	}

	private void SetEventOnClickBtn(GameObject btn, eFILTER_TYPE type, int index)
	{
		UIEventListener.Get(btn).onClick = delegate
		{
			OnClickBtn(type, index);
		};
	}

	private void OnClickBtn(eFILTER_TYPE type, int index)
	{
		if (type == eFILTER_TYPE.FORMAT)
		{
			OnClickRadioBtn(type, index);
			UpdateFormatIconInBtn(GetFormatBtnState());
		}
		else
		{
			if (index == 0)
			{
				FlagsArray[(int)type] = 1;
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_TOGGLE_ON);
			}
			else
			{
				DisableBit(ref FlagsArray[(int)type], 0);
				InvertBit(ref FlagsArray[(int)type], index);
				if (FlagsArray[(int)type] == 0)
				{
					FlagsArray[(int)type] = 1;
				}
				GameMgr.GetIns().GetSoundMgr().PlaySe(IsEnableBit(ref FlagsArray[(int)type], index) ? Se.TYPE.SYS_TOGGLE_ON : Se.TYPE.SYS_TOGGLE_OFF);
			}
			RenameSpriteArray(type, FlagsArray[(int)type]);
		}
		this.OnValidate.Call();
	}

	private void UpdateFormatIconInBtn(Format inFormat)
	{
		switch (inFormat)
		{
		case Format.Rotation:
			_spriteRotationBtn.spriteName = RenameSpriteString(_spriteRotationBtn.spriteName, isEnable: true);
			_spriteUnlimitedBtn.spriteName = RenameSpriteString(_spriteUnlimitedBtn.spriteName, isEnable: false);
			break;
		case Format.Unlimited:
			_spriteRotationBtn.spriteName = RenameSpriteString(_spriteRotationBtn.spriteName, isEnable: false);
			_spriteUnlimitedBtn.spriteName = RenameSpriteString(_spriteUnlimitedBtn.spriteName, isEnable: true);
			break;
		default:
			_spriteRotationBtn.spriteName = RenameSpriteString(_spriteRotationBtn.spriteName, isEnable: false);
			_spriteUnlimitedBtn.spriteName = RenameSpriteString(_spriteUnlimitedBtn.spriteName, isEnable: false);
			break;
		}
	}

	private void OnClickRadioBtn(eFILTER_TYPE type, int index)
	{
		UpdateRadioBtn(type, index);
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_TOGGLE_ON);
	}

	private void UpdateRadioBtn(eFILTER_TYPE type, int index)
	{
		if (!IsEnableBit(ref FlagsArray[(int)type], index))
		{
			FlagsArray[(int)type] = 0;
			InvertBit(ref FlagsArray[(int)type], index);
			RenameSpriteArray(type, FlagsArray[(int)type]);
		}
	}

	private void EnableBit(ref int target, int index)
	{
		int num = 1 << index;
		target |= num;
	}

	private void DisableBit(ref int target, int index)
	{
		int num = 1 << index;
		target &= ~num;
	}

	private void InvertBit(ref int target, int index)
	{
		int num = 1 << index;
		target ^= num;
	}

	private bool IsEnableBit(ref int target, int index)
	{
		int num = 1 << index;
		return (target & num) != 0;
	}

	private void RenameSpriteArray(eFILTER_TYPE type, int bitStream)
	{
		ButtonArray buttonArray = BtnArray[(int)type];
		for (int i = 0; i < buttonArray.Length; i++)
		{
			RenameBtnSprite(buttonArray[i], (bitStream >> i) % 2 != 0);
		}
	}

	private void RenameBtnSprite(UIButton button, bool isEnable)
	{
		button.normalSprite = RenameSpriteString(button.normalSprite, isEnable);
	}

	private string RenameSpriteString(string str, bool isEnable)
	{
		string oldValue = (isEnable ? "_off" : "_on");
		string newValue = (isEnable ? "_on" : "_off");
		return str.Replace(oldValue, newValue);
	}

	public void SetMyRotationData(MyRotationInfo myRotationInfo, MyRotationFilterType myRotationFilterType, bool isAllBackVisible)
	{
		if (myRotationInfo == _myRotationInfo && myRotationFilterType == _myRotationFilterType && _isMyRotationAllPackVisible == isAllBackVisible)
		{
			return;
		}
		_myRotationFilterType = myRotationFilterType;
		_myRotationInfo = myRotationInfo;
		_isMyRotationAllPackVisible = isAllBackVisible;
		if (_packBtnList.Count > 0)
		{
			foreach (UIButton packBtn in _packBtnList)
			{
				UnityEngine.Object.Destroy(packBtn.gameObject);
			}
			_packBtnList.Clear();
		}
		SetPackButtons();
	}

	private void SetPackButtons()
	{
		if (_packBtnList.Count > 0)
		{
			return;
		}
		_packBtnOrigin.gameObject.SetActive(value: false);
		IFormatBehavior formatBehavior = _formatBehavior;
		CreatePackButton(Data.SystemText.Get("Card_0040")).GetComponentInChildren<UILabel>().height = 26;
		if (_myRotationInfo == null)
		{
			foreach (CardSetName availableCardSetName in formatBehavior.AvailableCardSetNameList)
			{
				CreatePackButton(availableCardSetName.ShortName);
			}
			if (formatBehavior.IsShowPrizeCardSetFilter)
			{
				CreatePackButton(Data.SystemText.Get("Common_0157"));
			}
			if (formatBehavior.IsShowPhantomCardSetFilter)
			{
				CreatePackButton(Data.SystemText.Get("PhantomCardPackName_short"));
			}
		}
		else
		{
			if (_myRotationFilterType == MyRotationFilterType.CARD_POOL_ALL_PACK || _isMyRotationAllPackVisible)
			{
				foreach (CardSetName availableCardSetName2 in formatBehavior.AvailableCardSetNameList)
				{
					if (!Data.MyRotationAllInfo.DisableCardPackIdList.Contains(availableCardSetName2.ID))
					{
						CreatePackButton(availableCardSetName2.ShortName);
					}
				}
			}
			else
			{
				foreach (string cardPadkId in _myRotationInfo.CardPadkIdList)
				{
					CreatePackButton(Data.Master.CardSetNameMgr.Get(cardPadkId).ShortName);
				}
			}
			if (formatBehavior.IsShowPrizeCardSetFilter)
			{
				CreatePackButton(Data.SystemText.Get("Common_0157"));
			}
		}
		_gridPack.Reposition();
		int num = 1;
		for (int i = 0; i < _packBtnList.Count; i++)
		{
			if (!_packBtnList[i].gameObject.activeSelf)
			{
				continue;
			}
			int num2 = num % _gridPack.maxPerLine;
			if (num2 == 1)
			{
				if (num == _packBtnList.Count)
				{
					_packBtnList[i].normalSprite = "pilltab_02_single_off";
					_packBtnList[i].pressedSprite = "pilltab_02_single_on";
				}
				else
				{
					_packBtnList[i].normalSprite = "pilltab_02_left_off";
					_packBtnList[i].pressedSprite = "pilltab_02_left_on";
				}
			}
			else if (num2 == 0 || i == _packBtnList.Count - 1)
			{
				_packBtnList[i].normalSprite = "pilltab_02_right_off";
				_packBtnList[i].pressedSprite = "pilltab_02_right_on";
			}
			else
			{
				_packBtnList[i].normalSprite = "pilltab_02_middle_off";
				_packBtnList[i].pressedSprite = "pilltab_02_middle_on";
			}
			num++;
		}
		ResetPackButtonFlags();
		UpdatePackButtonSprites();
	}

	private GameObject CreatePackButton(string btnText)
	{
		GameObject obj = NGUITools.AddChild(_gridPack.gameObject, _packBtnOrigin.gameObject);
		obj.SetActive(value: true);
		obj.GetComponentInChildren<UILabel>().text = btnText;
		UIButton component = obj.GetComponent<UIButton>();
		int count = _packBtnList.Count;
		SetEventOnClickPackButton(component, count);
		_packBtnList.Add(component);
		_packButtonFlagList.Add(item: false);
		return obj;
	}

	private void SetEventOnClickPackButton(UIButton button, int index)
	{
		EventDelegate.Add(button.onClick, delegate
		{
			OnClickPackButton(index);
		});
	}

	private void OnClickPackButton(int index)
	{
		if (index == 0)
		{
			ResetPackButtonFlags();
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_TOGGLE_ON);
		}
		else
		{
			_packButtonFlagList[0] = false;
			_packButtonFlagList[index] = !_packButtonFlagList[index];
			if (_packButtonFlagList.All((bool flag2) => !flag2))
			{
				_packButtonFlagList[0] = true;
			}
			bool flag = _packButtonFlagList[index];
			GameMgr.GetIns().GetSoundMgr().PlaySe(flag ? Se.TYPE.SYS_TOGGLE_ON : Se.TYPE.SYS_TOGGLE_OFF);
		}
		UpdatePackButtonSprites();
		this.OnValidate.Call();
	}

	private void ResetPackButtonFlags()
	{
		if (_packButtonFlagList.Count != 0)
		{
			_packButtonFlagList[0] = true;
			int i = 1;
			for (int count = _packButtonFlagList.Count; i < count; i++)
			{
				_packButtonFlagList[i] = false;
			}
		}
	}

	private void UpdatePackButtonSprites()
	{
		int i = 0;
		for (int count = _packBtnList.Count; i < count; i++)
		{
			RenameBtnSprite(_packBtnList[i], _packButtonFlagList[i]);
		}
	}

	private void OnClickKeyWord()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
		_keywordFilter = CardDetailFilterDialog.Create(_detailFilterPrefab, _keywordFilterList, _existKeyWordList);
		_keywordFilter.Dialog.OnClose = delegate
		{
			_keywordFilterList = _keywordFilter.GetFilterList();
			RefreshCurrentKeyWordList();
			_detailGrid.Reposition();
		};
		_keywordFilter.OnChange = delegate
		{
			_keywordFilterList = _keywordFilter.GetFilterList();
			this.OnValidate.Call();
		};
	}

	private void OnClickTypeFilter()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
		TypeFilterDialog typeFilter = TypeFilterDialog.Create(_typeFilterDialogPrefab.gameObject, _filterTypeList, _typeFilterList);
		typeFilter.Dialog.OnClose = delegate
		{
			RefreshCurrentTypeFilterList();
		};
		typeFilter.OnChange = delegate
		{
			_filterTypeList = typeFilter.GetAllSelectType();
			this.OnValidate.Call();
		};
	}

	private void RefreshCurrentKeyWordList()
	{
		RemoveCurrentKeyWordList();
		if (_keywordFilterList == null)
		{
			return;
		}
		foreach (string keywordFilter in _keywordFilterList)
		{
			GameObject gameObject = NGUITools.AddChild(_kewordListGrid.gameObject, _filterOffButtonOriginal.gameObject);
			gameObject.name = keywordFilter;
			_currentFilterList.Add(gameObject);
			gameObject.SetActive(value: true);
			CardDetailFilterOffButton offButton = gameObject.GetComponent<CardDetailFilterOffButton>();
			offButton.Initialize(keywordFilter);
			offButton.OnClick = delegate(string keyword)
			{
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_TOGGLE_OFF);
				_currentFilterList.Remove(offButton.gameObject);
				UnityEngine.Object.DestroyImmediate(offButton.gameObject);
				_kewordListGrid.Reposition();
				_keywordFilterList.Remove(keyword);
				this.OnValidate.Call();
				_detailGrid.Reposition();
				OnUpdateTypeOrKeyWordFilter();
			};
		}
		_kewordListGrid.Reposition();
		OnUpdateTypeOrKeyWordFilter();
	}

	private void RemoveCurrentKeyWordList()
	{
		foreach (GameObject currentFilter in _currentFilterList)
		{
			UnityEngine.Object.DestroyImmediate(currentFilter);
		}
		_currentFilterList.Clear();
	}

	private void RefreshCurrentTypeFilterList()
	{
		RemoveCurrentTypeFilterList();
		foreach (CardBasePrm.TribeType type in _filterTypeList)
		{
			string tribeNameByKey = DataMgr.GetTribeNameByKey((int)type);
			GameObject gameObject = NGUITools.AddChild(_typeListGrid.gameObject, _filterOffButtonOriginal.gameObject);
			gameObject.name = tribeNameByKey;
			_currentTypeFilterList.Add(gameObject);
			gameObject.SetActive(value: true);
			CardDetailFilterOffButton offButton = gameObject.GetComponent<CardDetailFilterOffButton>();
			offButton.Initialize(tribeNameByKey);
			offButton.OnClick = delegate
			{
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_TOGGLE_OFF);
				_currentTypeFilterList.Remove(offButton.gameObject);
				_filterTypeList.Remove(type);
				UnityEngine.Object.DestroyImmediate(offButton.gameObject);
				this.OnValidate.Call();
				_typeListGrid.Reposition();
				_detailGrid.Reposition();
				OnUpdateTypeOrKeyWordFilter();
			};
		}
		_typeListGrid.Reposition();
		_detailGrid.Reposition();
		OnUpdateTypeOrKeyWordFilter();
	}

	private void OnUpdateTypeOrKeyWordFilter()
	{
		_scrollView.RestrictWithinBounds(instant: false);
		_scrollView.UpdateScrollbars(recalculateBounds: true);
	}

	private void RemoveCurrentTypeFilterList()
	{
		foreach (GameObject currentTypeFilter in _currentTypeFilterList)
		{
			UnityEngine.Object.DestroyImmediate(currentTypeFilter);
		}
		_currentTypeFilterList.Clear();
		_typeListGrid.Reposition();
	}

	private List<int> RemoveTokenCard(List<int> cardPool)
	{
		List<int> list = new List<int>(cardPool.Count);
		CardMaster instance = CardMaster.GetInstance(_formatBehavior.CardMasterId);
		foreach (int item in cardPool)
		{
			if (!instance.GetCardParameterFromId(item).IsTokenCard)
			{
				list.Add(item);
			}
		}
		return list;
	}

	public void InitializeFilterForDeckEdit(List<int> cardPool, ClassSet classSet, Format format, MyRotationInfo myRotationInfo, MyRotationFilterType myRotationFilterType)
	{
		UIBase_CardManager.FilterParameter filterParameter = new UIBase_CardManager.FilterParameter();
		if (classSet.SubClass == CardBasePrm.ClanType.NONE)
		{
			filterParameter.Class = GetClassFilterMask(CardBasePrm.ClanType.NONE) | GetClassFilterMask(classSet.MainClass);
		}
		else
		{
			filterParameter.Class = GetClassFilterMask(CardBasePrm.ClanType.NONE) | GetClassFilterMask(classSet.MainClass) | GetClassFilterMask(classSet.SubClass);
		}
		filterParameter.Craftable = 1;
		filterParameter.FormatState = format;
		filterParameter.TypeFilter = new List<CardBasePrm.TribeType>();
		filterParameter.FixedClassSet = classSet;
		_myRotationInfo = myRotationInfo;
		if (myRotationInfo != null)
		{
			SetMyRotationFilterParam(filterParameter, myRotationFilterType, myRotationInfo);
		}
		ClassSet = classSet;
		cardPool = RemoveTokenCard(cardPool);
		UpdateAllSelectableKeyWord(UIManager.GetInstance().getUIBase_CardManager().SelectCardIDInConditionMask(cardPool, filterParameter, _formatBehavior, myRotationInfo, alreadySorted: true));
		SetTypeFilterSetting(cardPool, filterParameter, myRotationFilterType);
		UIManager.GetInstance().getUIBase_CardManager().AddKeyWordCache(cardPool, _formatBehavior.CardMasterId);
	}

	public static void SetMyRotationFilterParam(UIBase_CardManager.FilterParameter filter, MyRotationFilterType myRotetionFilterType, MyRotationInfo myRotationInfo)
	{
		if (myRotetionFilterType != MyRotationFilterType.DECK)
		{
			filter.DisableCardSetidList = Data.MyRotationAllInfo.DisableCardPackIdList;
		}
		IFormatBehavior defaultBehaviour = FormatBehaviorManager.GetDefaultBehaviour(Format.MyRotation);
		switch (myRotetionFilterType)
		{
		case MyRotationFilterType.DECK:
			filter.CardSetIdList2 = new List<string>();
			break;
		case MyRotationFilterType.CARD_POOL_ALL_PACK:
		{
			List<CardSetName> availableCardSetNameList = defaultBehaviour.AvailableCardSetNameList;
			List<string> list = new List<string>();
			foreach (CardSetName item in availableCardSetNameList)
			{
				if (!Data.MyRotationAllInfo.DisableCardPackIdList.Contains(item.ID))
				{
					list.Add(item.ID);
				}
			}
			filter.CardSetIdList2 = list;
			break;
		}
		case MyRotationFilterType.CARD_POOL_SELECT_ONLY:
			filter.CardSetIdList2 = myRotationInfo.CardPadkIdList;
			break;
		}
	}

	public void InitializeFilterForAllCardList(MyRotationInfo myRotationInfo)
	{
		List<int> cardPool = UIManager.GetInstance().getUIBase_CardManager().SortIDList(CardMaster.GetInstance(_formatBehavior.CardMasterId).GetAllCardIds(), _formatBehavior.CardMasterId);
		cardPool = RemoveTokenCard(cardPool);
		UIBase_CardManager.FilterParameter filterParameter = new UIBase_CardManager.FilterParameter();
		filterParameter.Craftable = 1;
		filterParameter.TypeFilter = new List<CardBasePrm.TribeType>();
		ClassSet = new ClassSet(CardBasePrm.ClanType.ALL);
		UpdateAllSelectableKeyWord(UIManager.GetInstance().getUIBase_CardManager().SelectCardIDInConditionMask(cardPool, filterParameter, _formatBehavior, myRotationInfo, alreadySorted: true));
		SetTypeFilterSetting(cardPool, filterParameter, MyRotationFilterType.None);
		UIManager.GetInstance().getUIBase_CardManager().AddKeyWordCache(cardPool, _formatBehavior.CardMasterId);
	}

	public void InitializeForCardDestruct(IFormatBehavior formatBehavior, MyRotationInfo myRotationInfo)
	{
		Initialize(formatBehavior);
		InitializeFilterForAllCardList(myRotationInfo);
	}

	public void UpdateTypeFilterForDeckEdit(List<int> cardPool, ClassSet classSet, Format format, MyRotationInfo myRotationInfo, MyRotationFilterType myRotationFilterType)
	{
		InitializeFilterForDeckEdit(cardPool, classSet, format, myRotationInfo, myRotationFilterType);
	}

	private void SetTypeFilterSetting(List<int> cardPool, UIBase_CardManager.FilterParameter filter, MyRotationFilterType myRotationFilterType)
	{
		_typeFilterList = new List<CardBasePrm.TribeType>();
		IList<int> list = UIManager.GetInstance().getUIBase_CardManager().SelectCardIDInConditionMask(cardPool, filter, _formatBehavior, _myRotationInfo, alreadySorted: true);
		List<CardParameter> list2 = new List<CardParameter>(list.Count);
		CardMaster instance = CardMaster.GetInstance(_formatBehavior.CardMasterId);
		foreach (int item in list)
		{
			list2.Add(instance.GetCardParameterFromId(item));
		}
		foreach (CardBasePrm.TribeType value in Enum.GetValues(typeof(CardBasePrm.TribeType)))
		{
			if (value == CardBasePrm.TribeType.ALL || value == CardBasePrm.TribeType.MAX)
			{
				continue;
			}
			foreach (CardParameter item2 in list2)
			{
				if (!item2.IsTribeAll && item2.Tribe.Contains(value))
				{
					_typeFilterList.Add(value);
					break;
				}
			}
		}
	}

	private void UpdateAllSelectableKeyWord(IList<int> cardPool)
	{
		_existKeyWordList = new Dictionary<string, bool>();
		CardMaster instance = CardMaster.GetInstance(_formatBehavior.CardMasterId);
		foreach (int item in cardPool)
		{
			CardParameter cardParameterFromId = instance.GetCardParameterFromId(item);
			foreach (string item2 in UIManager.GetInstance().getUIBase_CardManager().GetKeyword(cardParameterFromId))
			{
				_existKeyWordList[item2] = true;
			}
		}
	}

	private void OnInputCharacterVoiceSearch()
	{
		this.OnValidate.Call();
	}
}
