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

namespace Wizard;

public class GachaResultBuyCardPack : UIBase
{
	private const int MAX_ROW_IN_PAGE = 2;

	private readonly int[] MAX_COLUMN_RARELITY = new int[4] { 8, 8, 8, 8 };

	private const float CARD_SCALE = 0.55f;

	private readonly Vector3 GRID_LOCAL_POSITION = Vector3.up * -6f;

	private readonly Vector3 GRID_ONLY_ONE_LOCAL_POSITION = Vector3.up * -15f;

	private readonly Vector3 NEW_LABEL_POSITION = Vector3.up * -150f;

	private const string FREE_PACK_LEADER_SKIN_EFFECT = "gch_result_skin_1";

	private bool m_isCenterLoading;

	[SerializeField]
	private UIButton m_buttonNext;

	[SerializeField]
	private UIButton m_buttonPrev;

	[SerializeField]
	private UIGrid gridLegendary;

	[SerializeField]
	private UIGrid gridGold;

	[SerializeField]
	private UIGrid gridSilver;

	[SerializeField]
	private UIGrid gridBronze;

	[SerializeField]
	private GameObject cardObjPool;

	[SerializeField]
	private GameObject parentLegendary;

	[SerializeField]
	private GameObject parentGold;

	[SerializeField]
	private GameObject parentSilver;

	[SerializeField]
	private GameObject parentBronze;

	[SerializeField]
	private UILabel labelLegendary;

	[SerializeField]
	private UILabel labelGold;

	[SerializeField]
	private UILabel labelSilver;

	[SerializeField]
	private UILabel labelBronze;

	[SerializeField]
	private GameObject m_RarityParentRow1;

	[SerializeField]
	private GameObject m_RarityParentRow2;

	[SerializeField]
	private UIToggle m_IndicatorBase;

	[SerializeField]
	private UITexture _particleTexture;

	private List<List<CardPack>> m_CardPackListPerPage = new List<List<CardPack>>();

	private Dictionary<int, List<UIBase_CardManager.CardObjData>> m_Page_LoadedCard_Dictionary = new Dictionary<int, List<UIBase_CardManager.CardObjData>>();

	private Dictionary<int, int> m_CardId_Num_Dictionary = new Dictionary<int, int>();

	private List<string> m_LoadedAssetPathList = new List<string>();

	private int currentPage;

	private List<int> _specialEffectCardList = new List<int>();

	private List<UIToggle> m_IndicaatorList = new List<UIToggle>();

	private CardDetailUI m_cardDetailUI;

	private bool m_isInitLoadFinish;

	private bool _isLoadEffectFinish;

	private List<string> _loadEffectList = new List<string>();

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

	private bool _isWaitingEffectMaterial;

	private UIParticleEffectGroup _particleGroup;

	public bool IsFlickEnable { get; set; }

	public bool IsLoading { get; private set; }

	public List<string> _loadedAssetPathList => m_LoadedAssetPathList;

	public bool IsDialogCloseStart { get; set; }

	private void Update()
	{
		if (!m_isCenterLoading && m_isInitLoadFinish && GameMgr.GetIns().GetInputMgr().IsFlick() && IsFlickEnable)
		{
			if (GameMgr.GetIns().GetInputMgr().GetFlickVec()
				.x < 0f)
			{
				OnNextButton();
			}
			else
			{
				OnPrevButton();
			}
		}
	}

	public void Init(List<CardPack> cardPackList, CardDetailUI cardDetailUI)
	{
		m_cardDetailUI = cardDetailUI;
		IsFlickEnable = true;
		List<CardPack> list = new List<CardPack>();
		for (int i = 0; i < cardPackList.Count; i++)
		{
			int card_id = cardPackList[i].card_id;
			if (m_CardId_Num_Dictionary.ContainsKey(card_id))
			{
				m_CardId_Num_Dictionary[card_id]++;
				continue;
			}
			m_CardId_Num_Dictionary.Add(card_id, 1);
			list.Add(cardPackList[i]);
		}
		foreach (CardPack cardPack in cardPackList)
		{
			if (cardPack.IsFreePackLeaderSkin)
			{
				_specialEffectCardList.Add(cardPack.card_id);
			}
		}
		List<CardPack> list2 = new List<CardPack>();
		list2.AddRange(SortCardList(list.FindAll((CardPack m) => m.rarity == 4)));
		list2.AddRange(SortCardList(list.FindAll((CardPack m) => m.rarity == 3)));
		list2.AddRange(SortCardList(list.FindAll((CardPack m) => m.rarity == 2)));
		list2.AddRange(SortCardList(list.FindAll((CardPack m) => m.rarity == 1)));
		DividCardListEachPages(list2);
		GameObject gameObject = m_IndicatorBase.transform.parent.gameObject;
		if (m_CardPackListPerPage.Count <= 1)
		{
			gameObject.SetActive(value: false);
			return;
		}
		gameObject.SetActive(value: true);
		m_IndicaatorList.Add(m_IndicatorBase);
		for (int num = 1; num < m_CardPackListPerPage.Count; num++)
		{
			m_IndicaatorList.Add(NGUITools.AddChild(gameObject, m_IndicatorBase.gameObject).GetComponent<UIToggle>());
		}
		gameObject.GetComponent<UIGrid>().Reposition();
	}

