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

public class LoadingInScene : MonoBehaviour
{
	private const string LOAD_TITLE = "Load_Title";

	private const string LOAD_DESC = "Load_Desc";

	private const string CARD_MASK_PATH = "loading_battle_mask";

	public const int LOADING_TIPS_INDEX_MIN = 1;

	private static int loadingTipsIndexMax;

	[SerializeField]
	private UILabel LoadingLabel;

	[SerializeField]
	private UIAnchor Anchor;

	[SerializeField]
	private LoadingBase Progress;

	[SerializeField]
	private bool Matching;

	[SerializeField]
	private bool Battle;

	[SerializeField]
	private bool ShowProgress;

	[SerializeField]
	private NguiObjs LoadingTextObj;

	[SerializeField]
	private UILabel LoadingCountLabel;

	[SerializeField]
	private bool notText;

	[SerializeField]
	private UITexture BGTexture;

	[SerializeField]
	private UIPanel _fadeBlackPanel;

	private int DotCnt;

	private Coroutine _coroutineTextAnimation;

	private Texture2D cardMask;

	private List<string> resourcePathList;

	public static int LoadingTipsIndexMax
	{
		get
		{
			if (loadingTipsIndexMax == 0)
			{
				loadingTipsIndexMax = Data.SystemText.Count("Load_Title");
			}
			return loadingTipsIndexMax;
		}
	}

	public bool IsFadeout { get; set; }

	public LoadingBase ProgressObj => Progress;

	public void SetShowProgress(bool enable, bool immediate = false)
	{
		ShowProgress = enable;
		float num = (enable ? 1f : 0f);
		if (immediate)
		{
			if ((bool)Anchor)
			{
				UISprite component = Anchor.GetComponent<UISprite>();
				if ((bool)component)
				{
					component.alpha = 1f - num;
				}
			}
			if ((bool)Progress)
			{
				UISprite component2 = Progress.GetComponent<UISprite>();
				if ((bool)component2)
				{
					component2.alpha = num;
				}
			}
		}
		else
		{
			if ((bool)Anchor)
			{
				TweenAlpha.Begin(Anchor.gameObject, 0.5f, 1f - num);
			}
			if ((bool)Progress)
			{
				TweenAlpha.Begin(Progress.gameObject, 0.5f, num);
			}
		}
	}

	private void Start()
	{
		IsFadeout = false;
		DotCnt = 0;
		float num = (ShowProgress ? 1f : 0f);
		if ((bool)Anchor)
		{
			TweenAlpha.Begin(Anchor.gameObject, 0f, 1f - num);
		}
		if ((bool)Progress)
		{
			TweenAlpha.Begin(Progress.gameObject, 0f, num);
		}
	}

