using System;
using System.Collections;
using System.Collections.Generic;
using Cute;
using UnityEngine;
using Wizard.Scripts.Network.Data.TaskData.SpotCardExchange;
using Wizard.UI.Common;

namespace Wizard;

public class SpotCardExchange : MonoBehaviour
{
	private const string SPRITE_CLASS_TAB_NEUTRAL = "class_tab_neutral";

	private const int CONFIRM_DIALOG_DEPTH = 100;

	private const int CARD_OBJECT_DEPTH = 20;

	private const float CARD_OBJECT_SCALE = 0.36f;

	private const float CARD_OBJECT_COLLIDER_SCALE = 0.9f;

	[SerializeField]
	private UILabel _labelSpotCardPoint;

	[SerializeField]
	private SimpleScrollViewUI _spotCardScrollView;

	[SerializeField]
	private UILabel _labelNoSpotCardList;

	[SerializeField]
	private TabList _tabListClass;

	[SerializeField]
	private UISprite _spriteClassTab;

	[SerializeField]
	private PurchaseConfirm _purchaseConfirmDialog;

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

	private UIAtlas _atlasProfile;

	private List<UIBase_CardManager.CardObjData> _cardObjectList;

	[SerializeField]
	private GameObject _cardObjectRoot;

	[SerializeField]
	private GameObject _cardDetailRoot;

	private CardDetailUI _cardDetail;

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

	private int _cardDetailIndex;

	private CardBasePrm.ClanType _displayClassType;

	private Dictionary<CardBasePrm.ClanType, List<SpotCardExchangeInfo>> _spotCardListInClassDict = new Dictionary<CardBasePrm.ClanType, List<SpotCardExchangeInfo>>();

	private SpotCardExchangeInfoTask.PrereleaseExchangeInfo _prereleaseExchangeInfo;

	public void Init()
	{
		_labelSpotCardPoint.gameObject.SetActive(value: false);
		InitCardDetail();
		StartCoroutine(LoadInitialResources(delegate
		{
			InitClassTab();
			UpdateExchangeableSpotCardList(CardBasePrm.ClanType.ALL);
		}));
	}

	private IEnumerator LoadInitialResources(Action onFinish)
	{
		UIManager.GetInstance().createInSceneCenterLoading();
		yield return StartCoroutine(LoadProfileAtlas());
		UIManager.GetInstance().closeInSceneCenterLoading();
		onFinish.Call();
	}

	private IEnumerator LoadProfileAtlas()
	{
		string profileAtlasName = UIManager.GetInstance().GetSceneAssetPath(UIAtlasManager.AssetBundleNames.Profile, null);
		yield return StartCoroutine(Toolbox.ResourcesManager.LoadAssetAsync(profileAtlasName, null));
		_loadedResourceList.Add(profileAtlasName);
		profileAtlasName = UIManager.GetInstance().GetSceneAssetPath(UIAtlasManager.AssetBundleNames.Profile, null, isload: true);
		_atlasProfile = Toolbox.ResourcesManager.LoadObject<GameObject>(profileAtlasName).GetComponent<UIAtlas>();
	}

	private void UnloadResources()
	{
		Toolbox.ResourcesManager.RemoveAssetGroup(_loadedResourceList);
		_loadedResourceList.Clear();
	}

	private void OnDestroy()
	{
		UnloadResources();
		UnloadCardObject();
	}

	private void InitCardDetail()
	{
		_cardDetail = DialogCreator.CreateCardDetailDialog(_cardDetailRoot, "Detail");
		_cardDetail.OnDragCard = CardDetailDragCallback;
		_cardDetail.OnDetailCardUpdate = UpdateCardDetailArrowButtonVisible;
		_cardDetail.gameObject.SetActive(value: false);
	}

	private IEnumerator LoadCardObject(List<int> cardIdList, Action onFinish)
	{
		if (cardIdList.Count == 0)
		{
			onFinish.Call();
			yield break;
		}
		UnloadCardObject();
		UIManager uiMgr = UIManager.GetInstance();
		uiMgr.createInSceneCenterLoading();
		bool isLoaded = false;
		uiMgr.CardLoadSelect(null, cardIdList, base.gameObject.layer, is2D: true, delegate
		{
			isLoaded = true;
		});
		while (!isLoaded)
		{
			yield return null;
		}
		InitCardObject();
		uiMgr.closeInSceneCenterLoading();
		onFinish.Call();
	}

