package com.remax.visualnovel.utils.analytics

import android.content.Context
import android.os.Bundle
import com.remax.visualnovel.manager.login.LoginManager
import com.google.firebase.analytics.FirebaseAnalytics
import timber.log.Timber

object AnalyticsUtils {

    private lateinit var firebaseAnalytics: FirebaseAnalytics

    fun init(context: Context) {
        if (!this::firebaseAnalytics.isInitialized) {
            firebaseAnalytics = FirebaseAnalytics.getInstance(context)
        }
        refreshUserId()
    }

    fun refreshUserId() {
        Timber.d("firebase埋点更新userId:${LoginManager.user?.userId}")
        if (this::firebaseAnalytics.isInitialized) {
            firebaseAnalytics.setUserId(LoginManager.user?.userId?:"")
        }
    }

    /**
     * 添加埋点
     */
    fun logAnalytics(event: String, bundle: Bundle? = null) {
        Timber.d("埋点事件:$event")
        if (this::firebaseAnalytics.isInitialized) {
            Timber.d("firebase埋点 event:$event bundle:${bundle.toString()}")
            firebaseAnalytics.logEvent(event, bundle)
        }
    }

}