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

public class LoadingDownLoadCardView : MonoBehaviour
{
	private class LoadSceneCardDataAccessor
	{
		public readonly int CardNum;

		private List<LoadSceneCardData> _loadSceneCardDataList;

		public int CurrentIndex { get; private set; }

		public LoadSceneCardData CurrentCardData => _loadSceneCardDataList[CurrentIndex];

		public LoadSceneCardDataAccessor(List<int> cardIdList)
		{
			CardNum = cardIdList.Count;
			_loadSceneCardDataList = new List<LoadSceneCardData>();
			for (int i = 0; i < CardNum; i++)
			{
				_loadSceneCardDataList.Add(new LoadSceneCardData(i, cardIdList[i]));
			}
		}

		public void UpdateIndexToNext()
		{
			if (CurrentIndex < CardNum - 1)
			{
				CurrentIndex++;
			}
			else
			{
				CurrentIndex = 0;
			}
		}

		public void UpdateIndexToPrev()
		{
			if (CurrentIndex > 0)
			{
				CurrentIndex--;
			}
			else
			{
				CurrentIndex = CardNum - 1;
			}
		}
	}

	private class LoadSceneCardData
	{
		private static readonly Color DEFAULT_EFFECT_COLOR = Color.white;

		public const string FORMAT_DESC_NORMAL = "CardDescN_{0}";

		public const string FORMAT_DESC_EVO = "CardDescE_{0}";

		private const string FORMAT_EFFECT_COLOR = "CardColor_{0}";

		private Color _cardEffectColor;

		public CardParameter CardParam { get; private set; }

		public string CardName { get; private set; }

		public string CardNormalDesc { get; private set; }

		public string CardEvolDesc { get; private set; }

		public Color CardEffectColor => _cardEffectColor;

		public LoadSceneCardData(int index, int cardId)
		{
			SystemText systemText = Data.SystemText;
			_ = Data.Load.data.ResourceDlViewID;
			CardParam = CardMaster.GetInstance(CardMaster.CardMasterId.Default).GetCardParameterFromId(cardId);
			bool num = CardParam.CharType == CardBasePrm.CharaType.NORMAL;
			CardName = systemText.Get($"LoadCard_{cardId}");
			CardNormalDesc = systemText.Get($"CardDescN_{cardId}");
			if (num)
			{
				CardEvolDesc = systemText.Get($"CardDescE_{cardId}");
			}
			else
			{
				CardEvolDesc = "";
			}
			if (!ColorUtility.TryParseHtmlString(systemText.Get($"CardColor_{(int)CardParam.Clan}"), out _cardEffectColor))
			{
				_cardEffectColor = DEFAULT_EFFECT_COLOR;
			}
		}
	}

	private static readonly Vector3 CARD_SCALE = new Vector3(0.022f, 0.017f, 1f);

	[SerializeField]
	private Transform CardObjectTransform;

	[SerializeField]
	private ParticleSystem[] LoadingEffects;

	[SerializeField]
	private UIScrollView _cardDetailScrollView;

	[SerializeField]
	private UILabel _cardNameLabel;

	[SerializeField]
	private GameObject _followerDetailObjctRoot;

	[SerializeField]
	private GameObject _spellOrAmuletDetailObjctRoot;

	[SerializeField]
	private UILabel _followerNormalSkillLabel;

	[SerializeField]
	private UILabel _followerNormalAtkLabel;

	[SerializeField]
	private UILabel _followerNormalLifeLabel;

	[SerializeField]
	private UILabel _followerEvoSkillLabel;

	[SerializeField]
	private UILabel _followerEvoAtkLabel;

	[SerializeField]
	private UILabel _followerEvoLifeLabel;

	[SerializeField]
	private GameObject _followerEvoTopRootObj;

	[SerializeField]
	private UILabel _spellOrAmuletSkillLabel;

	[SerializeField]
	private UILabel _cardTypeLabel;

	[SerializeField]
	private ResourceDownloadCardFactory _cardFactory;

	[SerializeField]
	private UIPanel _cardPanel;

	[SerializeField]
	private TextLineCreater _normalTextLineCreater;