	private IEnumerator SetLoadingText()
	{
		if (Battle)
		{
			List<int> allCardIds = CardMaster.GetInstanceForBattle().GetAllCardIds();
			int count = allCardIds.Count;
			CardParameter cardParameterFromId;
			int cardID;
			ResourcesManager.AssetLoadPathType pathType;
			while (true)
			{
				cardID = ((Data.Load.data._userTutorial.tutorial_step == 100) ? allCardIds[UnityEngine.Random.Range(0, count)] : (UnityEngine.Random.Range(0, 10) switch
				{
					1 => 101111070, 
					2 => 101011010, 
					3 => 100011030, 
					4 => 101121020, 
					5 => 101111020, 
					6 => 101121040, 
					7 => 101021020, 
					8 => 101014010, 
					9 => 101014030, 
					_ => 101114040, 
				}));
				cardParameterFromId = CardMaster.GetInstanceForBattle().GetCardParameterFromId(cardID);
				cardParameterFromId = CardMaster.GetInstanceForBattle().GetCardParameterFromId(cardParameterFromId.BaseCardId);
				cardID = cardParameterFromId.BaseCardId;
				if (!ReLotteryCheck(cardID))
				{
					pathType = ResourcesManager.AssetLoadPathType.UnitCardTextures;
					switch (cardParameterFromId.CharType)
					{
					case CardBasePrm.CharaType.CLASS:
					case CardBasePrm.CharaType.EVOLUTION:
						continue;
					case CardBasePrm.CharaType.NORMAL:
						pathType = ResourcesManager.AssetLoadPathType.UnitCardTextures;
						break;
					case CardBasePrm.CharaType.SPELL:
						pathType = ResourcesManager.AssetLoadPathType.SpellCardTextures;
						break;
					case CardBasePrm.CharaType.FIELD:
					case CardBasePrm.CharaType.CHANT_FIELD:
						pathType = ResourcesManager.AssetLoadPathType.SpellCardTextures;
						break;
					}
					break;
				}
			}
			ResourcesManager resMgr = Toolbox.ResourcesManager;
			LoadingTextObj.labels[0].text = cardParameterFromId.CardName;
			LoadingTextObj.labels[1].SetWrapText(cardParameterFromId.Description);
			resourcePathList = new List<string>
			{
				resMgr.GetAssetTypePath(cardID.ToString(), pathType),
				resMgr.GetAssetTypePath("class_base_common", ResourcesManager.AssetLoadPathType.ClassCharaMaterialMain),
				resMgr.GetAssetTypePath("loading_battle_mask", ResourcesManager.AssetLoadPathType.UiOtherTexture)
			};
			yield return StartCoroutine(resMgr.LoadAssetGroupAsync(resourcePathList, null));
			Texture value = resMgr.LoadObject<Texture>(resMgr.GetAssetTypePath(cardID + "0", pathType, isfetch: true));
			Texture value2 = resMgr.LoadObject<Texture>(resMgr.GetAssetTypePath("loading_battle_mask", ResourcesManager.AssetLoadPathType.UiOtherTexture, isfetch: true));
			LoadingTextObj.textures[0].mainTexture = null;
			LoadingTextObj.textures[0].material = resMgr.LoadObject<Material>(resMgr.GetAssetTypePath("class_base_common", ResourcesManager.AssetLoadPathType.ClassCharaMaterialMain, isfetch: true));
			LoadingTextObj.textures[0].material.SetTexture("_MainTex", value);
			LoadingTextObj.textures[0].material.SetTexture("_MaskTex", value2);
			LoadingTextObj.textures[0].gameObject.SetActive(value: false);
			LoadingTextObj.textures[0].gameObject.SetActive(value: true);
		}
		else
		{
			int num = UnityEngine.Random.Range(1, LoadingTipsIndexMax + 1);
			LoadingTextObj.labels[0].SetWrapText(Data.SystemText.Get(string.Format("{0}{1}", "Load_Title", num.ToString("0000"))));
			LoadingTextObj.labels[1].SetWrapText(Data.SystemText.Get(string.Format("{0}{1}", "Load_Desc", num.ToString("0000"))));
		}
	}

	private bool ReLotteryCheck(int id)
	{
		if (CardMaster.GetInstanceForBattle().GetCardParameterFromId(id).ResourceCardId != id || Data.Load.data.LoadingExclusionCardList.Contains(id) || CardMaster.IsMutationCardCheck(id) || (id > 800000000 && id % 800000000 < 100000000) || CardMaster.IsChoiceBraveCardCheck(id))
		{
			return true;
		}
		return false;
	}

	private void Update()
	{
		if ((bool)Anchor && !Anchor.gameObject.activeSelf)
		{
			Anchor.enabled = true;
			Anchor.gameObject.SetActive(value: true);
		}
	}

	private void StartLoadingTextAnimation(string overrideLoadingText = null)
	{
		string loadingText = ((overrideLoadingText != null) ? overrideLoadingText : ((!Matching) ? Data.SystemText.Get("System_0001") : Data.SystemText.Get("Battle_0008")));
		StopLoadingTextAnimation();
		_coroutineTextAnimation = StartCoroutine(LoadingTextAnimation(loadingText));
	}

	private void StopLoadingTextAnimation()
	{
		if (_coroutineTextAnimation != null)
		{
			StopCoroutine(_coroutineTextAnimation);
			_coroutineTextAnimation = null;
		}
	}

	private IEnumerator LoadingTextAnimation(string loadingText)
	{
		while (true)
		{
			if ((bool)LoadingLabel)
			{
				LoadingLabel.text = loadingText;
				DotCnt++;
				if (DotCnt > 3)
				{
					DotCnt = 0;
				}
				if (LoadingCountLabel != null)
				{
					LoadingCountLabel.text = "";
				}
				for (int i = 0; i < DotCnt; i++)
				{
					if (LoadingCountLabel != null)
					{
						LoadingCountLabel.text += ".";
					}
					else
					{
						LoadingLabel.text += ".";
					}
				}
			}
			if (notText)
			{
				if (LoadingLabel != null)
				{
					LoadingLabel.text = "";
				}
				if (LoadingCountLabel != null)
				{
					LoadingCountLabel.text = "";
				}
			}
			yield return new WaitForSeconds(0.05f);
		}
	}

