using System;
using System.Linq;
using Cute;
using UnityEngine;
using Wizard.ErrorDialog;

namespace Wizard;

public class ChatConnectController : MonoBehaviour
{
	public const int DEFAULT_POLLING_TIME = 3;

	private const int INVALID_POLLING_TIME = 0;

	private static readonly int[] ERROR_CODE_NOT_STOP_POLLING = new int[1] { 207 };

	private bool _isPausePolling;

	private bool _isConnectPolling;

	private float _pollingIntervalCount;

	private bool _isActiveCenterLoading;

	private static bool _isOpenChat = false;

	private Action _eventOnPolling;

	private Action _onConnectResultError;

	public int PollingInterval { get; private set; } = 3;

	public void Init(Action eventOnPolling, Action onConnectResultError)
	{
		_isOpenChat = true;
		_eventOnPolling = eventOnPolling;
		_onConnectResultError = onConnectResultError;
		UpdatePollingInterval(3);
		ResetPolling();
	}

	public void Close()
	{
		if (_isConnectPolling)
		{
			SetActiveCenterLoading(isActive: true, force: false);
		}
		StopPolling();
		_isOpenChat = false;
	}

	public void StopPolling()
	{
		PollingInterval = 0;
		_pollingIntervalCount = 0f;
	}

	public void ResetPolling()
	{
		ResumePolling();
		_pollingIntervalCount = 0f;
	}

	public void PausePolling()
	{
		_isPausePolling = true;
	}

	public void ResumePolling()
	{
		_isPausePolling = false;
	}

	public void UpdatePollingInterval(int intervalTime)
	{
		PollingInterval = intervalTime;
	}

	private void OnDestroy()
	{
		_isOpenChat = false;
	}

	private void Update()
	{
		UpdatePolling();
	}

	private void UpdatePolling()
	{
		if (PollingInterval == 0 || _isPausePolling)
		{
			return;
		}
		_pollingIntervalCount += Time.deltaTime;
		if ((float)PollingInterval <= _pollingIntervalCount)
		{
			if (Toolbox.NetworkManager.isConnect || NetworkUI.GetInstance().IsOpenErrorDialog())
			{
				ResetPolling();
			}
			else
			{
				_eventOnPolling.Call();
			}
		}
	}

	public void StartConnectCommon(NetworkTask task, Action<NetworkTask.ResultCode> callbackOnSuccess, bool isPolling = false)
	{
		Action<NetworkTask.ResultCode> action = null;
		Action<int> action2 = null;
		if (isPolling)
		{
			task.SkipCuteTimeOutPopup();
			task.SkipCuteHttpStatusErrorPopup();
			task.SkipAllCuteResultCodeCheckErrorPopup();
			_isConnectPolling = true;
			action = OnConnectFailureErrorPolling;
			action2 = OnConnectResultErrorPolling;
		}
		else
		{
			SetActiveCenterLoading(isActive: true);
			action = OnConnectFailureError;
			action2 = OnConnectResultError;
		}
		UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate(NetworkTask.ResultCode code)
		{
			if (isPolling)
			{
				_isConnectPolling = false;
			}
			else
			{
				SetActiveCenterLoading(isActive: false);
			}
			if (_isOpenChat)
			{
				callbackOnSuccess.Call(code);
			}
		}, action, action2, encrypt: true, useJson: false, !isPolling));
	}

	private void OnConnectFailureErrorPolling(NetworkTask.ResultCode code)
	{
		_isConnectPolling = false;
		ResetPolling();
	}

	private void OnConnectResultErrorPolling(int error)
	{
		if (_isOpenChat)
		{
			Wizard.ErrorDialog.Dialog.Create(error);
		}
		_isConnectPolling = false;
		StopPolling();
		_onConnectResultError.Call();
	}

	private void OnConnectFailureError(NetworkTask.ResultCode code)
	{
		SetActiveCenterLoading(isActive: false);
	}

	private void OnConnectResultError(int error)
	{
		SetActiveCenterLoading(isActive: false);
		if (!ERROR_CODE_NOT_STOP_POLLING.Contains(error))
		{
			StopPolling();
		}
		_onConnectResultError.Call();
	}

	private void SetActiveCenterLoading(bool isActive, bool force = true)
	{
		if (_isActiveCenterLoading != isActive)
		{
			_isActiveCenterLoading = isActive;
			if (isActive)
			{
				UIManager.GetInstance().createInSceneCenterLoading(notBlack: false, notCollider: false, force);
			}
			else
			{
				UIManager.GetInstance().closeInSceneCenterLoading(force);
			}
		}
	}
}