	[SerializeField]
	private TextLineCreater _evoTextLineCreater;

	[SerializeField]
	private TextLineCreater _spellOrAmuletSkillTextLineCreater;

	[SerializeField]
	private UIToggle indicatorBase;

	[SerializeField]
	private GameObject _blackPanel;

	private Vector3 _defaultPosCardSprite = Vector3.zero;

	private Quaternion _defaultRotCardSprite = Quaternion.identity;

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

	private LoadSceneCardDataAccessor _cardDataAccessor;

	private List<int> _cardIdList;

	private List<CardListTemplate> _cardList;

	public const string FORMAT_CARD_NAME = "LoadCard_{0}";

	public bool IsAnimatingNow { get; private set; }

	public void Initialize(Action onReady)
	{
		LoadingEffects[2].gameObject.SetActive(value: false);
		LoadingEffects[3].gameObject.SetActive(value: false);
		LoadingEffects[4].gameObject.SetActive(value: false);
		LoadingEffects[0].gameObject.SetActive(value: false);
		LoadCardResource(delegate
		{
			_blackPanel.SetActive(value: false);
			LoadingEffects[0].gameObject.SetActive(value: true);
			TweenAlpha.Begin(CardObjectTransform.gameObject, 0f, 1f);
			_cardDataAccessor = new LoadSceneCardDataAccessor(GetCardIdList());
			ChangeLoadCardInfo(_cardDataAccessor.CurrentCardData, _cardDataAccessor.CurrentIndex);
			GameObject gameObject = indicatorBase.transform.parent.gameObject;
			_indicatorList.Add(indicatorBase);
			for (int i = 1; i < _cardDataAccessor.CardNum; i++)
			{
				_indicatorList.Add(NGUITools.AddChild(gameObject, indicatorBase.gameObject).GetComponent<UIToggle>());
			}
			gameObject.GetComponent<UIGrid>().Reposition();
			UpdateIndicator(_cardDataAccessor.CurrentIndex);
			onReady.Call();
		});
	}

	private List<int> CreateCardIdList()
	{
		Dictionary<int, List<int>> dictionary = LoadCardIdList();
		if (dictionary.TryGetValue(Data.Load.data.RotationLatestCardPackId, out var value))
		{
			return value;
		}
		Debug.LogError($"カードパックID[{Data.Load.data.RotationLatestCardPackId}]が見つかりません");
		return dictionary.FirstOrDefault().Value;
	}

	private Dictionary<int, List<int>> LoadCardIdList()
	{
		List<string[]> list = Utility.ConvertCSV_Array((Resources.Load("CSV/load_card") as TextAsset).ToString());
		Dictionary<int, List<int>> dictionary = new Dictionary<int, List<int>>();
		foreach (string[] item in list)
		{
			if (!int.TryParse(item[0], out var result))
			{
				continue;
			}
			List<int> list2 = new List<int>(list.Count);
			for (int i = 1; i < item.Length; i++)
			{
				if (int.TryParse(item[i], out var result2))
				{
					list2.Add(result2);
				}
			}
			dictionary[result] = list2;
		}
		return dictionary;
	}

	private List<int> GetCardIdList()
	{
		if (_cardIdList == null)
		{
			_cardIdList = CreateCardIdList();
		}
		return _cardIdList;
	}

	public bool CanView()
	{
		return _cardFactory.CanView(GetCardIdList());
	}

	private void LoadCardResource(Action onFinish)
	{
		_cardFactory.Load(GetCardIdList(), _cardPanel.gameObject, delegate(List<CardListTemplate> cardList)
		{
			OnFinishCardLoad(cardList);
			onFinish();
		});
	}

	private void OnFinishCardLoad(List<CardListTemplate> cardList)
	{
		for (int i = 0; i < cardList.Count; i++)
		{
			CardListTemplate cardListTemplate = cardList[i];
			cardListTemplate.gameObject.SetActive(i == 0);
			cardListTemplate.HideNum();
			cardListTemplate.transform.localScale = CARD_SCALE;
		}
		_cardList = cardList;
	}

