using System;
using System.Collections.Generic;
using ShiroginSDK.Runtime.Core.SDK;
using ShiroginSDK.Runtime.Services.Base;
using ShiroginSDK.Runtime.Services.Interfaces;
using UnityEngine;
#if SHIROGIN_FACEBOOK
using Facebook.Unity;
#endif
namespace ShiroginSDK.Runtime.Services.Implementations.Facebook
{
#if SHIROGIN_FACEBOOK
///
/// Handles Facebook SDK initialization, login, and event tracking.
/// Now managed as a ServiceBase implementation for modular SDK integration.
///
public class FacebookService : ServiceBase, IFacebookService
{
ShiroginConfig _config;
private bool _isInitialized;
// ----------------------------------------------------
// Initialization
// ----------------------------------------------------
public void Initialize()
{
_config = ShiroginConfig.Load();
if (!_config.enableFacebook)
{
Debug.LogWarning("[FacebookService] 🛑 Facebook DISABLED in Config. Running in Dummy Mode.");
return;
}
if (_isInitialized)
return;
if (!FB.IsInitialized)
{
FB.Init(OnInitComplete, OnHideUnity);
}
else
{
FB.ActivateApp();
_isInitialized = true;
}
}
// ----------------------------------------------------
// Login
// ----------------------------------------------------
public void Login(Action callback = null)
{
if (_config == null || !_config.enableFacebook)
{
Debug.Log("[FacebookService] (Dummy) Login called but disabled.");
callback?.Invoke(false, null);
return;
}
if (!_isInitialized)
{
Debug.LogWarning("[FacebookService] ⚠️ Login attempted before initialization.");
callback?.Invoke(false, null);
return;
}
var permissions = new List { "public_profile", "email" };
FB.LogInWithReadPermissions(permissions, result =>
{
if (FB.IsLoggedIn)
{
var userId = AccessToken.CurrentAccessToken.UserId;
Debug.Log($"[FacebookService] ✅ Login successful: {userId}");
callback?.Invoke(true, userId);
}
else
{
Debug.LogWarning("[FacebookService] ❌ Login failed.");
callback?.Invoke(false, null);
}
});
}
// ----------------------------------------------------
// Analytics
// ----------------------------------------------------
public void LogPurchase(float amount, string currency)
{
if (!_isInitialized)
{
Debug.LogWarning("[FacebookService] ⚠️ LogPurchase called before SDK init.");
return;
}
FB.LogPurchase(amount, currency);
Debug.Log($"[FacebookService] 💰 Purchase logged: {amount} {currency}");
}
public void LogEvent(string eventName, float? value = null, Dictionary parameters = null)
{
if (_config == null || !_config.enableFacebook) return;
if (!_isInitialized)
{
Debug.LogWarning("[FacebookService] ⚠️ LogEvent called before SDK init.");
return;
}
if (value.HasValue)
FB.LogAppEvent(eventName, value.Value, parameters);
else
FB.LogAppEvent(eventName, null, parameters);
Debug.Log($"[FacebookService] 📊 Event logged: {eventName}");
}
// ----------------------------------------------------
// Lifecycle
// ----------------------------------------------------
protected override void OnInitialize()
{
Initialize();
}
protected override void OnDispose()
{
// Nothing to dispose for now
}
private void OnInitComplete()
{
_isInitialized = true;
FB.ActivateApp();
Debug.Log("[FacebookService] ✅ SDK Initialized.");
}
private void OnHideUnity(bool isGameShown)
{
Time.timeScale = isGameShown ? 1 : 0;
}
}
#else
///
/// Handles Facebook SDK initialization, login, and event tracking.
/// Now managed as a ServiceBase implementation for modular SDK integration.
///
public class FacebookService : ServiceBase, IFacebookService
{
ShiroginConfig _config;
private bool _isInitialized;
// ----------------------------------------------------
// Initialization
// ----------------------------------------------------
public void Initialize()
{
_config = ShiroginConfig.Load();
if (!_config.enableFacebook)
{
Debug.LogWarning("[FacebookService] 🛑 Facebook DISABLED in Config. Running in Dummy Mode.");
}
}
// ----------------------------------------------------
// Login
// ----------------------------------------------------
public void Login(Action callback = null)
{
if (_config == null || !_config.enableFacebook)
{
Debug.Log("[FacebookService] (Dummy) Login called but disabled.");
callback?.Invoke(false, null);
return;
}
}
// ----------------------------------------------------
// Analytics
// ----------------------------------------------------
public void LogPurchase(float amount, string currency)
{
Debug.Log($"[FacebookService] Dummy💰 Purchase logged: {amount} {currency}");
}
public void LogEvent(string eventName, float? value = null, Dictionary parameters = null)
{
Debug.Log($"[FacebookService] Dummy Event logged: {eventName}");
}
// ----------------------------------------------------
// Lifecycle
// ----------------------------------------------------
protected override void OnInitialize()
{
Initialize();
}
protected override void OnDispose()
{
// Nothing to dispose for now
}
private void OnInitComplete()
{
_isInitialized = true;
Debug.Log("[FacebookService] Dummy✅ SDK Initialized.");
}
private void OnHideUnity(bool isGameShown)
{
Time.timeScale = isGameShown ? 1 : 0;
}
}
#endif
}