using System;
using System.Collections;
using System.Collections.Generic;
using Facebook.Unity;
using UnityEngine;

namespace Cute;

public class SocialServiceUtility : MonoBehaviour
{
	private bool isLogin_;

	private bool isCountTime;

	private float timer;

	public bool IsRunning { get; protected set; }

	public bool IsLoggedIn
	{
		get
		{
			return isLogin_;
		}
		protected set
		{
			isLogin_ = value;
		}
	}

	public string SocialServiceUserId { get; private set; }

	public string FirebaseIdToken { get; private set; }

	public string FaceBookAccountId { get; private set; }

	public string FaceBookAuthenticationToken { get; private set; }

	public bool FaceBookIsLoggedIn => FB.IsLoggedIn;

	public static SocialServiceUtility Instance { get; private set; }

	public static SocialServiceUtility CreateInstance()
	{
		if (null == Instance)
		{
			Instance = new GameObject(typeof(SocialServiceUtility).Name).AddComponent<SocialServiceUtility>();
			UnityEngine.Object.DontDestroyOnLoad(Instance);
		}
		return Instance;
	}

	private void Update()
	{
		if (isCountTime)
		{
			checkTimeOut();
		}
	}

	private void Awake()
	{
	}

	private void AndroidInit()
	{
	}

	private void FbInit()
	{
	}

	public void FbSignIn(string nonce, Action<bool> onetimeCallback)
	{
		if (IsRunning)
		{
			return;
		}
		if (FaceBookIsLoggedIn && FaceBookAccountId != null)
		{
			if (onetimeCallback != null)
			{
				onetimeCallback(FaceBookIsLoggedIn);
			}
			return;
		}
		IsRunning = true;
		INetworkUI networkUI = Toolbox.NetworkManager.NetworkUI;
		networkUI.StartLoading();
		FB.LogInWithReadPermissions(new List<string> { "public_profile" }, delegate(ILoginResult result)
		{
			IsRunning = false;
			networkUI.StopLoading();
			if (result == null)
			{
				onetimeCallback(obj: false);
			}
			else if (!string.IsNullOrEmpty(result.Error))
			{
				onetimeCallback(obj: false);
			}
			else if (result.Cancelled)
			{
				onetimeCallback(obj: false);
			}
			else if (!string.IsNullOrEmpty(result.RawResult))
			{
				FaceBookAccountId = AccessToken.CurrentAccessToken.TokenString;
				FaceBookAuthenticationToken = "";
				onetimeCallback(obj: true);
			}
		});
	}

	public IEnumerator SignIn(Action<bool> onetimeCallback)
	{
		if (IsRunning)
		{
			yield break;
		}
		if (IsLoggedIn && SocialServiceUserId != null)
		{
			onetimeCallback?.Invoke(IsLoggedIn);
			yield break;
		}
		IsRunning = true;
		INetworkUI networkUI = Toolbox.NetworkManager.NetworkUI;
		networkUI.StartLoading();
		StartTimeCount();
		UnitySocialPlatformSingIn(networkUI);
		while (IsRunning)
		{
			yield return null;
		}
		onetimeCallback(IsLoggedIn);
	}

	public void UnitySocialPlatformSingIn(INetworkUI networkUI)
	{
		Social.localUser.Authenticate(delegate(bool result)
		{
			if (IsRunning && !result)
			{
				IsLoggedIn = false;
				IsRunning = false;
				StopTimeCount();
				networkUI.StopLoading();
			}
		});
	}

	private void getFirebaseIdToken(INetworkUI networkUI)
	{
	}

	public void FbSignOut()
	{
		if (FaceBookIsLoggedIn)
		{
			FB.LogOut();
			FaceBookAccountId = null;
		}
	}

	public void SignOut(Action onetimeCallback = null)
	{
		if (IsLoggedIn && !IsRunning)
		{
			IsRunning = true;
			IsRunning = false;
			IsLoggedIn = false;
			onetimeCallback?.Invoke();
		}
	}

	public static string GetSocialServiceName()
	{
		return "";
	}

	public static int GetSocialServiceType()
	{
		return 0;
	}

	public void StartTimeCount()
	{
		isCountTime = true;
	}

	public void StopTimeCount()
	{
		isCountTime = false;
		timer = 0f;
	}

	private void checkTimeOut()
	{
		timer += Time.deltaTime;
		if (timer >= 30f)
		{
			timer = 0f;
			INetworkUI networkUI = Toolbox.NetworkManager.NetworkUI;
			networkUI.StopLoading();
			StopTimeCount();
			networkUI.OpenSocialServiceNoResponseErrorPopup();
			IsRunning = false;
		}
	}
}
