using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Cute;
using UnityEngine;
using Wizard;
using Wizard.UI.Common;
using Wizard.UI.Profile;

public class DeckIntroduction : MonoBehaviour
{
	private const int MAX_WIDTH_ANNOTATION_LABEL = 210;

	private const Format DEFAULT_FORMAT = Format.Rotation;

	private static readonly Vector2 FORMAT_CHANGE_UI_POSITION = new Vector2(-466f, 246f);

	private static readonly Dictionary<Format, FormatChangeUI.FormatCategory> FORMAT_TO_FORMAT_CATEGORY = new Dictionary<Format, FormatChangeUI.FormatCategory>
	{
		{
			Format.Rotation,
			FormatChangeUI.FormatCategory.Rotation
		},
		{
			Format.Unlimited,
			FormatChangeUI.FormatCategory.Unlimited
		},
		{
			Format.Crossover,
			FormatChangeUI.FormatCategory.Crossover
		}
	};

	[SerializeField]
	private TabList _tabList;

	[SerializeField]
	private UISprite _classIcon;

	[SerializeField]
	private UITexture _classCharaTexture;

	[SerializeField]
	private UILabel _className;

	[SerializeField]
	private UITexture _classBG;

	private Format _formatState;

	[SerializeField]
	private UILabel _labelAnnotation;

	private UIAtlas _classIconAtlas;

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

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

	private List<string> _loadCardAssetList;

	private List<DeckIntroductionItem> _deckIntroductionItem = new List<DeckIntroductionItem>();

	[SerializeField]
	private GameObject _dialogAttachRoot;

	[SerializeField]
	private GameObject _deckViewPrefab;

	[SerializeField]
	private GameObject _cardDetailPrefab;

	[SerializeField]
	private GameObject _introductionItemPrefab;

	[SerializeField]
	private UIGrid _deckListGrid;

	[SerializeField]
	private UIScrollView _scrollView;

	[SerializeField]
	private GameObject _confirmLabelForRotation;

	private DialogBase _dialog;

	private DeckIntroductionTask _introductionTask;

	private GameObject _deckViewObj;

	private UICardList _cardList;

	private GameObject _cardDetailObj;

	private CardDetailUI _cardDetail;

	private CardBasePrm.ClanType _classType = CardBasePrm.ClanType.NONE;

	private bool _isUpdateDeckList = true;

	private FormatChangeUI _formatChangeUI;

	private Vector3 _anotationLabelDefaultPosition;

	private int _seriesId;

	public const int LATEST_SERIES_ID = -1;

	public static void Create(GameObject prefab, GameObject parent, int seriesId = -1, Format format = Format.Max)
	{
		GameObject obj = NGUITools.AddChild(parent, prefab);
		DeckIntroduction component = obj.GetComponent<DeckIntroduction>();
		component.SetSeriesId(seriesId);
		component._formatState = format;
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.AddButton(DialogBase.ButtonType.Gray, isReflect: false, Data.SystemText.Get("OtherTop_0065"));
		dialogBase.ClickSe_Btn1 = Se.TYPE.SYS_BTN_DECIDE;
		dialogBase.isNotCloseWindowButton1 = true;
		dialogBase.onPushButton1 = component.CreateSelectSeriesIdDialog;
		dialogBase.AddButton(DialogBase.ButtonType.Close);
		dialogBase.TitleOnOff(flag: false);
		dialogBase.CloseOnOff(flag: false);
		dialogBase.SetSize(DialogBase.Size.XL);
		dialogBase.OnClose = (Action)Delegate.Combine(dialogBase.OnClose, (Action)delegate
		{
			UnityEngine.Object.Destroy(obj);
		});
		dialogBase.SetObj(component._dialogAttachRoot);
		component._dialog = dialogBase;
		dialogBase.gameObject.SetActive(value: false);
	}

	private void SetSeriesId(int seriesId)
	{
		_seriesId = seriesId;
	}

