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

public class MyPageItemCard : MyPageItem
{
	private enum DeckMenuType
	{
		Default,
		WithDeckIntroduction,
		CrossoverWithDeckIntroduction
	}

	private readonly int DECK_MENU_TOP_BUTTON_POSITION_Y_PREROTATION = 145;

	private readonly int DECK_MENU_TOP_BUTTON_POSITION_Y = 49;

	[SerializeField]
	private UIButton _deckButton;

	[SerializeField]
	private UIButton _cardListButton;

	[SerializeField]
	private GameObject _deckEditMenuRoot;

	[SerializeField]
	private GameObject[] _deckEditMenuTypeRoots;

	[SerializeField]
	private UIButton[] _deckUnlimitedButtons;

	[SerializeField]
	private UIButton[] _deckRotationButtons;

	[SerializeField]
	private UILabel[] _rotationPeriodLabel;

	[SerializeField]
	private UIButton _deckPreRotationButton;

	[SerializeField]
	private UIButton _deckCrossoverButton;

	[SerializeField]
	private UIButton _deckMyRotationButton;

	[SerializeField]
	private UIButton[] _deckIntroductionButtons;

	[SerializeField]
	private GameObject _deckIntroductionPrefab;

	[SerializeField]
	private Transform[] _firstTipsPositions;

	[SerializeField]
	private UILabel _preReleaseTimeLimitLabel;

	private Effect _firstTipsEffect;

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

	private bool _tipsCrystalChanged;

	private DeckMenuType _deckMenuType;

	private const int CARD_INDEX_DECK = 0;

	private const int CARD_INDEX_CARD_LIST = 1;

	public override void Initialize(MyPageMenu parent)
	{
		base.Initialize(parent);
		UIButton[] deckUnlimitedButtons = _deckUnlimitedButtons;
		for (int i = 0; i < deckUnlimitedButtons.Length; i++)
		{
			deckUnlimitedButtons[i].onClick.Add(new EventDelegate(OnPushDeckEditUnlimited));
		}
		deckUnlimitedButtons = _deckRotationButtons;
		for (int i = 0; i < deckUnlimitedButtons.Length; i++)
		{
			deckUnlimitedButtons[i].onClick.Add(new EventDelegate(OnPushDeckEditRotation));
		}
		deckUnlimitedButtons = _deckIntroductionButtons;
		for (int i = 0; i < deckUnlimitedButtons.Length; i++)
		{
			deckUnlimitedButtons[i].onClick.Add(new EventDelegate(OnPushDeckIntroduction));
		}
		_deckPreRotationButton.onClick.Add(new EventDelegate(OnClickDeckEditPreRotation));
		_deckCrossoverButton.onClick.Add(new EventDelegate(OnClickDeckEditCrossover));
		_deckMyRotationButton.onClick.Add(new EventDelegate(OnClickDeckEditMyRotation));
		InitializeRotationPeriodLabel();
		SaveDefaultPosition(_deckEditMenuRoot);
	}

	public override void Show(bool skipCardAnimation = false)
	{
		base.Show(skipCardAnimation);
		_deckEditMenuRoot.SetActive(value: false);
		_deckCrossoverButton.gameObject.SetActive(IsEnableSpecialFormat(Format.Crossover));
		_deckMyRotationButton.gameObject.SetActive(IsEnableSpecialFormat(Format.MyRotation));
		_deckMenuType = GetDeckMenuType();
		SetupDeckMenuType();
		RestoreCardPanelPosition();
		StartCardPanelAppearAnimation();
		_deckButton.onClick.Clear();
		_deckButton.onClick.Add(new EventDelegate(delegate
		{
			OnPushDeckButton();
		}));
		_cardListButton.onClick.Clear();
		_cardListButton.onClick.Add(new EventDelegate(delegate
		{
			OnPushCardListButton();
		}));
		RestoreDefaultPosition(_deckEditMenuRoot);
		switch (Prerelease.Status)
		{
		case Prerelease.eStatus.PRE_ROTATION:
			_preReleaseTimeLimitLabel.text = Data.SystemText.Get("MyPage_0048", ConvertTime.ToLocal(Prerelease.Instance.EndTime));
			break;
		case Prerelease.eStatus.DISPLAY_DECK_ONLY:
			_preReleaseTimeLimitLabel.text = Data.SystemText.Get("MyPage_0070");
			break;
		}
		if (_deckMenuType == DeckMenuType.Default)
		{
			bool flag = Prerelease.Status != Prerelease.eStatus.NONE;
			_deckPreRotationButton.gameObject.SetActive(flag);
			int num = (flag ? DECK_MENU_TOP_BUTTON_POSITION_Y_PREROTATION : DECK_MENU_TOP_BUTTON_POSITION_Y);
			UIButton[] deckRotationButtons = _deckRotationButtons;
			foreach (UIButton obj in deckRotationButtons)
			{
				Vector3 localPosition = obj.transform.localPosition;
				obj.transform.localPosition = new Vector3(localPosition.x, num, localPosition.z);
			}
		}
		UIManager.GetInstance().CheckFirstTips(FirstTips.TipsType.Card);
	}