	public IEnumerator ViewCardList(int page)
	{
		currentPage = page;
		if (page > 1)
		{
			m_buttonPrev.gameObject.SetActive(value: true);
		}
		else
		{
			m_buttonPrev.gameObject.SetActive(value: false);
		}
		if (page < m_CardPackListPerPage.Count)
		{
			m_buttonNext.gameObject.SetActive(value: true);
		}
		else
		{
			m_buttonNext.gameObject.SetActive(value: false);
		}
		if (m_IndicaatorList.Count > 1)
		{
			m_IndicaatorList[page - 1].value = true;
		}
		RemoveCardObjFromGrid(gridLegendary);
		RemoveCardObjFromGrid(gridGold);
		RemoveCardObjFromGrid(gridSilver);
		RemoveCardObjFromGrid(gridBronze);
		if (m_Page_LoadedCard_Dictionary.ContainsKey(page))
		{
			CompleteLoadCard();
		}
		else if (IsLoading)
		{
			SetCenterLoading(isVisible: true);
			labelLegendary.gameObject.SetActive(value: false);
			labelGold.gameObject.SetActive(value: false);
			labelSilver.gameObject.SetActive(value: false);
			labelBronze.gameObject.SetActive(value: false);
			while (IsLoading)
			{
				yield return null;
			}
			CompleteLoadCard();
		}
		else
		{
			yield return StartCoroutine(LoadCardList(page, CompleteLoadCard));
		}
	}

	private void CompleteLoadCard()
	{
		AddCardObjToGrid(m_Page_LoadedCard_Dictionary[currentPage]);
		SetCenterLoading(isVisible: false);
		if (m_Page_LoadedCard_Dictionary.Count < m_CardPackListPerPage.Count && m_Page_LoadedCard_Dictionary.Count == currentPage && !IsLoading)
		{
			StartCoroutine(LoadCardList(currentPage + 1, delegate
			{
				m_isInitLoadFinish = true;
			}));
		}
	}

	protected override void OnDestroy()
	{
		base.OnDestroy();
		DisposeEffect();
	}