	private IEnumerator Start()
	{
		_anotationLabelDefaultPosition = _labelAnnotation.transform.localPosition;
		yield return LoadAtlas();
		yield return StartCoroutine(StartDeckIntroductionTask());
		SetSeriesId(_introductionTask.DisplaySeriesId);
		yield return LoadResource();
		_dialog.gameObject.SetActive(value: true);
		if (_formatState == Format.Max)
		{
			_formatState = _introductionTask.DisplayFormat;
		}
		InitFormatBtn(_formatState);
		InitClassTab(_introductionTask);
	}

	private void OnDestroy()
	{
		ReleaseResource();
	}

	private IEnumerator StartDeckIntroductionTask()
	{
		_introductionTask = new DeckIntroductionTask();
		if (_seriesId != -1)
		{
			_introductionTask.SetParameter(_seriesId);
		}
		bool isSuccess = false;
		yield return StartCoroutine(Toolbox.NetworkManager.Connect(_introductionTask, delegate
		{
			isSuccess = true;
		}));
		while (!isSuccess)
		{
			yield return null;
		}
	}

	private void ReleaseResource()
	{
		Toolbox.ResourcesManager.RemoveAssetGroup(_resourceList);
		_resourceList.Clear();
		RemoveTopCardResource();
	}

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

	private static string GetClassBGPath(CardBasePrm.ClanType classType, bool isFetch)
	{
		int num = (int)classType;
		return Toolbox.ResourcesManager.GetAssetTypePath("bg_deck_info_" + num.ToString("00"), ResourcesManager.AssetLoadPathType.Background, isFetch);
	}

	private string GetClassCharaPath(CardBasePrm.ClanType classType, bool isFetch)
	{
		string charaTexName = ClassPage.GetCharaTexName(GameMgr.GetIns().GetDataMgr().GetCharaPrmByClassId((int)classType, isCurrentChara: false)
			.skin_id);
			return Toolbox.ResourcesManager.GetAssetTypePath(charaTexName, ResourcesManager.AssetLoadPathType.ClassCharaProfile, isFetch);
		}

