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

namespace Wizard;

public class GatheringEntryConfirm : MonoBehaviour
{
	[SerializeField]
	private GatheringRuleView _ruleView;

	[SerializeField]
	private UserPlateBase _ownerPlate;

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

	private GatheringInfo _info;

	private void Start()
	{
	}

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

	public void StartConfirm(GatheringGetInfoTask task)
	{
		base.gameObject.SetActive(value: false);
		_info = task.Info;
		GatheringUserInfo ownerInfo = _info.OwnerInfo;
		_loadedResourceList.AddRange(ownerInfo.GetUserAssetPathList());
		UIManager.GetInstance().StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(_loadedResourceList, delegate
		{
			base.gameObject.SetActive(value: true);
			DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
			_ruleView.SetGatheringInfo(task.Info);
			dialogBase.SetObj(base.gameObject);
			dialogBase.SetSize(DialogBase.Size.M);
			dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_CancelBtn);
			dialogBase.SetButtonText(Data.SystemText.Get("Gathering_Join_0003"));
			dialogBase.SetTitleLabel(Data.SystemText.Get("Gathering_Menu_0002"));
			dialogBase.onPushButton1 = delegate
			{
				OnClickEntryButton();
			};
			_ownerPlate.InitializeSimplePlate(ownerInfo);
		}));
	}

	private void OnClickEntryButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		if (_info.Rule.IsEntryDeckOnly)
		{
			DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
			dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_CancelBtn);
			dialogBase.SetTitleLabel(Data.SystemText.Get("Gathering_0025"));
			dialogBase.SetText(Data.SystemText.Get("Gathering_Join_0007"));
			dialogBase.SetButtonText(Data.SystemText.Get("Gathering_0022"));
			dialogBase.onPushButton1 = delegate
			{
				ShowDeckListDialog(_info);
			};
		}
		else
		{
			GatheringEntryTask gatheringEntryTask = new GatheringEntryTask();
			gatheringEntryTask.SetParameter(_info.Id, null);
			UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(gatheringEntryTask, delegate
			{
				OnSuccessEntry();
			}));
		}
	}

	public static void ShowDeckListDialog(GatheringInfo info)
	{
		string deckListHeader = Data.SystemText.Get("Gathering_0026");
		MultiDeckSelectDialog.Create(RoomConnectController.RuleSelectDeckCount(info.Rule.BattleParameterInstance.Rule), info.Rule.BattleParameterInstance.DeckFormat, deckListHeader).OnDecide = delegate(List<DeckData> deckList)
		{
			EntryGathering(deckList, info);
		};
	}

	public static void EntryGathering(List<DeckData> deckList, GatheringInfo info)
	{
		GatheringEntryTask gatheringEntryTask = new GatheringEntryTask();
		gatheringEntryTask.SetParameter(info.Id, deckList);
		UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(gatheringEntryTask, delegate
		{
			OnSuccessEntry();
		}));
	}

	public static void OnSuccessEntry()
	{
		GatheringChat.ResetLatestReadChatMessageId();
		UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.Gathering);
	}
}