	private void InitializeRotationPeriodLabel()
	{
		string shortName = Data.Master.CardSetNameMgr.Get(Data.Load.data.RotationFirstCardPackId.ToString()).ShortName;
		string shortName2 = Data.Master.CardSetNameMgr.Get(Data.Load.data.RotationLatestCardPackId.ToString()).ShortName;
		int num = Data.Load.data.RotationFirstCardPackId - 10000;
		int num2 = Data.Load.data.RotationLatestCardPackId - 10000;
		string text = Data.SystemText.Get("MyPage_0115", shortName, shortName2, num.ToString(), num2.ToString());
		UILabel[] rotationPeriodLabel = _rotationPeriodLabel;
		for (int i = 0; i < rotationPeriodLabel.Length; i++)
		{
			rotationPeriodLabel[i].text = text;
		}
	}

	private void OnDisable()
	{
		if (_firstTipsEffect != null)
		{
			_firstTipsEffect.Stop();
		}
		if (_tipsCrystalChanged)
		{
			_tipsCrystalChanged = false;
		}
	}

	private void OnPushDeckButton()
	{
		if (base.IsCardMoving)
		{
			return;
		}
		_deckMenuType = GetDeckMenuType();
		SetupDeckMenuType();
		base.Parent.SetDefaultBackButtonHandler();
		base.CardAnimation.OnClicked(0);
		ShowDeckMenu();
		if (PlayerPrefsWrapper.GetBool(PlayerPrefsWrapper.FIRST_TIPS_AFTER_ROTATION_USER_FLAG))
		{
			if (FirstTips.IsFirstTipsOpen(FirstTips.TipsType.DeckAfterFormatUser))
			{
				FirstTips.TipsType tipsType = FirstTips.TipsType.DeckAfterFormatUser;
				FirstTips firstTips = UIManager.GetInstance().CheckFirstTips(tipsType);
				if (firstTips != null)
				{
					firstTips.IsEnableBackKeyChange = false;
				}
				UIManager.SetObjectToGrey(base.TopBar.BuyCrystalButton.gameObject, b: true);
				base.TopBar.BuyCrystalButton.isEnabled = false;
				StartCoroutine(FirstTipsCoroutine(tipsType));
				FirstTips.SaveFinishFirstTips(FirstTips.TipsType.DeckBeforeFormatUser);
				SetDeckTutorialMode(isTutorial: true);
			}
		}
		else if (FirstTips.IsFirstTipsOpen(FirstTips.TipsType.DeckBeforeFormatUser))
		{
			UIManager.GetInstance().CheckFirstTips(FirstTips.TipsType.DeckBeforeFormatUser);
		}
		_deckEditMenuRoot.SetActive(value: false);
		_deckEditMenuRoot.SetActive(value: true);
	}

	private IEnumerator FirstTipsCoroutine(FirstTips.TipsType type)
	{
		bool finishLoad = false;
		_loadAssetList.AddRange(GameMgr.GetIns().GetEffectMgr().InitCommonEffect("Json/EffectTutorialData", isBattle: true, isField: false, isBattleEffect: false, delegate
		{
			finishLoad = true;
		}));
		while (FirstTips.IsFirstTipsOpen(type))
		{
			yield return null;
		}
		while (!finishLoad)
		{
			yield return null;
		}
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetTitleLabel(Data.SystemText.Get("Dia_Home_001_Title"));
		dialogBase.SetText(Data.SystemText.Get("FirstTips_0035"));
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
		dialogBase.SetPanelDepth(3000);
		dialogBase.OnClose = delegate
		{
			Vector3 pos = _firstTipsPositions[(int)_deckMenuType].TransformPoint(0f, 0f, 0f);
			_firstTipsEffect = GameMgr.GetIns().GetEffectMgr().Start(EffectMgr.EffectType.CMN_TUTORIAL_TAP_1, pos, MyPageItemSoroPlay.EFFECT_ROTATION);
			_firstTipsEffect.Play(pos, MyPageItemSoroPlay.EFFECT_ROTATION);
			_firstTipsEffect.transform.parent = _firstTipsPositions[(int)_deckMenuType];
		};
	}