	private IEnumerator LoadEffect()
	{
		if (!_isLoadEffectFinish)
		{
			_loadEffectList.Add(Toolbox.ResourcesManager.GetAssetTypePath("gch_result_skin_1", ResourcesManager.AssetLoadPathType.Effect2D));
			yield return StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(_loadEffectList, delegate
			{
			}));
			_isLoadEffectFinish = true;
		}
	}

	private void DisposeEffect()
	{
		if (_loadEffectList.Count != 0)
		{
			Toolbox.ResourcesManager.RemoveAssetGroup(_loadEffectList);
			_loadEffectList.Clear();
			ReleaseParticleEffectGroup();
		}
	}

	private IEnumerator LoadCardList(int page, Action callback = null)
	{
		if (m_Page_LoadedCard_Dictionary.ContainsKey(page))
		{
			callback.Call();
			yield break;
		}
		IsLoading = true;
		if (_specialEffectCardList.Count > 0)
		{
			yield return LoadEffect();
		}
		while (_isWaitingEffectMaterial)
		{
			yield return null;
		}
		List<int> idList = m_CardPackListPerPage[page - 1].ConvertAll((CardPack conv) => conv.card_id);
		yield return null;
		UIManager.GetInstance().CardLoadGachaPack(base.gameObject, idList, 24, is2D: true);
		while (!UIManager.GetInstance().getUIBase_CardManager().getCreateEndFlag() || !UIManager.GetInstance().getUIBase_CardManager().isAssetAllReady)
		{
			yield return null;
		}
		List<UIBase_CardManager.CardObjData> cardList2DObjs = UIManager.GetInstance().getCardList2DObjs();
		m_Page_LoadedCard_Dictionary.Add(page, cardList2DObjs);
		m_LoadedAssetPathList.AddRange(Toolbox.ResourcesManager.CardListAssetPathList);
		Toolbox.ResourcesManager.CardListAssetPathList.Clear();
		Toolbox.ResourcesManager.Card2DAssetPathList.Clear();
		for (int num = 0; num < cardList2DObjs.Count; num++)
		{
			GameObject cardObj = cardList2DObjs[num].CardObj;
			CardListTemplate component = cardObj.GetComponent<CardListTemplate>();
			component.SetNum(m_CardId_Num_Dictionary[cardList2DObjs[num].ids]);
			cardObj.AddComponent<BoxCollider>().size = component._cardTexture.localSize;
			UIEventListener.Get(cardObj).onClick = m_cardDetailUI.OnPushCardDetailOn;
			cardObj.transform.parent = cardObjPool.transform;
			cardObj.transform.localPosition = Vector3.zero;
			cardObj.SetActive(value: false);
			cardObj.SetActive(value: true);
		}
		IsLoading = false;
		callback.Call();
	}

	private void SetCenterLoading(bool isVisible)
	{
		m_isCenterLoading = isVisible;
		if (isVisible)
		{
			UIManager.GetInstance().createInSceneCenterLoading();
		}
		else
		{
			UIManager.GetInstance().closeInSceneCenterLoading();
		}
	}

	public void OnNextButton()
	{
		if (!m_isCenterLoading && m_isInitLoadFinish && !IsDialogCloseStart && !m_cardDetailUI.GetIsDetailOn() && currentPage < m_CardPackListPerPage.Count)
		{
			StartCoroutine(ViewCardList(++currentPage));
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_SLIDE_BTN);
		}
	}

	public void OnPrevButton()
	{
		if (!m_isCenterLoading && m_isInitLoadFinish && !IsDialogCloseStart && !m_cardDetailUI.GetIsDetailOn() && currentPage > 1)
		{
			StartCoroutine(ViewCardList(--currentPage));
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_SLIDE_BTN);
		}
	}

	private List<CardPack> SortCardList(List<CardPack> cardList)
	{
		List<int> idList = cardList.ConvertAll((CardPack conv) => conv.card_id);
		idList = UIManager.GetInstance().getUIBase_CardManager().SortIDList(idList, CardMaster.CardMasterId.Default);
		cardList = idList.ConvertAll((int conv) => cardList.Find((CardPack find) => find.card_id == conv));
		return cardList;
	}

	private void DividCardListEachPages(List<CardPack> cardPackList)
	{
		List<CardPack> list = new List<CardPack>();
		int rarity = cardPackList[0].rarity;
		int num = 1;
		int num2 = 0;
		for (int i = 0; i < cardPackList.Count; i++)
		{
			if (cardPackList[i].rarity <= MAX_COLUMN_RARELITY.Length)
			{
				num2++;
				if (rarity != cardPackList[i].rarity)
				{
					num++;
					num2 = 1;
					rarity = cardPackList[i].rarity;
				}
				else if (num2 > MAX_COLUMN_RARELITY[rarity - 1])
				{
					num++;
					num2 = 1;
				}
				if (num > 2)
				{
					num = 1;
					m_CardPackListPerPage.Add(list);
					list = new List<CardPack>();
					list.Add(cardPackList[i]);
				}
				else
				{
					list.Add(cardPackList[i]);
				}
			}
		}
		if (list.Count > 0)
		{
			m_CardPackListPerPage.Add(list);
		}
	}

	private void RemoveCardObjFromGrid(UIGrid gridObj)
	{
		List<Transform> childList = gridObj.GetChildList();
		for (int i = 0; i < childList.Count; i++)
		{
			childList[i].transform.parent = cardObjPool.transform;
			childList[i].transform.localPosition = Vector3.zero;
		}
	}

	private void ReleaseParticleEffectGroup()
	{
		if (_particleGroup != null)
		{
			UIParticleEffectManager.Instance.Remove(_particleGroup);
			_particleGroup = null;
		}
	}

	private void AddCardObjToGrid(List<UIBase_CardManager.CardObjData> cardObjList)
	{
		bool flag = false;
		bool flag2 = false;
		bool flag3 = false;
		bool flag4 = false;
		foreach (GameObject effect in _effectList)
		{
			UnityEngine.Object.Destroy(effect);
		}
		_effectList.Clear();
		ReleaseParticleEffectGroup();
		for (int i = 0; i < cardObjList.Count; i++)
		{
			CardParameter cardParameterFromId = CardMaster.GetInstance(CardMaster.CardMasterId.Default).GetCardParameterFromId(cardObjList[i].ids);
			GameObject cardObj = cardObjList[i].CardObj;
			switch (cardParameterFromId.Rarity)
			{
			case 1:
				flag = true;
				gridBronze.AddChild(cardObj.transform);
				cardObj.transform.localScale = Vector3.one * 0.55f;
				break;
			case 2:
				flag2 = true;
				gridSilver.AddChild(cardObj.transform);
				cardObj.transform.localScale = Vector3.one * 0.55f;
				break;
			case 3:
				flag3 = true;
				gridGold.AddChild(cardObj.transform);
				cardObj.transform.localScale = Vector3.one * 0.55f;
				break;
			case 4:
				flag4 = true;
				gridLegendary.AddChild(cardObj.transform);
				cardObj.transform.localScale = Vector3.one * 0.55f;
				break;
			}
			if (_specialEffectCardList.Contains(cardObjList[i].ids))
			{
				_isWaitingEffectMaterial = true;
				if (_particleGroup == null)
				{
					_particleGroup = UIParticleEffectManager.Instance.CreateNewGroup(_particleTexture);
				}
				Effect2dCreateParam param = new Effect2dCreateParam
				{
					Parent = cardObj.gameObject,
					EffectName = "gch_result_skin_1",
					InitActive = false,
					UnloadAssetList = _loadEffectList,
					MaterialSetEndCallback = delegate
					{
						_isWaitingEffectMaterial = false;
					}
				};
				_particleGroup.StartEffect(param, isEnableUpdateReposition: true);
			}
			cardObj.transform.localPosition += Vector3.back;
			if (GameMgr.GetIns().GetDataMgr().IsNewCard(cardObjList[i].ids))
			{
				cardObj.GetComponent<CardListTemplate>()._newLabel.transform.localPosition = NEW_LABEL_POSITION;
			}
			cardObj.SetActive(value: true);
		}
		SystemText systemText = Data.SystemText;
		labelLegendary.gameObject.SetActive(flag4);
		if (flag4)
		{
			labelLegendary.text = systemText.Get("Card_Rarity_0004");
		}
		labelGold.gameObject.SetActive(flag3);
		if (flag3)
		{
			labelGold.text = systemText.Get("Card_Rarity_0003");
		}
		labelSilver.gameObject.SetActive(flag2);
		if (flag2)
		{
			labelSilver.text = systemText.Get("Card_Rarity_0002");
		}
		labelBronze.gameObject.SetActive(flag);
		if (flag)
		{
			labelBronze.text = systemText.Get("Card_Rarity_0001");
		}
		UIGrid uIGrid = null;
		if (flag4)
		{
			parentLegendary.transform.parent = m_RarityParentRow1.transform;
			parentLegendary.transform.localPosition = Vector3.zero;
			uIGrid = gridLegendary;
		}
		else if (flag3)
		{
			parentGold.transform.parent = m_RarityParentRow1.transform;
			parentGold.transform.localPosition = Vector3.zero;
			uIGrid = gridGold;
		}
		else if (flag2)
		{
			parentSilver.transform.parent = m_RarityParentRow1.transform;
			parentSilver.transform.localPosition = Vector3.zero;
			uIGrid = gridSilver;
		}
		else
		{
			parentBronze.transform.parent = m_RarityParentRow1.transform;
			parentBronze.transform.localPosition = Vector3.zero;
			uIGrid = gridBronze;
		}
		if ((flag && flag2) || (flag && flag3) || (flag && flag4))
		{
			parentBronze.transform.parent = m_RarityParentRow2.transform;
			parentBronze.transform.localPosition = Vector3.zero;
			uIGrid.transform.localPosition = GRID_LOCAL_POSITION;
			gridBronze.transform.localPosition = GRID_LOCAL_POSITION;
		}
		else if ((flag2 && flag3) || (flag2 && flag4))
		{
			parentSilver.transform.parent = m_RarityParentRow2.transform;
			parentSilver.transform.localPosition = Vector3.zero;
			uIGrid.transform.localPosition = GRID_LOCAL_POSITION;
			gridSilver.transform.localPosition = GRID_LOCAL_POSITION;
		}
		else if (flag3 && flag4)
		{
			parentGold.transform.parent = m_RarityParentRow2.transform;
			parentGold.transform.localPosition = Vector3.zero;
			uIGrid.transform.localPosition = GRID_LOCAL_POSITION;
			gridGold.transform.localPosition = GRID_LOCAL_POSITION;
		}
		else
		{
			uIGrid.transform.localPosition = GRID_ONLY_ONE_LOCAL_POSITION;
		}
	}
}
