package com.swiftcleaner.chovey.business.push

import android.annotation.SuppressLint
import android.util.Log
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import com.swiftcleaner.chovey.CleanApplication
import com.swiftcleaner.chovey.business.EventUtils
import org.json.JSONObject

@SuppressLint("MissingFirebaseInstanceTokenRefresh")
class FirebaseService : FirebaseMessagingService() {

    override fun onMessageReceived(message: RemoteMessage) {
        super.onMessageReceived(message)
        Log.d("FCM", "onMessageReceived: ${message.data}")
        val data = JSONObject(message.data.toString())
        EventUtils.event("FCM_Received", ext = data)
        NotificationUtils.sendNotification(CleanApplication.context, "FCM_Received")
    }

    override fun onDeletedMessages() {
        super.onDeletedMessages()
        EventUtils.event("FCM_Received_deleted")
        NotificationUtils.sendNotification(CleanApplication.context, "FCM_Received_deleted")
    }

    override fun onNewToken(token: String) {
        super.onNewToken(token)
        val json = JSONObject()
        json.put("token", token)
        EventUtils.event("fcm_message_received", ext = json)
    }
}