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

public class MyPageItemHome : MyPageItem
{
	private enum Notification
	{
		Info,
		Max
	}

	private const string TEXTURE_ROOM_CAMPAIGN = "campaign_banner_mypage";

	public static readonly Vector3 TUTORIAL_OFFSET_FOOTER = new Vector3(0f, 19f, 0f);

	[SerializeField]
	private GameObject _bannerPrefab;

	[SerializeField]
	private GameObject _subBannerPrefab;

	private GameObject _bannerObject;

	private GameObject _subBannerObject;

	private MyPageBanner _banner;

	private MyPageSubBanner _subBanner;

	[SerializeField]
	private UIButton _giftButton;

	[SerializeField]
	private GameObject _giftNumberRoot;

	[SerializeField]
	private UILabel _giftNumber;

	private int _giftCount;

	[SerializeField]
	private UIButton _missionButton;

	[SerializeField]
	private UILabel _missionNumberLabel;

	[SerializeField]
	private UIButton _guildButton;

	[SerializeField]
	private UILabel _guildNotificationLabel;

	[SerializeField]
	private GameObject _cardDetailObj;

	[SerializeField]
	private GameObject _contentsRoot;

	[SerializeField]
	private UIButton _battlePassButton;

	[SerializeField]
	private GameObject _homeDialogPrefab;

	[SerializeField]
	private GameObject[] _customMyPageHideList;

	[SerializeField]
	private GameObject[] _customMyPageOnlyList;

	[SerializeField]
	private UIButton _bgCustomButton;

	[SerializeField]
	private UIButton _hideUIButton;

	[SerializeField]
	private GameObject[] _hideUIList;

	[SerializeField]
	private UIButton _restoreUIButton;

	private MyPageHomeStatic _homeStatic;

	private const int GIFT_MAX_COUNT = 99;

	public const int MISSION_MAX_COUNT = 99;

	private bool _isFinishInitialize;

	private int _notificationMask;

	public GameObject ContentsRoot => _contentsRoot;

	public bool IsCustomMyPage => Data.MyPage.data.BGInfo.BGType != MyPageDetail.BGType.Deck;

	public void SetHomeStatic(MyPageHomeStatic homeDeck)
	{
		_homeStatic = homeDeck;
		CreateBanner();
	}

	public override void Initialize(MyPageMenu parent)
	{
		base.Initialize(parent);
		_giftButton.onClick.Add(new EventDelegate(delegate
		{
			OnPushGiftButton();
		}));
		_missionButton.onClick.Add(new EventDelegate(delegate
		{
			OnPushMissionButton();
		}));
		_guildButton.onClick.Add(new EventDelegate(delegate
		{
			OnPushGuildButton();
		}));
		_battlePassButton.onClick.Add(new EventDelegate(delegate
		{
			OnPushBattlePassButton();
		}));
		_bgCustomButton.onClick.Add(new EventDelegate(delegate
		{
			OnPushBGCustomButton();
		}));
		_hideUIButton.onClick.Add(new EventDelegate(delegate
		{
			OnPushHideUIButton();
		}));
		_restoreUIButton.onClick.Add(new EventDelegate(delegate
		{
			OnPushRestoreUIButton();
		}));
		_guildNotificationLabel.gameObject.SetActive(value: false);
	}

	public override void OnMyPageInfoReceive()
	{
		base.OnMyPageInfoReceive();
		_bannerObject.GetComponent<MyPageBanner>().CreateMyPageBanner();
		_subBanner.CreateMyPageBanner();
		UpdateGuildNotification();
		UpdateMissionNumber();
		_giftCount = Data.MyPage.data.unread_mail_count;
		SetUnreadGiftCount(_giftCount);
		UIManager.GetInstance().StartCoroutine(WaitForTreasureBoxCpRewardDialogClosed(delegate
		{
			CheckNotify(delegate
			{
				CheckHomeDialog();
			});
		}));
		UpdateMaintenance();
	}

	private IEnumerator WaitForTreasureBoxCpRewardDialogClosed(Action onFinish)
	{
		while (!base.Parent._treasureBoxCpRewardDialogClosed)
		{
			yield return null;
		}
		onFinish.Call();
	}