	private void InitCardObject()
	{
		List<UIBase_CardManager.CardObjData> cardList2DObjs = UIManager.GetInstance().getCardList2DObjs();
		_cardObjectList = new List<UIBase_CardManager.CardObjData>(cardList2DObjs);
		cardList2DObjs.Clear();
		List<string> cardListAssetPathList = Toolbox.ResourcesManager.CardListAssetPathList;
		_loadedCardResourceList.AddRange(new List<string>(cardListAssetPathList));
		cardListAssetPathList.Clear();
		if (_cardObjectList == null)
		{
			return;
		}
		for (int i = 0; i < _cardObjectList.Count; i++)
		{
			GameObject cardObject = _cardObjectList[i].CardObj;
			cardObject.SetActive(value: false);
			UITexture[] componentsInChildren = cardObject.GetComponentsInChildren<UITexture>();
			foreach (UITexture uITexture in componentsInChildren)
			{
				if (uITexture.name.Contains("CardTexture"))
				{
					UITexture component = uITexture.GetComponent<UITexture>();
					Material material = component.material;
					component.mainTexture = material.mainTexture;
					component.material = null;
				}
			}
			cardObject.transform.parent = _cardObjectRoot.transform;
			CardListTemplate component2 = cardObject.GetComponent<CardListTemplate>();
			component2.HideNum();
			component2._newLabel.gameObject.SetActive(value: false);
			component2.SetId(_cardObjectList[i].ids);
			component2.SetScale(0.36f);
			component2.AddDepth(20);
			int tempIndex = i;
			component2.AddColliderToFrame(0.9f).onClick = delegate
			{
				_cardDetailIndex = tempIndex;
				_cardDetail.OnPushCardDetailOn(cardObject);
			};
		}
	}

