package com.base.locationsharewhite.fcm

import android.annotation.SuppressLint
import com.applovin.sdk.AppLovinSdkUtils.runOnUiThread
import com.base.locationsharewhite.BuildConfig
import com.base.locationsharewhite.fcm.NotificationUiUtil.sendNotificationIfCan
import com.base.locationsharewhite.helper.EventUtils
import com.base.locationsharewhite.helper.EventUtils.event
import com.base.locationsharewhite.helper.MyApplication
import com.base.locationsharewhite.utils.ToastUtils.toast
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import org.json.JSONException
import org.json.JSONObject

@SuppressLint("MissingFirebaseInstanceTokenRefresh")
class MessagingService : FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)
//        val id = remoteMessage.messageId
//        val ext = JSONObject()
//        try {
//            ext.put("getMessageType", remoteMessage.messageType)
//            ext.put("getData", remoteMessage.data)
//            ext.put("getCollapseKey", remoteMessage.collapseKey)
//            ext.put("getFrom", remoteMessage.from)
//            ext.put("getPriority", remoteMessage.priority)
//            ext.put("getSenderId", remoteMessage.senderId)
//            ext.put("getSentTime", remoteMessage.sentTime)
//        } catch (e: JSONException) {
//            event("FCM_Received_Error", id, ext, false)
//            throw RuntimeException(e)
//        } finally {
//        }
        val data = JSONObject(remoteMessage.data.toString())
        event("FCM_Received", "MessagingService", data, false)

        sendNotificationIfCan(this, PopupConstObject.POPUP_WHERE_FCM, null)

//        if (BuildConfig.DEBUG) {
//            runOnUiThread {
//                runCatching {
//                    MyApplication.appContext.toast("MessagingService fcm")
//                }
//            }
//        }
    }
}