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

namespace Guru
{
	[MonoSingleton(EMonoSingletonType.CreateOnNewGameObject, false)]
	public class FBService : MonoSingleton<FBService>
	{
		public static readonly string LOG_TAG = "FB";
		
		public void StartService()
		{
			if (!FB.IsInitialized)
			{
				// Initialize the Facebook SDK
				FB.Init(InitCallback, OnHideUnity);
			}
			else
			{
				// Already initialized, signal an app activation App Event
				FB.ActivateApp();
			}
		}

		private void InitCallback()
		{
			if (FB.IsInitialized)
			{
				// Signal an app activation App Event
				FB.ActivateApp();
				FB.Mobile.SetAdvertiserIDCollectionEnabled(true);
				FB.Mobile.SetAutoLogAppEventsEnabled(false); // 关闭自动打点上报
#if UNITY_IOS
				FB.Mobile.SetAdvertiserTrackingEnabled(true);
#endif
			}
			else
			{
				Log.E(LOG_TAG, "Failed to Initialize the Facebook SDK");
			}
		}

		private void OnHideUnity(bool isGameShown)
		{
			if (!isGameShown)
			{
				// Pause the game - we will need to hide
				Time.timeScale = 0;
			}
			else
			{
				// Resume the game - we're getting focus again
				Time.timeScale = 1;
			}
		}
	}
}