		private IEnumerator LoadResource()
		{
			List<string> loadList = new List<string>();
			int num = 9;
			for (int i = 1; i < num; i++)
			{
				loadList.Add(GetClassCharaPath((CardBasePrm.ClanType)i, isFetch: false));
				loadList.Add(GetClassBGPath((CardBasePrm.ClanType)i, isFetch: false));
			}
			yield return StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(loadList, null));
			_resourceList.AddRange(loadList);
		}

		private IEnumerator LoadTopCardResource(CardBasePrm.ClanType classType, Action<CardBasePrm.ClanType> onFinish)
		{
			UIManager.GetInstance().createInSceneCenterLoading();
			List<string> tempLoadList = new List<string>(_loadTopCardAssetList);
			_loadTopCardAssetList.Clear();
			for (int i = 0; i < _introductionTask._result.Count; i++)
			{
				DeckIntroductionTask.IntroductionData introductionData = _introductionTask._result[i];
				string cardMaterialPath = DeckIntroductionItem.GetCardMaterialPath(introductionData.TopCardId);
				if (introductionData.Deck.Format == _formatState && introductionData.Deck.GetDeckClassID() == (int)classType && !_loadTopCardAssetList.Contains(cardMaterialPath))
				{
					_loadTopCardAssetList.Add(cardMaterialPath);
				}
			}
			yield return StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(_loadTopCardAssetList, null));
			Toolbox.ResourcesManager.RemoveAssetGroup(tempLoadList);
			onFinish.Call(classType);
			UIManager.GetInstance().closeInSceneCenterLoading();
		}

		private void RemoveTopCardResource()
		{
			Toolbox.ResourcesManager.RemoveAssetGroup(_loadTopCardAssetList);
			_loadTopCardAssetList.Clear();
		}

		private void InitClassTab(DeckIntroductionTask task)
		{
			_classIcon.atlas = _classIconAtlas;
			int num = 9;
			CardBasePrm.ClanType clanType = CardBasePrm.ClanType.NONE;
			for (int i = 1; i < num; i++)
			{
				CardBasePrm.ClanType classType = (CardBasePrm.ClanType)i;
				Tab tab = _tabList.AddTab(delegate
				{
					ChangePage(classType);
				}, "class_tab_" + i.ToString("00"));
				if (task.IsExistClass(classType, _formatState))
				{
					if (clanType == CardBasePrm.ClanType.NONE)
					{
						clanType = classType;
					}
				}
				else
				{
					_tabList.SetTabToGrayByIndex(i - 1, disable: true);
				}
				tab.name = "Class_" + i + "(Clone)";
			}
			_tabList.Reset();
			_tabList.SelectTabByIndex((int)(clanType - 1), isForceSet: true);
		}

		private bool NeedResurgentConfirmLabel()
		{
			if ((_seriesId == 34 || _seriesId == 33 || _seriesId == 9) && _formatState == Format.Rotation)
			{
				return true;
			}
			return false;
		}

		private void ChangePage(CardBasePrm.ClanType classType)
		{
			if (_classType == classType && !_isUpdateDeckList)
			{
				return;
			}
			_classType = classType;
			_isUpdateDeckList = false;
			ClassCharacterMasterData charaPrmByClassId = GameMgr.GetIns().GetDataMgr().GetCharaPrmByClassId((int)classType, isCurrentChara: false);
			_className.text = charaPrmByClassId._className;
			ClassCharaPrm.SetClassLabelSetting(_className, classType);
			_classCharaTexture.mainTexture = Toolbox.ResourcesManager.LoadObject(GetClassCharaPath(classType, isFetch: true)) as Texture;
			_classBG.mainTexture = Toolbox.ResourcesManager.LoadObject(GetClassBGPath(classType, isFetch: true)) as Texture;
			_confirmLabelForRotation.SetActive(NeedResurgentConfirmLabel());
			_labelAnnotation.transform.localPosition = _anotationLabelDefaultPosition;
			string value;
			if (IsNotCopyToUnlimited(classType) && _formatState == Format.Rotation)
			{
				_labelAnnotation.gameObject.SetActive(value: false);
			}
			else if (_introductionTask.AlternativeFormatAndSeries != null && _introductionTask.AlternativeFormatAndSeries.TryGetValue(_formatState, out value))
			{
				_labelAnnotation.gameObject.SetActive(value: true);
				SetAnnotationText(value);
				if (NeedResurgentConfirmLabel())
				{
					_labelAnnotation.transform.localPosition = new Vector3(_anotationLabelDefaultPosition.x, 209f, _anotationLabelDefaultPosition.z);
				}
			}
			else
			{
				_labelAnnotation.gameObject.SetActive(value: false);
			}
			StartCoroutine(LoadTopCardResource(classType, InitDeckList));
		}

		private bool IsNotCopyToUnlimited(CardBasePrm.ClanType classType)
		{
			for (int i = 0; i < _introductionTask._result.Count; i++)
			{
				DeckData deck = _introductionTask._result[i].Deck;
				if (deck.HasResurgentCard() && deck.IsOutOfRotationFormat && deck.GetDeckClassID() == (int)classType)
				{
					return true;
				}
			}
			return false;
		}

		private void InitDeckList(CardBasePrm.ClanType classType)
		{
			for (int i = 0; i < _deckIntroductionItem.Count; i++)
			{
				_deckIntroductionItem[i].gameObject.SetActive(value: false);
			}
			List<DeckIntroductionTask.IntroductionData> list = new List<DeckIntroductionTask.IntroductionData>();
			for (int j = 0; j < _introductionTask._result.Count; j++)
			{
				DeckIntroductionTask.IntroductionData introductionData = _introductionTask._result[j];
				if (introductionData.Deck.GetDeckClassID() == (int)classType && introductionData.Deck.Format == _formatState)
				{
					list.Add(introductionData);
				}
			}
			for (int k = 0; k < list.Count; k++)
			{
				DeckIntroductionItem deckIntroductionItem = null;
				if (k < _deckIntroductionItem.Count)
				{
					deckIntroductionItem = _deckIntroductionItem[k];
					deckIntroductionItem.gameObject.SetActive(value: true);
				}
				else
				{
					deckIntroductionItem = NGUITools.AddChild(_deckListGrid.gameObject, _introductionItemPrefab).GetComponent<DeckIntroductionItem>();
					_deckIntroductionItem.Add(deckIntroductionItem);
				}
				deckIntroductionItem.Initialize(list[k]);
				deckIntroductionItem.OnClick = delegate(DeckIntroductionTask.IntroductionData data)
				{
					OnClickDeck(data);
				};
			}
			_deckListGrid.Reposition();
			_scrollView.ResetPosition();
		}

		private void InitFormatBtn(Format format)
		{
			_formatState = format;
			FormatChangeUI.FormatCategory defaultFormatCategory = FORMAT_TO_FORMAT_CATEGORY[_formatState];
			FormatChangeUI.FormatCategory anotherFormatCategory = (_introductionTask.IsExistFormat(Format.Crossover) ? FORMAT_TO_FORMAT_CATEGORY[Format.Crossover] : FormatChangeUI.FormatCategory.Invalid);
			_formatChangeUI = FormatChangeUI.Create(defaultFormatCategory, anotherFormatCategory, OnClickFormatBtn);
			_formatChangeUI.ShowOldRotationIcon();
			_dialog.SetObj(_formatChangeUI.gameObject, FORMAT_CHANGE_UI_POSITION);
		}

		private void SetAnnotationText(string serieasName)
		{
			_labelAnnotation.overflowMethod = UILabel.Overflow.ResizeFreely;
			_labelAnnotation.text = Data.SystemText.Get("OtherTop_0068", serieasName);
			_labelAnnotation.ProcessText();
			if (_labelAnnotation.width > 210)
			{
				_labelAnnotation.overflowMethod = UILabel.Overflow.ShrinkContent;
				_labelAnnotation.width = 210;
			}
		}

		private void OnClickDeck(DeckIntroductionTask.IntroductionData data)
		{
			ShowDeckView(data.Deck);
		}

		private void ShowDeckView(DeckData deck)
		{
			string text = "Detail";
			if (_cardDetailObj == null)
			{
				_cardDetailObj = NGUITools.AddChild(base.gameObject, _cardDetailPrefab);
				_cardDetail = _cardDetailObj.GetComponent<CardDetailUI>();
				_cardDetail.Initialize(LayerMask.NameToLayer(text), CardMaster.CardMasterId.Default);
				_cardDetailObj.SetActive(value: false);
			}
			if (_deckViewObj == null)
			{
				_deckViewObj = UnityEngine.Object.Instantiate(_deckViewPrefab);
				_cardList = _deckViewObj.GetComponent<UICardList>();
				_cardList.Init(base.gameObject, _cardDetail, null, OnCloseDeckView, text, in_DetailCameraUse: true, null, 40);
				_deckViewObj.SetActive(value: false);
			}
			_scrollView.DisableSpring();
			UIManager.GetInstance().createInSceneCenterLoading();
			StartCoroutine(CardLoadCoroutine(deck));
		}

		private IEnumerator CardLoadCoroutine(DeckData deck)
		{
			IList<int> cardIdList = deck.GetCardIdList();
			_cardList.RemoveData();
			_loadCardAssetList = _cardList.GetLoadFileList(cardIdList as List<int>);
			yield return StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(_loadCardAssetList, null));
			_cardList.IsEnableMyRotationDisplay = false;
			_cardList.IsConventionDeckIntroduction = true;
			_cardList.SetDeck(deck, null);
			if (deck.Format == Format.Rotation)
			{
				_cardList.SetFormatIcon("icon_rotation_s");
			}
			_cardList.UpdateShortageRedEther();
			_cardList.SetShortageCardVisible(_cardList.IsEnableShortageCardVisible);
			_cardList.SetActiveDeckIntroductionObj(isActive: true);
			yield return null;
			_dialog.SetDisp(inDisp: false);
			_deckViewObj.SetActive(value: true);
			UIManager.GetInstance().closeInSceneCenterLoading();
		}

		private void OnCloseDeckView()
		{
			_dialog.SetDisp(inDisp: true);
			_deckViewObj.SetActive(value: false);
			_scrollView.UpdatePosition();
			if (_loadCardAssetList != null)
			{
				Toolbox.ResourcesManager.RemoveAssetGroup(_loadCardAssetList);
				_loadCardAssetList.Clear();
			}
		}

		private void CreateSelectSeriesIdDialog()
		{
			int prevSeriesId = _seriesId;
			List<int> seriesIdList = _introductionTask.ResultDeckSeriesIdList;
			_ = _introductionTask.ResultDeckSeriesNameList;
			seriesIdList.IndexOf(_seriesId);
			DialogBase dialogBase = DeckIntroductionPeriodSelectDialog.Create(_seriesId, _introductionTask, delegate(int newId)
			{
				_seriesId = newId;
			});
			int num = _dialogAttachRoot.GetComponentInChildren<UIPanel>().depth + 5;
			dialogBase.SetPanelDepth(num);
			dialogBase.InsideObject.GetComponent<UIPanel>().depth = num + 1;
			UIPanel[] componentsInChildren = dialogBase.InsideObject.GetComponentsInChildren<UIPanel>();
			for (int num2 = 0; num2 < componentsInChildren.Length; num2++)
			{
				componentsInChildren[num2].depth += num + 2;
			}
			dialogBase.SetTitleLabel(Data.SystemText.Get("OtherTop_0066"));
			dialogBase.SetButtonLayout(DialogBase.ButtonLayout.DecisionBtn);
			dialogBase.ClickSe_Btn1 = Se.TYPE.SYS_BTN_DECIDE;
			dialogBase.onPushButton1 = delegate
			{
				if (_seriesId != prevSeriesId)
				{
					StartCoroutine(ChangeDeckIntroductionSeries());
				}
			};
			dialogBase.onCloseWithoutSelect = delegate
			{
				_seriesId = prevSeriesId;
			};
		}

		private IEnumerator ChangeDeckIntroductionSeries()
		{
			yield return StartCoroutine(StartDeckIntroductionTask());
			UpdateFormatChangeUI();
			UpdateClassTab();
		}

		private void UpdateClassTab()
		{
			_isUpdateDeckList = true;
			CardBasePrm.ClanType clanType = CardBasePrm.ClanType.NONE;
			for (int i = 1; i < 9; i++)
			{
				CardBasePrm.ClanType clanType2 = (CardBasePrm.ClanType)i;
				if (_introductionTask.IsExistClass(clanType2, _formatState))
				{
					if (clanType == CardBasePrm.ClanType.NONE)
					{
						clanType = clanType2;
					}
					_tabList.SetTabToGrayByIndex(i - 1, disable: false);
				}
				else
				{
					_tabList.SetTabToGrayByIndex(i - 1, disable: true);
				}
			}
			_tabList.Reset();
			_tabList.SelectTabByIndex((int)(clanType - 1), isForceSet: true);
		}

		private void OnClickFormatBtn(FormatChangeUI.FormatCategory formatCategory)
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_TOGGLE_ON);
			Format key = FORMAT_TO_FORMAT_CATEGORY.First((KeyValuePair<Format, FormatChangeUI.FormatCategory> data) => data.Value == formatCategory).Key;
			if (_formatState != key)
			{
				_formatState = key;
				UpdateClassTab();
			}
		}

		private void UpdateFormatChangeUI()
		{
			bool flag = _introductionTask.IsExistFormat(Format.Rotation);
			bool flag2 = _introductionTask.IsExistFormat(Format.Unlimited);
			bool flag3 = _introductionTask.IsExistFormat(Format.Crossover);
			_formatChangeUI.SetEnableFormatButton(FORMAT_TO_FORMAT_CATEGORY[Format.Rotation], flag);
			_formatChangeUI.SetEnableFormatButton(FORMAT_TO_FORMAT_CATEGORY[Format.Unlimited], flag2);
			_formatChangeUI.UpdateAnotherFormatButton(flag3 ? FORMAT_TO_FORMAT_CATEGORY[Format.Crossover] : FormatChangeUI.FormatCategory.Invalid);
			if (_formatState == Format.Crossover && !flag3)
			{
				_formatState = Format.Rotation;
			}
			if (!flag)
			{
				_formatState = Format.Unlimited;
			}
			else if (!flag2)
			{
				_formatState = Format.Rotation;
			}
			_formatChangeUI.ChangeFormat(FORMAT_TO_FORMAT_CATEGORY[_formatState]);
		}
	}
