using System;
using UnityEngine;

namespace Wizard;

public class GatheringChatInterruptOrLeave : MonoBehaviour, IChatActionUI
{
	[SerializeField]
	private UIButton _button;

	[SerializeField]
	private UILabel _label;

	private bool _isOwner;

	private ChatConnectController _chatConnectController;

	private const string BUTTON_SPRITE_NAME_DEFAULT_OFF = "btn_common_01_s_off";

	private const string BUTTON_SPRITE_NAME_DEFAULT_ON = "btn_common_01_s_on";

	private const string BUTTON_SPRITE_NAME_RED_OFF = "btn_common_04_s_off";

	private const string BUTTON_SPRITE_NAME_RED_ON = "btn_common_04_s_on";

	public void Init(IChatSettings chatSettings, ChatConnectController chatConnectController, ChatLogUI chatLogUI, Action actionAddNewChatLogAfterSendChat)
	{
		_chatConnectController = chatConnectController;
		GatheringChatSettings gatheringChatSettings = chatSettings as GatheringChatSettings;
		_isOwner = gatheringChatSettings.GatheringInfo.Role == GatheringRule.eRole.OWNER;
		_button.onClick.Clear();
		if (_isOwner)
		{
			_label.text = Data.SystemText.Get("Gathering_Chat_0011");
			_button.onClick.Add(new EventDelegate(OnClickInterrupt));
			_button.normalSprite = "btn_common_04_s_off";
			_button.pressedSprite = "btn_common_04_s_on";
		}
		else
		{
			_label.text = Data.SystemText.Get("Gathering_Chat_0015");
			_button.onClick.Add(new EventDelegate(OnClickLeave));
			_button.normalSprite = "btn_common_01_s_off";
			_button.pressedSprite = "btn_common_01_s_on";
		}
	}

	private void OnClickInterrupt()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
		PauseChatPolling();
		SystemText text = Data.SystemText;
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetTitleLabel(text.Get("Gathering_Chat_0012"));
		dialogBase.SetText(text.Get("Gathering_Chat_0013"));
		dialogBase.SetButtonText(text.Get("Gathering_Chat_0014"), text.Get("Common_0005"));
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.RedBtn_CancelBtn);
		dialogBase.SetSize(DialogBase.Size.S);
		dialogBase.onPushButton1 = delegate
		{
			GatheringInterruptTask task = new GatheringInterruptTask();
			_chatConnectController.StartConnectCommon(task, delegate
			{
				StopChatPolling();
				ChangeViewWithDialog(text.Get("Gathering_Chat_0019"));
			});
		};
		dialogBase.onPushButton2 = ResumeChatPolling;
		dialogBase.onCloseWithoutSelect = ResumeChatPolling;
	}

	private void OnClickLeave()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
		PauseChatPolling();
		SystemText text = Data.SystemText;
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetTitleLabel(text.Get("Gathering_Chat_0016"));
		dialogBase.SetText(text.Get("Gathering_Chat_0017"));
		dialogBase.SetButtonText(text.Get("Gathering_Chat_0018"), text.Get("Common_0005"));
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_CancelBtn);
		dialogBase.SetSize(DialogBase.Size.S);
		dialogBase.onPushButton1 = delegate
		{
			GatheringLeaveTask task = new GatheringLeaveTask();
			_chatConnectController.StartConnectCommon(task, delegate
			{
				StopChatPolling();
				ChangeViewWithDialog(text.Get("Gathering_Chat_0020"));
			});
		};
		dialogBase.onPushButton2 = ResumeChatPolling;
		dialogBase.onCloseWithoutSelect = ResumeChatPolling;
	}

	private void ChangeViewWithDialog(string text)
	{
		UIManager.GetInstance().CreateConfirmationDialog(text).OnCloseStart = Gathering.BackToMyPageForDrop;
	}

	private void PauseChatPolling()
	{
		_chatConnectController.PausePolling();
	}

	private void ResumeChatPolling()
	{
		_chatConnectController.ResumePolling();
	}

	private void StopChatPolling()
	{
		_chatConnectController.StopPolling();
	}
}