	private void SetDeckTutorialMode(bool isTutorial)
	{
		Footer footer = UIManager.GetInstance()._Footer;
		for (int i = 0; i < footer._underButtons.Length; i++)
		{
			footer.SetButtonEnableColorChange(i, !isTutorial);
		}
		UIButton[] deckUnlimitedButtons = _deckUnlimitedButtons;
		for (int j = 0; j < deckUnlimitedButtons.Length; j++)
		{
			UIManager.SetObjectToGrey(deckUnlimitedButtons[j].gameObject, isTutorial);
		}
		deckUnlimitedButtons = _deckIntroductionButtons;
		for (int j = 0; j < deckUnlimitedButtons.Length; j++)
		{
			UIManager.SetObjectToGrey(deckUnlimitedButtons[j].gameObject, isTutorial);
		}
		UIManager.SetObjectToGrey(_deckPreRotationButton.gameObject, isTutorial);
		UIManager.SetObjectToGrey(_deckCrossoverButton.gameObject, isTutorial);
		UIManager.SetObjectToGrey(_deckMyRotationButton.gameObject, isTutorial);
		base.TopBar.SetBackButtonEnable(!isTutorial);
	}

	private void SetupDeckMenuType()
	{
		foreach (DeckMenuType value in Enum.GetValues(typeof(DeckMenuType)))
		{
			GameObject gameObject = _deckEditMenuTypeRoots[(int)value];
			if (!(gameObject == null))
			{
				gameObject.SetActive(value == _deckMenuType);
			}
		}
	}

	private bool IsEnableSpecialFormat(Format format)
	{
		if (!DeckListUI.CheckSpecialFormatPeriod(format))
		{
			return false;
		}
		return DeckListUtility.DeckGroupDataBaseClone().Any((DeckGroup deckgroup) => deckgroup.DeckFormat == format);
	}

	private DeckMenuType GetDeckMenuType()
	{
		if (Prerelease.Status != Prerelease.eStatus.NONE)
		{
			return DeckMenuType.Default;
		}
		if (IsEnableSpecialFormat(Format.Crossover) || IsEnableSpecialFormat(Format.MyRotation))
		{
			return DeckMenuType.CrossoverWithDeckIntroduction;
		}
		if (!Data.MyPage.data._bannerList.Any((MyPageBannerBase.BannerInfo info) => info.Click == "deck_intro_rotation" || info.Click == "deck_intro_unlimited"))
		{
			return DeckMenuType.Default;
		}
		return DeckMenuType.WithDeckIntroduction;
	}

	private void OnPushCardListButton()
	{
		if (!base.IsCardMoving)
		{
			base.CardAnimation.OnClicked(1);
			UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.CardAllList);
		}
	}

	private void ShowDeckMenu()
	{
		base.IsEnableFooterCurrentMenu = true;
		base.Parent.SetBackButtonEnable();
		_deckButton.onClick.Clear();
		base.TopBar.SetTitleLabel(Data.SystemText.Get("MyPage_0014"));
		MoveCardPanelLeftPosition(_deckButton.gameObject);
		FadeOutCardPanelAndNonActive(_cardListButton.GetComponent<MyPageCardPanel>());
		_deckEditMenuRoot.SetActive(value: true);
		AppearAnimationFromRight(_deckEditMenuRoot);
	}

	private void OnPushDeckEditUnlimited()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		DeckListUI.ChangeSceneToDeckList(Format.Unlimited);
	}

	private void OnPushDeckEditRotation()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		DeckListUI.ChangeSceneToDeckList(Format.Rotation);
		if (_firstTipsEffect != null)
		{
			UnityEngine.Object.Destroy(_firstTipsEffect.gameObject);
		}
	}

	private void OnClickDeckEditPreRotation()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		DeckListUI.ChangeSceneToDeckList(Format.PreRotation);
	}

	private void OnClickDeckEditCrossover()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		DeckListUI.ChangeSceneToDeckList(Format.Crossover);
	}

	private void OnClickDeckEditMyRotation()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		DeckListUI.ChangeSceneToDeckList(Format.MyRotation);
	}

	private void OnPushDeckIntroduction()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		DeckIntroduction.Create(_deckIntroductionPrefab, base.gameObject, -1, Format.Rotation);
	}

	public void GoToCardDeck()
	{
		ShowDeckMenu();
	}

	private void OnDestroy()
	{
		if (_loadAssetList.Count > 0)
		{
			Toolbox.ResourcesManager.RemoveAssetGroup(_loadAssetList);
		}
	}
}