	private void CardDetailDragCallback(Vector2 vec)
	{
		if (!_cardDetail.IsEnableShowDetail)
		{
			return;
		}
		float x = vec.x;
		if (!(Mathf.Abs(x) < 70f))
		{
			int num = _cardDetailIndex + ((!(x > 0f)) ? 1 : (-1));
			if (num >= 0 && _cardObjectList.Count > num)
			{
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_SLIDE_BTN);
				_cardDetail.CloseDefault(playSe: false);
				_cardDetail.ShowCardDetail(_cardObjectList[num].CardObj);
				_cardDetailIndex = num;
				UpdateCardDetailArrowButtonVisible();
			}
		}
	}

	private void UpdateCardDetailArrowButtonVisible()
	{
		_cardDetail.LeftButtonVisible = _cardDetailIndex > 0;
		_cardDetail.RightButtonVisible = _cardDetailIndex < _cardObjectList.Count - 1;
	}

	private void UnloadCardObject()
	{
		Toolbox.ResourcesManager.RemoveAssetGroup(_loadedCardResourceList);
		_loadedCardResourceList.Clear();
		if (_cardObjectList != null)
		{
			for (int i = 0; i < _cardObjectList.Count; i++)
			{
				UnityEngine.Object.Destroy(_cardObjectList[i].CardObj.gameObject);
			}
			_cardObjectList.Clear();
		}
	}

	private void InitClassTab()
	{
		_spriteClassTab.atlas = _atlasProfile;
		for (int i = 0; i < 9; i++)
		{
			CardBasePrm.ClanType classType = (CardBasePrm.ClanType)i;
			string spriteBaseName = ((classType != CardBasePrm.ClanType.ALL) ? ("class_tab_" + i.ToString("00")) : "class_tab_neutral");
			_tabListClass.AddTab(delegate
			{
				if (classType != _displayClassType)
				{
					ShowExchangeableSpotCardList(classType);
				}
			}, spriteBaseName).name = "Class_" + i + "(Clone)";
		}
		_tabListClass.Reset();
	}

	private void UpdateDisplaySpotCardPoint()
	{
		_labelSpotCardPoint.text = GetHaveSpotCardPoint().ToString();
	}

	private int GetHaveSpotCardPoint()
	{
		return PlayerStaticData.UserSpotCardPointCount;
	}

	private void UpdateExchangeableSpotCardList(CardBasePrm.ClanType displayClassType)
	{
		SpotCardExchangeInfoTask task = new SpotCardExchangeInfoTask();
		StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
		{
			_spotCardListInClassDict = task.ExchangeableSpotCardListInClassDict;
			_prereleaseExchangeInfo = task.PrereleaseInfo;
			UpdateDisplaySpotCardPoint();
			_labelSpotCardPoint.gameObject.SetActive(value: true);
			ShowExchangeableSpotCardList(displayClassType);
		}));
	}

	private void ShowExchangeableSpotCardList(CardBasePrm.ClanType classType)
	{
		_displayClassType = classType;
		List<SpotCardExchangeInfo> cardList = _spotCardListInClassDict[_displayClassType];
		_spotCardScrollView.SetVisiable(isVisiable: false);
		if (cardList.Count > 0)
		{
			List<int> list = new List<int>(cardList.Count);
			CardMaster.GetInstance(CardMaster.CardMasterId.Default);
			for (int i = 0; i < cardList.Count; i++)
			{
				list.Add(cardList[i].CardId);
			}
			StartCoroutine(LoadCardObject(list, delegate
			{
				_spotCardScrollView.SetVisiable(isVisiable: true);
				_spotCardScrollView.CreateScrollView(cardList.Count, InitializePlate);
				_labelNoSpotCardList.gameObject.SetActive(value: false);
			}));
		}
		else
		{
			_labelNoSpotCardList.gameObject.SetActive(value: true);
		}
	}

	private void InitializePlate(int index, GameObject plate)
	{
		List<SpotCardExchangeInfo> list = _spotCardListInClassDict[_displayClassType];
		if (index >= list.Count)
		{
			plate.SetActive(value: false);
			return;
		}
		SpotCardExchangePlate component = plate.GetComponent<SpotCardExchangePlate>();
		component.SetData(list[index], GetHaveSpotCardPoint(), _prereleaseExchangeInfo, OnClickConfirmExchangeButton);
		component.SetCardObject(_cardObjectList[index].CardObj, _cardObjectRoot);
	}

	private void OnClickConfirmExchangeButton(SpotCardExchangeInfo spotCardExchangeInfo)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		ShowConfirmExchangeDialog(spotCardExchangeInfo);
	}

	private void ShowConfirmExchangeDialog(SpotCardExchangeInfo spotCardExchangeInfo)
	{
		SystemText systemText = Data.SystemText;
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetSize(DialogBase.Size.S);
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_CancelBtn);
		dialogBase.SetTitleLabel(systemText.Get("Shop_0153"));
		dialogBase.SetButtonText(systemText.Get("Shop_0154"));
		dialogBase.SetPanelDepth(100);
		dialogBase.onPushButton1 = (Action)Delegate.Combine(dialogBase.onPushButton1, (Action)delegate
		{
			OnClickExchangeButton(spotCardExchangeInfo);
		});
		PurchaseConfirm purchaseConfirm = UnityEngine.Object.Instantiate(_purchaseConfirmDialog);
		dialogBase.SetObj(purchaseConfirm.gameObject);
		purchaseConfirm.SetSpotCardPointConfirmDialog(spotCardExchangeInfo.ExchangePoint, systemText.Get("Shop_0152", spotCardExchangeInfo.GetExchangeCardText()), GetHaveSpotCardPoint());
		if (_prereleaseExchangeInfo.IsPrerelease && spotCardExchangeInfo.IsPrereleaseCard)
		{
			purchaseConfirm.SetWarningText(systemText.Get("Shop_0184", _prereleaseExchangeInfo.RemainingExchangeableCount.ToString()));
		}
	}

	private void OnClickExchangeButton(SpotCardExchangeInfo spotCardExchangeInfo)
	{
		SpotCardExchangeTask spotCardExchangeTask = new SpotCardExchangeTask();
		spotCardExchangeTask.SetParameter(spotCardExchangeInfo.CardId, spotCardExchangeInfo.ExchangePoint);
		StartCoroutine(Toolbox.NetworkManager.Connect(spotCardExchangeTask, delegate
		{
			ShowSuccessExchangeDialog(spotCardExchangeInfo.GetExchangeCardText());
			UpdateDisplaySpotCardPoint();
			UpdateExchangeableSpotCardList(_displayClassType);
		}));
	}

	private void ShowSuccessExchangeDialog(string exchangedCardName)
	{
		SystemText systemText = Data.SystemText;
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetTitleLabel(systemText.Get("Common_0021"));
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
		dialogBase.SetText(systemText.Get("Shop_0155", exchangedCardName), isWrapText: true);
		dialogBase.SetPanelDepth(100);
	}
}