	public void FadeOutLoading()
	{
		TweenAlpha.Begin(CardObjectTransform.gameObject, 1f, 0f);
		for (int i = 0; i < LoadingEffects.Length; i++)
		{
			LoadingEffects[i].Stop();
			LoadingEffects[i].Clear();
		}
	}

	public void Next()
	{
		StartCoroutine(ChangeCardAnm(isRight: true));
	}

	public void Prev()
	{
		StartCoroutine(ChangeCardAnm(isRight: false));
	}

	private void ChangeLoadCardInfo(LoadSceneCardData cardData, int index)
	{
		foreach (CardListTemplate card in _cardList)
		{
			card.gameObject.SetActive(value: false);
		}
		_cardList[index].gameObject.SetActive(value: true);
		SetCardDetailLabels(cardData, index);
		ParticleSystem[] componentsInChildren = LoadingEffects[0].gameObject.GetComponentsInChildren<ParticleSystem>();
		foreach (ParticleSystem obj in componentsInChildren)
		{
			ParticleSystem.MainModule main = obj.main;
			main.startColor = cardData.CardEffectColor;
			obj.Stop();
			obj.Play();
		}
		componentsInChildren = LoadingEffects[1].gameObject.GetComponentsInChildren<ParticleSystem>();
		foreach (ParticleSystem obj2 in componentsInChildren)
		{
			ParticleSystem.MainModule main2 = obj2.main;
			main2.startColor = cardData.CardEffectColor;
			obj2.Stop();
			obj2.Play();
		}
		UpdateIndicator(_cardDataAccessor.CurrentIndex);
	}

	private void SetCardDetailLabels(LoadSceneCardData cardData, int index)
	{
		_cardNameLabel.text = cardData.CardName;
		_cardTypeLabel.text = GetCardTypeText(cardData);
		SetCardSkillText(cardData, index);
		_cardDetailScrollView.ResetPosition();
		_cardDetailScrollView.UpdateScrollbars(recalculateBounds: true);
	}

	private string GetCardTypeText(LoadSceneCardData cardData)
	{
		return cardData.CardParam.CharType switch
		{
			CardBasePrm.CharaType.FIELD => Data.SystemText.Get("Card_0046"), 
			CardBasePrm.CharaType.SPELL => Data.SystemText.Get("Card_0045"), 
			_ => string.Empty, 
		};
	}

	private void SetCardSkillText(LoadSceneCardData cardData, int index)
	{
		int cardId = GetCardIdList()[index];
		CardParameter cardParameterFromId = CardMaster.GetInstance(CardMaster.CardMasterId.Default).GetCardParameterFromId(cardId);
		if (cardParameterFromId.CharType == CardBasePrm.CharaType.NORMAL)
		{
			_followerDetailObjctRoot.SetActive(value: true);
			_spellOrAmuletDetailObjctRoot.SetActive(value: false);
			_followerNormalAtkLabel.text = cardParameterFromId.Atk.ToString();
			_followerNormalLifeLabel.text = cardParameterFromId.Life.ToString();
			_followerEvoAtkLabel.text = cardParameterFromId.EvoAtk.ToString();
			_followerEvoLifeLabel.text = cardParameterFromId.EvoLife.ToString();
			SetFollowerText(_cardDataAccessor.CurrentCardData, index);
		}
		else
		{
			_followerDetailObjctRoot.SetActive(value: false);
			_spellOrAmuletDetailObjctRoot.SetActive(value: true);
			SetViewCardText(cardData.CardNormalDesc, _spellOrAmuletSkillLabel, _spellOrAmuletSkillTextLineCreater);
		}
	}

	private void SetFollowerText(LoadSceneCardData cardData, int index)
	{
		if (_cardList != null)
		{
			SetViewCardText(cardData.CardNormalDesc, _followerNormalSkillLabel, _normalTextLineCreater);
			SetViewCardText(cardData.CardEvolDesc, _followerEvoSkillLabel, _evoTextLineCreater);
			float y = _followerNormalSkillLabel.transform.localPosition.y - (float)_followerNormalSkillLabel.height - 44f;
			_followerEvoTopRootObj.transform.localPosition = new Vector3(_followerEvoTopRootObj.transform.localPosition.x, y);
		}
	}