	public override void Show(bool skipCardAnimation = false)
	{
		base.Show(skipCardAnimation);
		_contentsRoot.SetActive(value: true);
		if (!IsCustomMyPage)
		{
			_homeStatic.Show();
		}
		_hideUIButton.gameObject.SetActive(IsCustomMyPage);
		_restoreUIButton.gameObject.SetActive(value: false);
		_bannerObject.SetActive(!base.Parent.IsFirstGuid);
		_subBannerObject.SetActive(!base.Parent.IsFirstGuid);
		_bgCustomButton.gameObject.SetActive(Data.Load.data.AcquiredMyPageBGList.Count > 0);
		UpdateMaintenance();
		CheckShopTutorial();
		OnUpdateCustomMyPageEnable(isHomeActive: true);
	}

	public void OnUpdateCustomMyPageEnable(bool isHomeActive)
	{
		_hideUIButton.gameObject.SetActive(IsCustomMyPage);
		if (isHomeActive && IsCustomMyPage)
		{
			_banner.Show();
			_subBanner.Show();
		}
		else
		{
			CommonBackGround.Instance.EffectVisible = true;
		}
		GameObject[] customMyPageHideList = _customMyPageHideList;
		foreach (GameObject gameObject in customMyPageHideList)
		{
			if (gameObject != null)
			{
				gameObject.SetActive(!IsCustomMyPage);
			}
		}
		customMyPageHideList = _customMyPageOnlyList;
		foreach (GameObject gameObject2 in customMyPageHideList)
		{
			if (gameObject2 != null)
			{
				gameObject2.SetActive(IsCustomMyPage);
			}
		}
		if (_homeStatic != null && _homeStatic.CardLoadFinish)
		{
			_homeStatic.SetActive(!IsCustomMyPage && isHomeActive);
		}
		if (!(base.Parent.CustomBGControl != null))
		{
			return;
		}
		base.Parent.CustomBGControl.SetEnable(IsCustomMyPage && isHomeActive);
		if (!(IsCustomMyPage && isHomeActive))
		{
			return;
		}
		base.Parent.CustomBGControl.Show(delegate
		{
			if (isHomeActive && IsCustomMyPage)
			{
				CommonBackGround.Instance.EffectVisible = false;
			}
		});
	}

	private void CheckShopTutorial()
	{
		if (Data.Load.data._userTutorial.TutorialStep == 41)
		{
			DialogBase dialogBase = MyPageMenu.CreateDialogForTutorial();
			dialogBase.SetText(Data.SystemText.Get("Tutorial_0012"));
			dialogBase.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
			base.Parent.SetGuideToOkOnlyDialog(dialogBase);
			UIManager.SetObjectToGrey(_giftButton.gameObject, b: true);
			UIManager.SetObjectToGrey(_missionButton.gameObject, b: true);
			UIManager.SetObjectToGrey(_guildButton.gameObject, b: true);
			UIManager.SetObjectToGrey(_battlePassButton.gameObject, b: true);
			UIManager.SetObjectToGrey(_bgCustomButton.gameObject, b: true);
			_homeStatic.SetTutorialMode();
			dialogBase.OnClose = delegate
			{
				Footer footer = UIManager.GetInstance()._Footer;
				base.Parent.SetGuideEffect(footer._underButtons[5].transform, TUTORIAL_OFFSET_FOOTER, 180f);
			};
		}
	}

	public override void Hide()
	{
		base.Hide();
		_homeStatic.Hide();
		base.gameObject.SetActive(value: true);
		_contentsRoot.SetActive(value: false);
	}

