using System;
using System.Collections;
using UnityEngine;
using Wizard.ErrorDialog;
using Wizard.RoomMatch;

namespace Wizard;

public class GatheringChatSendRoomMatchUI : MonoBehaviour, IChatActionUI
{
	[SerializeField]
	private UIButton _buttonRoomMatch;

	private ChatConnectController _chatConnectCtr;

	public void Init(IChatSettings chatSettings, ChatConnectController chatConnectCtr, ChatLogUI chatLogUI, Action actionAddNewChatLogAfterSendChat)
	{
		_chatConnectCtr = chatConnectCtr;
		_buttonRoomMatch.onClick.Clear();
		_buttonRoomMatch.onClick.Add(new EventDelegate(delegate
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
			GatheringInfo gatheringInfo = (chatSettings as GatheringChatSettings).GatheringInfo;
			if (GatheringChat.IsMaintenance(Data.MaintenanceCodeList, gatheringInfo.Rule))
			{
				Wizard.ErrorDialog.Dialog.Create(2030);
			}
			else
			{
				RoomConnectController.InitializeParameter param = new RoomConnectController.InitializeParameter(RoomConnectController.PositionMode.OWNER, gatheringInfo.Rule.BattleParameterInstance, "")
				{
					IsGathering = true
				};
				UIManager.GetInstance().StartCoroutine(CreateRoom(param));
			}
		}));
	}

	private IEnumerator CreateRoom(RoomConnectController.InitializeParameter param)
	{
		UIManager.GetInstance().createInSceneCenterLoading(notBlack: true);
		RoomConnectController room = new RoomConnectController(param);
		yield return UIManager.GetInstance().StartCoroutine(room.StartConnect());
		if (room.ConnectRoomResultType == RoomConnectController.ConnectRoomResult.SUCCESS)
		{
			_chatConnectCtr.StopPolling();
			GameMgr.GetIns().GetDataMgr().m_BattleType = DataMgr.BattleType.RoomBattle;
			UIManager.GetInstance()._Footer.InviteIconDisp(inDisp: false);
			UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.Room);
		}
		UIManager.GetInstance().closeInSceneCenterLoading();
	}

	public void StartCreateRoom()
	{
		UIButton.current = _buttonRoomMatch;
		EventDelegate.Execute(_buttonRoomMatch.onClick);
		UIButton.current = null;
	}
}
