using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Cute;
using UnityEngine;
using Wizard;
using Wizard.Story;
using Wizard.Story.ChapterSelection.SelectionProcessing.BattleResult;

public class StoryNextSceneSelector : INextSceneSelector
{
	private BattleResultUIController m_battleResultNewControl;

	private const int BUILD_DECK_RENTAL_LOSE_COUNT = 2;

	private const float INTERVAL_OPEN_DIALOG = 0.1f;

	private bool _isEnableDeckRentalAssist;

	private bool _isWin;

	private int _loseCount;

	private readonly IProcessing _firstSelectionProcessing;

	private static bool _isHistoryOfUsingTrialDeck;

	private SelectedStoryInfo SelectedStoryInfo => Data.SelectedStoryInfo;

	private UIManager.ViewScene ChapterSelectionView => SelectedStoryInfo.ChapterSelectionView;

	private static KeyValuePair<string, int> DeckRentalPlayerPrefsKey => new KeyValuePair<string, int>($"{PlayerPrefsWrapper.STORY_BATTLE_LOSE_COUNT}{Data.SelectedStoryInfo.StoryId}", 0);

	public StoryNextSceneSelector(BattleResultUIController battleResultControl)
	{
		m_battleResultNewControl = battleResultControl;
		_loseCount = GetDeckRentalLoseCount();
		_firstSelectionProcessing = CreateSelectionProcessings();
	}

	public static int GetDeckRentalLoseCount()
	{
		return PlayerPrefsWrapper.GetValue(DeckRentalPlayerPrefsKey);
	}

	public static void SetDeckRentalLoseCount(int loseCount)
	{
		PlayerPrefsWrapper.SetValue(DeckRentalPlayerPrefsKey, loseCount);
	}

	public static void ResetHistoryOfUsingPreBuildDeck()
	{
		_isHistoryOfUsingTrialDeck = false;
	}

	public void Setup(bool isWin, GameObject gameObject)
	{
		_isWin = isWin;
		if (Data.StoryInfo.IsPreBuildDeck)
		{
			GameMgr.GetIns().GetDataMgr().LastSelectDeckAttributeType = DeckAttributeType.BuildDeck;
		}
		_isHistoryOfUsingTrialDeck = _isHistoryOfUsingTrialDeck || Data.StoryInfo.IsTrialDeck;
		if (!isWin)
		{
			Data.StoryInfo.IsPreBuildDeck = false;
		}
		int num = 0;
		if (IsEnableDeckRentalStory() && !isWin)
		{
			num = _loseCount + 1;
		}
		if (IsEnableDeckRentalStory() && num >= 2 && !_isHistoryOfUsingTrialDeck)
		{
			_isEnableDeckRentalAssist = true;
		}
		else
		{
			ShowResult(isWin);
		}
	}

