#if FIREBASE_ANALYTICS
using System.Threading;
using Cysharp.Threading.Tasks;
using Firebase.Crashlytics;
using VContainer.Unity;

namespace Darkmatter.Services.Analytics
{
    public class FirebaseAnalyticsSystem : IAsyncStartable
    {
        public async UniTask StartAsync(CancellationToken cancellation = new CancellationToken())
        {
#if !UNITY_EDITOR
            await Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
            {
                var dependencyStatus = task.Result;
                if (dependencyStatus == Firebase.DependencyStatus.Available)
                {
                    // Create and hold a reference to your FirebaseApp,
                    // where app is a Firebase.FirebaseApp property of your application class.
                    // Crashlytics will use the DefaultInstance, as well;
                    // this ensures that Crashlytics is initialized.
                    Firebase.FirebaseApp app = Firebase.FirebaseApp.DefaultInstance;
#if DEVELOPMENT_BUILD
                    Firebase.Crashlytics.Crashlytics.SetCustomKey("environment", "dev");
#else
                    Firebase.Crashlytics.Crashlytics.SetCustomKey("environment", "prod");
#endif
                    // When this property is set to true, Crashlytics will report all
                    // uncaught exceptions as fatal events. This is the recommended behavior.
                    Crashlytics.ReportUncaughtExceptionsAsFatal = true;

                    // Set a flag here for indicating that your project is ready to use Firebase.
                }
                else
                {
                    UnityEngine.Debug.LogError(System.String.Format(
                        "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
                    // Firebase Unity SDK is not safe to use here.
                }
            }, cancellation);
#endif
        }
    }
}
#endif