using UnityEngine;

namespace Wizard.Bingo;

public class BingoMissionDialog : MonoBehaviour
{
	[SerializeField]
	private GameObject _bingoMissionItemPrefab;

	[SerializeField]
	private UIGrid _grid;

	[SerializeField]
	private UIScrollView _scrollPanel;

	private BingoInfoTask.BingoInfoData _bingoInfoData;

	private ResourceHandler _resourceHandler;

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

	private void Initialize(BingoInfoTask.BingoInfoData bingoInfoData)
	{
		_bingoInfoData = bingoInfoData;
		UpdateMissionView();
	}

	private void UpdateMissionView()
	{
		_grid.transform.DestroyChildren();
		for (int i = 0; i < _bingoInfoData.MissionList.Count; i++)
		{
			bool flag = i + 1 == _bingoInfoData.MissionList.Count;
			AchievementWindowBase component = NGUITools.AddChild(_grid.gameObject, _bingoMissionItemPrefab).GetComponent<AchievementWindowBase>();
			component.gameObject.SetActive(value: true);
			component.SetBingoMission(_bingoInfoData.MissionList[i], !flag, _resourceHandler);
		}
		_grid.Reposition();
		_scrollPanel.ResetPosition();
	}
}