	private void ShowResult(bool isWin)
	{
		m_battleResultNewControl.MissionBtnObj.labels[0].text = Data.SystemText.Get("Battle_0200");
		m_battleResultNewControl.MissionBtnObj.buttons[0].onClick.Clear();
		m_battleResultNewControl.MissionBtnObj.buttons[0].onClick.Add(new EventDelegate(delegate
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
			UIManager.GetInstance().createInSceneCenterLoading();
			MissionInfoTask missionInfoTask = GameMgr.GetIns().GetMissionInfoTask();
			missionInfoTask.SetParameter();
			m_battleResultNewControl.StartCoroutine(Toolbox.NetworkManager.Connect(missionInfoTask, delegate
			{
				m_battleResultNewControl.CreateMissionList();
			}, BaseTask.OnRequestFailed, BaseTask.OnFailedErrorCode));
		}));
		m_battleResultNewControl.HomeBtnObj.buttons[0].onClick.Clear();
		m_battleResultNewControl.RetryBtnObj.buttons[0].onClick.Clear();
		if (isWin)
		{
			m_battleResultNewControl.HomeBtnObj.gameObject.SetActive(value: false);
			m_battleResultNewControl.RetryBtnObj.labels[0].text = Data.SystemText.Get("Story_0004");
			m_battleResultNewControl.RetryBtnObj.buttons[0].onClick.Add(new EventDelegate(delegate
			{
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE_TRANS);
				if (isWin)
				{
					GameMgr.GetIns().GetBattleCtrl().BattleEnd(UIManager.ViewScene.Scenario2);
				}
			}));
			return;
		}
		m_battleResultNewControl.HomeBtnObj.labels[0].text = Data.SystemText.Get("Story_0002");
		m_battleResultNewControl.HomeBtnObj.buttons[0].onClick.Add(new EventDelegate(delegate
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_CANCEL_TRANS);
			GameMgr.GetIns().GetBattleCtrl().BattleEnd(ChapterSelectionView);
		}));
		m_battleResultNewControl.RetryBtnObj.labels[0].text = Data.SystemText.Get("Battle_0204");
		m_battleResultNewControl.RetryBtnObj.buttons[0].onClick.Add(new EventDelegate(delegate
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
			CreateDeckSelectForStory();
		}));
	}

	private void ShowDeckRentalConfirmDialog()
	{
		SystemText systemText = Data.SystemText;
		DialogBase dialogBase = UIManager.GetInstance().CreateConfirmationDialog(systemText.Get("Story_0054"));
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_CancelBtn);
		dialogBase.SetTitleLabel(systemText.Get("Story_0053"));
		dialogBase.SetButtonText(systemText.Get("Story_0055"), systemText.Get("Common_0005"));
		dialogBase.onPushButton1 = delegate
		{
			CreateDeckSelectForStoryWithPrimaryTrialDeck();
		};
		dialogBase.onPushButton2 = delegate
		{
			DeckRentalFinish();
		};
		dialogBase.onCloseWithoutSelect = delegate
		{
			DeckRentalFinish();
		};
	}

	private void DeckRentalFinish()
	{
		if (!Data.StoryInfo.IsTrialDeck)
		{
			SetDeckRentalLoseCount(0);
			ShowResult(_isWin);
			ResultMenuAppearAnimation();
		}
	}

	public void CreateDeckSelectForStory()
	{
		Parameter param = new Parameter(UIManager.GetInstance(), SelectedStoryInfo, (Format)PlayerPrefsWrapper.GetValue(PlayerPrefsWrapper.LAST_SELECT_DECK_FORMAT), DeckAttributeType.Invalid, null, null);
		ExecuteSelectionProcessings(param);
	}

	private void CreateDeckSelectForStoryWithPrimaryTrialDeck()
	{
		bool isDecidedDeck = false;
		Parameter param = new Parameter(UIManager.GetInstance(), SelectedStoryInfo, Format.Max, DeckAttributeType.TrialDeck, delegate
		{
			if (!isDecidedDeck)
			{
				ShowDeckRentalConfirmDialog();
			}
		}, delegate
		{
			isDecidedDeck = true;
		});
		ExecuteSelectionProcessings(param);
		Data.StoryInfo.IsTrialDeck = false;
	}

	private bool IsEnableDeckRentalStory()
	{
		if (SelectedStoryInfo.IsFixedDeck)
		{
			return false;
		}
		if (SelectedStoryInfo.ClearStatus == StoryChapterData.ChapterClearStatus.Cleared)
		{
			return false;
		}
		return true;
	}

	private IEnumerator ShowRentalDeckCoroutine()
	{
		yield return new WaitForSeconds(0.5f);
		bool isWaitRewardDialog = m_battleResultNewControl.IsRewardWait;
		while (m_battleResultNewControl.IsRewardWait)
		{
			yield return null;
		}
		if (isWaitRewardDialog)
		{
			yield return new WaitForSeconds(0.1f);
		}
		ShowDeckRentalConfirmDialog();
	}

	public void Show()
	{
		if (_isEnableDeckRentalAssist)
		{
			m_battleResultNewControl.StartCoroutine(ShowRentalDeckCoroutine());
		}
		else
		{
			ResultMenuAppearAnimation();
		}
	}

	private void ResultMenuAppearAnimation()
	{
		iTween.MoveTo(m_battleResultNewControl.ButtonGrid.gameObject, iTween.Hash("position", m_battleResultNewControl.DefaultPosDict["ButtonGrid"], "time", 0.5f, "islocal", true, "easetype", iTween.EaseType.easeOutExpo));
	}

	private static IProcessing CreateSelectionProcessings()
	{
		IProcessing[] array = new IProcessing[6]
		{
			new DeckSelectionDialogDisplay(),
			new ChapterCharaDecider(),
			new DownloadInfoGetter(),
			new DeckSelectionConfirmDialogDisplay(),
			new Download(),
			new BattleStarter()
		};
		for (int i = 0; i < array.Length - 1; i++)
		{
			array[i].NextProcessing = array[i + 1];
		}
		return array.FirstOrDefault();
	}

	private void ExecuteSelectionProcessings(Parameter param)
	{
		if (_firstSelectionProcessing != null)
		{
			_firstSelectionProcessing.Execute(param);
		}
	}
}