	private void UpdateMaintenance()
	{
		if (!base.Parent.IsFirstGuid)
		{
			bool b = Data.MaintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.GIFT_MAINTENANCE);
			bool b2 = Data.MaintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.MISSION_MAINTENANCE);
			bool b3 = Data.MaintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.GUILD_MAINTENANCE);
			UIManager.SetObjectToGrey(_giftButton.gameObject, b);
			UIManager.SetObjectToGrey(_missionButton.gameObject, b2);
			UIManager.SetObjectToGrey(_guildButton.gameObject, b3);
			bool flag = Data.MaintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.BATTLE_PASS);
			bool flag2 = !Data.IsBattlePassPeriod;
			UIManager.SetObjectToGrey(_battlePassButton.gameObject, flag || flag2);
		}
	}

	public void OnReadGift()
	{
		_giftCount--;
		SetUnreadGiftCount(_giftCount);
	}

	private void CheckNotify(Action onFinish)
	{
		if (!_contentsRoot.activeSelf || Data.Load.data._userTutorial.TutorialStep != 100)
		{
			return;
		}
		DateTime last_announce_time = Data.MyPage.data.last_announce_time;
		if (last_announce_time > GameMgr.GetIns().AnnounceTime)
		{
			GameMgr.GetIns().AnnounceTime = last_announce_time;
			_notificationMask |= 1;
			if (_notificationMask != 0)
			{
				StartCoroutine(ShowNextNotification(onFinish));
			}
		}
		else
		{
			onFinish.Call();
		}
	}

	private void CheckHomeDialog()
	{
		MyPageHomeDialogData myPageHomeDialogData = Data.MyPage.data.MyPageHomeDialogData;
		if (myPageHomeDialogData.IsEnable)
		{
			MyPageHomeDialog.Create(_homeDialogPrefab, myPageHomeDialogData, null);
		}
	}

	private IEnumerator ShowNextNotification(Action onFinish)
	{
		while (UIManager.GetInstance().isFading() || UIManager.GetInstance().isOpenDialog())
		{
			yield return null;
		}
		if (_notificationMask == 0)
		{
			yield break;
		}
		int i;
		for (i = 0; (_notificationMask & (1 << i)) == 0; i++)
		{
		}
		_notificationMask ^= 1 << i;
		DialogBase dialogBase = null;
		if (1 == 0)
		{
			yield break;
		}
		if (_notificationMask != 0 && dialogBase != null)
		{
			dialogBase.OnClose = delegate
			{
				StartCoroutine(ShowNextNotification(onFinish));
			};
		}
		else
		{
			onFinish.Call();
		}
	}

	public void OnMyPageFadeOpen(bool isHomeActive)
	{
		OnUpdateCustomMyPageEnable(isHomeActive);
		_restoreUIButton.gameObject.SetActive(value: false);
	}

	private void Update()
	{
		if (!_isFinishInitialize && _homeStatic.CardLoadFinish)
		{
			_isFinishInitialize = true;
			GameObject obj = _giftButton.gameObject.transform.parent.gameObject;
			bool activeSelf = obj.activeSelf;
			obj.SetActive(value: false);
			obj.SetActive(value: true);
			obj.SetActive(activeSelf);
		}
	}

	private void OnPushGiftButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		MailTopTask mailTopTask = GameMgr.GetIns().GetMailTopTask();
		mailTopTask.SetParameter(Data.Load.data._userTutorial.TutorialStep != 100);
		StartCoroutine(Toolbox.NetworkManager.Connect(mailTopTask, OnRequestGift));
	}

	private void OnRequestGift(NetworkTask.ResultCode error)
	{
		UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.Mail);
	}

	private void OnPushMissionButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		MissionInfoTask missionInfoTask = GameMgr.GetIns().GetMissionInfoTask();
		missionInfoTask.SetParameter();
		StartCoroutine(Toolbox.NetworkManager.Connect(missionInfoTask, OpenMission));
	}

	private void OpenMission(NetworkTask.ResultCode error)
	{
		UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.Mission);
	}

	public void UpdateMissionNumber()
	{
		if (!(_missionNumberLabel.gameObject == null))
		{
			int unreceived_mission_reward_count = Data.MyPage.data.unreceived_mission_reward_count;
			_missionNumberLabel.gameObject.SetActive(unreceived_mission_reward_count > 0);
			_missionNumberLabel.text = unreceived_mission_reward_count.ToString();
			if (unreceived_mission_reward_count > 99)
			{
				_missionNumberLabel.text = 99 + "+";
			}
			else
			{
				_missionNumberLabel.text = unreceived_mission_reward_count.ToString();
			}
		}
	}

	private void OnPushBattlePassButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.BattlePass);
	}

	private void OnPushBGCustomButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		MyPageBGCustomDialog.Create(OnDecideMyPageBG);
	}

	private void OnPushHideUIButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		ToggleUIVisible(visible: false);
	}

	private void OnPushRestoreUIButton()
	{
		ToggleUIVisible(visible: true);
	}

	private void ToggleUIVisible(bool visible)
	{
		GameObject[] hideUIList = _hideUIList;
		foreach (GameObject gameObject in hideUIList)
		{
			if (gameObject != null)
			{
				gameObject.SetActive(visible);
			}
		}
		base.Parent.TopBar.gameObject.SetActive(visible);
		UIManager.GetInstance()._Footer.ShowFooterMenu(visible);
		_restoreUIButton.gameObject.SetActive(!visible);
	}

	public void OnDecideMyPageBG(MyPageDetail.BGType bgType, string selectId, List<string> randomIdList)
	{
		if (bgType == MyPageDetail.BGType.RandomBG)
		{
			selectId = DecideRandomBG(randomIdList);
		}
		base.Parent.ChangeMyPageBG(bgType, selectId);
	}

	public static string DecideRandomBG(List<string> randomIdList)
	{
		System.Random random = new System.Random();
		return randomIdList[random.Next(0, randomIdList.Count)];
	}

	public void ShowBanner()
	{
		if (Data.Load.data._userTutorial.TutorialStep == 100)
		{
			_banner.Show();
			_subBanner.Show();
		}
	}

	public void HideBanner()
	{
		_banner.Hide();
		_subBanner.Hide();
	}

	private void CreateBanner()
	{
		_bannerObject = NGUITools.AddChild(_contentsRoot, _bannerPrefab);
		_banner = _bannerObject.GetComponent<MyPageBanner>();
		_homeStatic.Initialize();
		_subBannerObject = NGUITools.AddChild(_contentsRoot, _subBannerPrefab);
		_subBanner = _subBannerObject.GetComponent<MyPageSubBanner>();
		_banner._isFirstTips = false;
		_subBanner._isFirstTips = false;
	}

	private void OnPushGuildButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.Guild);
	}

	private void UpdateGuildNotification()
	{
		_guildNotificationLabel.gameObject?.SetActive(IsGuildNotification());
	}

	private bool IsGuildNotification()
	{
		GuildNotification guildNotification = Data.MyPageNotifications.data.GuildNotification;
		if (!guildNotification.GuildId.HasValue)
		{
			return guildNotification.IsInvited;
		}
		if (guildNotification.GuildId.Value != PlayerPrefsWrapper.GetValue(PlayerPrefsWrapper.JOINING_GUILD_ID))
		{
			return true;
		}
		if (guildNotification.GuildRoomMessageId.HasValue && guildNotification.GuildRoomMessageId.Value > PlayerPrefsWrapper.GetValue(PlayerPrefsWrapper.READ_LATEST_GUILD_CHAT_MESSAGE_ID))
		{
			return true;
		}
		if (guildNotification.IsJoinRequest)
		{
			return true;
		}
		return false;
	}

	public void SetFirstTips()
	{
		_banner._isFirstTips = true;
		_banner.SetActive(inActive: false);
		_subBanner._isFirstTips = true;
		_subBanner.SetActive(inActive: false);
		_homeStatic.SetTutorialMode();
	}

	public void SetGuideEffectToGiftButton()
	{
		base.Parent.SetGuideEffect(_giftButton.transform, new Vector3(-14f, 0f, 0f), -90f);
	}

	public void SetGiftReceiveTutorialMode()
	{
		UIManager.SetObjectToGrey(_missionButton.gameObject, b: true);
		UIManager.SetObjectToGrey(_guildButton.gameObject, b: true);
		UIManager.SetObjectToGrey(_battlePassButton.gameObject, b: true);
		UIManager.SetObjectToGrey(_bgCustomButton.gameObject, b: true);
	}

	public void FinishFirstTips()
	{
		UIManager.SetObjectToGrey(_giftButton.gameObject, b: false);
		UIManager.SetObjectToGrey(_missionButton.gameObject, b: false);
		UIManager.SetObjectToGrey(_guildButton.gameObject, b: false);
		UIManager.SetObjectToGrey(_battlePassButton.gameObject, b: false);
		UIManager.SetObjectToGrey(_bgCustomButton.gameObject, b: false);
		_homeStatic.FinishFirstTips();
	}

	public void SetUnreadGiftCount(int count)
	{
		if (!(_giftNumberRoot.gameObject == null))
		{
			_giftNumberRoot.gameObject.SetActive(count > 0);
			if (count > 99)
			{
				_giftNumber.text = 99 + "+";
			}
			else
			{
				_giftNumber.text = count.ToString();
			}
		}
	}

	public void HideAndRepositionSubBanner(string click)
	{
		_subBanner.HideAndRepositionBanner(click);
	}
}
