package com.test.basd.onecleanmaster.display.fcm

import android.util.Log
import com.google.android.gms.tasks.OnCompleteListener
import com.google.android.gms.tasks.Task
import com.google.firebase.messaging.FirebaseMessaging
import com.test.basd.onecleanmaster.helps.EventHelper


object FcmHelper {

    private const val TAG = "FcmHelper"

    fun subscribeToTopic() {
        FirebaseMessaging.getInstance().subscribeToTopic("news")
            .addOnCompleteListener { task: Task<Void?> ->
                if (task.isSuccessful) {
                    EventHelper.event("fcm_subscribed_suc", "news")
                    Log.d(TAG, "Subscribed to topic: TOPIC_NAME")
                } else {
                    EventHelper.event("fcm_subscribed_fail")
                    Log.e(TAG, "Failed to subscribe to topic: TOPIC_NAME", task.exception)
                }
            }
    }

    fun getToken() {
        FirebaseMessaging.getInstance().token
            .addOnCompleteListener(OnCompleteListener { task ->
                if (!task.isSuccessful) {
                    EventHelper.event("fcm_token_fail")
                    Log.w(TAG, "Fetching FCM registration token failed", task.exception)
                    return@OnCompleteListener
                }
                // Get new FCM registration token
                val token = task.result
                EventHelper.event("fcm_token_suc", token)
                Log.d(TAG, "token: $token")
            })
    }
}