	private void SetViewCardText(string cardText, UILabel textLabel, TextLineCreater lineCreater)
	{
		textLabel.overflowMethod = UILabel.Overflow.ResizeHeight;
		textLabel.ProcessText();
		int textLineCount = Global.GetTextLineCount(Global.GetConvertWrapText(textLabel, cardText));
		lineCreater.ShowLines(textLineCount);
		textLabel.SetWrapText(cardText);
	}

	private IEnumerator ChangeCardAnm(bool isRight)
	{
		IsAnimatingNow = true;
		CardListTemplate currentCard = _cardList[_cardDataAccessor.CurrentIndex];
		if (!isRight)
		{
			_cardDataAccessor.UpdateIndexToPrev();
		}
		else
		{
			_cardDataAccessor.UpdateIndexToNext();
		}
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_SLIDE_BTN);
		TweenPosition tp = CardObjectTransform.GetComponent<TweenPosition>();
		TweenRotation tr = CardObjectTransform.GetComponent<TweenRotation>();
		_defaultPosCardSprite = CardObjectTransform.transform.localPosition;
		_defaultRotCardSprite = CardObjectTransform.transform.localRotation;
		tp.enabled = false;
		tr.enabled = false;
		LoadingEffects[1].gameObject.SetActive(value: false);
		LoadingEffects[2].gameObject.SetActive(value: false);
		LoadingEffects[2].gameObject.SetActive(value: true);
		TweenAlpha.Begin(CardObjectTransform.gameObject, 0.2f, 0f);
		TweenAlpha.Begin(currentCard._nameLabel.gameObject, 0.2f, 0f);
		if (isRight)
		{
			iTween.MoveTo(CardObjectTransform.gameObject, iTween.Hash("position", _defaultPosCardSprite + Vector3.left * 50f, "time", 0.2f, "islocal", true, "easetype", iTween.EaseType.easeOutQuad));
		}
		else
		{
			iTween.MoveTo(CardObjectTransform.gameObject, iTween.Hash("position", _defaultPosCardSprite + Vector3.right * 50f, "time", 0.2f, "islocal", true, "easetype", iTween.EaseType.easeOutQuad));
		}
		yield return new WaitForSeconds(0.2f);
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_DL_CARD_APPEAR);
		LoadingEffects[1].gameObject.SetActive(value: true);
		ChangeLoadCardInfo(_cardDataAccessor.CurrentCardData, _cardDataAccessor.CurrentIndex);
		LoadingEffects[3].gameObject.SetActive(value: false);
		LoadingEffects[3].gameObject.SetActive(value: true);
		LoadingEffects[4].gameObject.SetActive(value: false);
		LoadingEffects[4].gameObject.SetActive(value: true);
		TweenAlpha.Begin(CardObjectTransform.gameObject, 0.2f, 1f);
		TweenAlpha.Begin(currentCard._nameLabel.gameObject, 0.2f, 1f);
		CardObjectTransform.transform.localPosition = _defaultPosCardSprite + Vector3.down * 20f;
		CardObjectTransform.transform.localRotation = Quaternion.Euler(new Vector3(0f, 90f, 10f));
		iTween.MoveTo(CardObjectTransform.gameObject, iTween.Hash("position", _defaultPosCardSprite, "time", 0.2f, "islocal", true, "easetype", iTween.EaseType.easeOutQuad));
		iTween.RotateTo(CardObjectTransform.gameObject, iTween.Hash("rotation", _defaultRotCardSprite.eulerAngles, "time", 0.2f, "easetype", iTween.EaseType.easeOutQuad));
		yield return new WaitForSeconds(0.2f);
		CardObjectTransform.transform.localPosition = _defaultPosCardSprite;
		tp.enabled = true;
		tr.enabled = true;
		IsAnimatingNow = false;
	}

	private void UpdateIndicator(int index)
	{
		if (_indicatorList.Count > 1)
		{
			_indicatorList[index].value = true;
		}
	}
}
