using UnityEngine;

namespace Wizard.Bingo;

public class BingoRewardsDialog : MonoBehaviour
{
	public enum MissionViewTab
	{
		First,
		Second,
		Third,
		Fourth,
		AfterFifth
	}

	[SerializeField]
	private GameObject _bingoMissionItemPrefab;

	[SerializeField]
	private UIGrid _grid;

	private ResourceHandler _resourceHandler;

	[SerializeField]
	private UIButton _firstRewardsButton;

	[SerializeField]
	private UIButton _secondRewardsButton;

	[SerializeField]
	private UIButton _thirdRewardsButton;

	[SerializeField]
	private UIButton _fourthRewardsButton;

	[SerializeField]
	private UIButton _fifthRewardsButton;

	[SerializeField]
	private UIScrollView _scrollPanel;

	private BingoInfoTask.BingoInfoData _bingoInfoData;

	private MissionViewTab _currentViewTab;

	public static void Create(GameObject prefab, BingoInfoTask.BingoInfoData bingoData)
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		BingoRewardsDialog rewardDialog = Object.Instantiate(prefab).GetComponent<BingoRewardsDialog>();
		rewardDialog._resourceHandler = rewardDialog.gameObject.AddMissingComponent<ResourceHandler>();
		dialogBase.SetObj(rewardDialog.gameObject);
		dialogBase.SetSize(DialogBase.Size.M);
		dialogBase.SetTitleLabel(Data.SystemText.Get("Bingo_0006"));
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.CloseBtn);
		dialogBase.OnClose = delegate
		{
			rewardDialog._resourceHandler.UnloadAll();
		};
		rewardDialog.Initialize(bingoData);
	}

	private void Initialize(BingoInfoTask.BingoInfoData bingoInfoData)
	{
		_bingoInfoData = bingoInfoData;
		MissionViewTab viewTab = (MissionViewTab)Mathf.Clamp(_bingoInfoData.SheetNum - 1, 0, 4);
		OnPushTabButton(viewTab, firstCall: true);
		_firstRewardsButton.onClick.Clear();
		_firstRewardsButton.GetComponentInChildren<UILabel>().text = string.Format(Data.SystemText.Get("Bingo_0012"), GetCurrentTabIndex(MissionViewTab.First));
		_firstRewardsButton.onClick.Add(new EventDelegate(delegate
		{
			OnPushTabButton(MissionViewTab.First);
		}));
		_secondRewardsButton.onClick.Clear();
		_secondRewardsButton.GetComponentInChildren<UILabel>().text = string.Format(Data.SystemText.Get("Bingo_0012"), GetCurrentTabIndex(MissionViewTab.Second));
		_secondRewardsButton.onClick.Add(new EventDelegate(delegate
		{
			OnPushTabButton(MissionViewTab.Second);
		}));
		_thirdRewardsButton.onClick.Clear();
		_thirdRewardsButton.GetComponentInChildren<UILabel>().text = string.Format(Data.SystemText.Get("Bingo_0012"), GetCurrentTabIndex(MissionViewTab.Third));
		_thirdRewardsButton.onClick.Add(new EventDelegate(delegate
		{
			OnPushTabButton(MissionViewTab.Third);
		}));
		_fourthRewardsButton.onClick.Clear();
		_fourthRewardsButton.GetComponentInChildren<UILabel>().text = string.Format(Data.SystemText.Get("Bingo_0012"), GetCurrentTabIndex(MissionViewTab.Fourth));
		_fourthRewardsButton.onClick.Add(new EventDelegate(delegate
		{
			OnPushTabButton(MissionViewTab.Fourth);
		}));
		_fifthRewardsButton.onClick.Clear();
		_fifthRewardsButton.GetComponentInChildren<UILabel>().text = string.Format(Data.SystemText.Get("Bingo_0013"), GetCurrentTabIndex(MissionViewTab.AfterFifth));
		_fifthRewardsButton.onClick.Add(new EventDelegate(delegate
		{
			OnPushTabButton(MissionViewTab.AfterFifth);
		}));
	}

	private int GetCurrentTabIndex(MissionViewTab missionViewTab)
	{
		return (int)(missionViewTab + 1);
	}

	private void OnPushTabButton(MissionViewTab viewTab, bool firstCall = false)
	{
		if (_currentViewTab != viewTab || firstCall)
		{
			_currentViewTab = viewTab;
			UpdateMissionView(viewTab);
			if (!firstCall)
			{
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_TOGGLE_ON);
			}
		}
	}

	private void UpdateMissionView(MissionViewTab viewTab)
	{
		ChangeTab(viewTab);
		UpdateTabSprite(viewTab);
	}

	public void ChangeTab(MissionViewTab viewTab)
	{
		_bingoInfoData.AllLineRewardsListDic.TryGetValue((int)viewTab, out var value);
		_grid.transform.DestroyChildren();
		for (int i = 0; i < value.Count; i++)
		{
			bool flag = i + 1 == value.Count;
			AchievementWindowBase component = NGUITools.AddChild(_grid.gameObject, _bingoMissionItemPrefab).GetComponent<AchievementWindowBase>();
			component.gameObject.SetActive(value: true);
			component.SetBingoRewardDetails(value[i], !flag, _resourceHandler);
		}
		_grid.Reposition();
		_scrollPanel.ResetPosition();
	}

	private void UpdateTabSprite(MissionViewTab scene)
	{
		switch (scene)
		{
		case MissionViewTab.First:
			_firstRewardsButton.normalSprite = _firstRewardsButton.pressedSprite;
			_secondRewardsButton.normalSprite = _secondRewardsButton.disabledSprite;
			_thirdRewardsButton.normalSprite = _thirdRewardsButton.disabledSprite;
			_fourthRewardsButton.normalSprite = _fourthRewardsButton.disabledSprite;
			_fifthRewardsButton.normalSprite = _fifthRewardsButton.disabledSprite;
			break;
		case MissionViewTab.Second:
			_firstRewardsButton.normalSprite = _firstRewardsButton.disabledSprite;
			_secondRewardsButton.normalSprite = _secondRewardsButton.pressedSprite;
			_thirdRewardsButton.normalSprite = _thirdRewardsButton.disabledSprite;
			_fourthRewardsButton.normalSprite = _fourthRewardsButton.disabledSprite;
			_fifthRewardsButton.normalSprite = _fifthRewardsButton.disabledSprite;
			break;
		case MissionViewTab.Third:
			_firstRewardsButton.normalSprite = _firstRewardsButton.disabledSprite;
			_secondRewardsButton.normalSprite = _secondRewardsButton.disabledSprite;
			_thirdRewardsButton.normalSprite = _thirdRewardsButton.pressedSprite;
			_fourthRewardsButton.normalSprite = _fourthRewardsButton.disabledSprite;
			_fifthRewardsButton.normalSprite = _fifthRewardsButton.disabledSprite;
			break;
		case MissionViewTab.Fourth:
			_firstRewardsButton.normalSprite = _firstRewardsButton.disabledSprite;
			_secondRewardsButton.normalSprite = _secondRewardsButton.disabledSprite;
			_thirdRewardsButton.normalSprite = _thirdRewardsButton.disabledSprite;
			_fourthRewardsButton.normalSprite = _fourthRewardsButton.pressedSprite;
			_fifthRewardsButton.normalSprite = _fifthRewardsButton.disabledSprite;
			break;
		case MissionViewTab.AfterFifth:
			_firstRewardsButton.normalSprite = _firstRewardsButton.disabledSprite;
			_secondRewardsButton.normalSprite = _secondRewardsButton.disabledSprite;
			_thirdRewardsButton.normalSprite = _thirdRewardsButton.disabledSprite;
			_fourthRewardsButton.normalSprite = _fourthRewardsButton.disabledSprite;
			_fifthRewardsButton.normalSprite = _fifthRewardsButton.pressedSprite;
			break;
		}
	}
}