	public void FadeIn(bool notBlack = false, bool notCollider = false, float alphe = 1f, float panelFadeInDuration = 0.5f, string overrideText = null)
	{
		StartCoroutine(FadeInAndLoad(notBlack, notCollider, alphe, panelFadeInDuration));
		StartLoadingTextAnimation(overrideText);
	}

	private IEnumerator FadeInAndLoad(bool notBlack = false, bool notCollider = false, float alphe = 1f, float panelFadeInDuration = 0.5f)
	{
		if (notCollider)
		{
			GetComponent<BoxCollider>().enabled = false;
		}
		else
		{
			GetComponent<BoxCollider>().enabled = true;
		}
		if ((bool)Anchor)
		{
			Anchor.gameObject.SetActive(value: false);
		}
		StartFadeInUI(panelFadeInDuration);
		CancelInvoke("HideObj");
		IsFadeout = false;
		GameObject bgObj = base.transform.Find("Bg").gameObject;
		if (notBlack)
		{
			bgObj.SetActive(value: false);
			yield break;
		}
		if (LoadingTextObj != null)
		{
			yield return StartCoroutine(SetLoadingText());
		}
		bgObj.SetActive(value: true);
		bgObj.GetComponent<UISprite>().color = new Color(0f, 0f, 0f, alphe);
		if (null != BGTexture)
		{
			BGTexture.alpha = 0f;
			TweenAlpha.Begin(BGTexture.gameObject, 0.5f, 1f);
		}
	}

	public void FadeOut(bool disableCollider = false)
	{
		int num = 0;
		try
		{
			num = 1;
			if (null != BGTexture)
			{
				TweenAlpha.Begin(BGTexture.gameObject, 0.25f, 0f);
			}
			num = 2;
			StartFadeOutUI();
			num = 3;
			Invoke("HideObj", 0.5f);
			num = 4;
			IsFadeout = true;
			if (disableCollider)
			{
				GetComponent<BoxCollider>().enabled = false;
			}
			num = 5;
		}
		catch (Exception ex)
		{
			string text = "";
			if (base.gameObject == null)
			{
				text += "obj null ";
			}
			if (BGTexture == null)
			{
				text += "BGTexture null ";
			}
			else if (BGTexture.gameObject == null)
			{
				text += "BGTextureObj null ";
			}
			LocalLog.AccumulateTraceLog("#580304 FadeOut " + num + " disableCollider " + disableCollider + text);
			throw ex;
		}
	}

	private void StartFadeInUI(float panelFadeInDuration = 0.5f)
	{
		if (_fadeBlackPanel != null)
		{
			_fadeBlackPanel.alpha = 1f;
			TweenAlpha.Begin(_fadeBlackPanel.gameObject, panelFadeInDuration, 0f);
			return;
		}
		UIPanel component = GetComponent<UIPanel>();
		if ((bool)component)
		{
			component.alpha = 0.01f;
		}
		TweenAlpha.Begin(base.gameObject, panelFadeInDuration, 1f);
	}

	private void StartFadeOutUI(float panelFadeOutDuration = 0.5f)
	{
		if (_fadeBlackPanel != null)
		{
			_fadeBlackPanel.alpha = 0f;
			TweenAlpha.Begin(_fadeBlackPanel.gameObject, panelFadeOutDuration, 1f);
		}
		else
		{
			TweenAlpha.Begin(base.gameObject, panelFadeOutDuration, 0f);
		}
	}

	private void HideObj()
	{
		int num = 0;
		try
		{
			num = 1;
			StopLoadingTextAnimation();
			num = 2;
			if (cardMask != null)
			{
				UnityEngine.Object.Destroy(cardMask);
			}
			num = 3;
			if (resourcePathList != null)
			{
				Toolbox.ResourcesManager.RemoveAssetGroup(resourcePathList);
				num = 4;
				resourcePathList.Clear();
				resourcePathList = null;
			}
			num = 5;
			base.gameObject.SetActive(value: false);
		}
		catch (Exception ex)
		{
			string text = "";
			if (base.gameObject == null)
			{
				text += "obj null ";
			}
			LocalLog.AccumulateTraceLog("#580304 HideObj " + num + text);
			throw ex;
		}
	}
}
