package com.adealink.weparty.push

import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.coroutine.dispatcher.Dispatcher
import com.adealink.frame.data.json.froJsonErrorNull
import com.adealink.frame.log.data.NotifyLogType
import com.adealink.frame.log.data.ReportLogReason
import com.adealink.frame.push.data.PushMessage
import com.adealink.frame.util.AppUtil
import com.adealink.weparty.App
import com.adealink.weparty.R
import com.adealink.weparty.push.data.JiehePushMessage
import com.adealink.weparty.push.data.PushMessageType
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

object PushMessageManager {
    fun processPushMessage(pushMessage: PushMessage) {
        if (pushMessage.messageType == PushMessageType.UPLOAD_USER_LOG.type) {
            CoroutineScope(Dispatcher.WENEXT_THREAD_POOL).launch {
                App.instance.logService.uploadLog(
                    ReportLogReason.Notify(
                        NotifyLogType.PUSH,
                        froJsonErrorNull(pushMessage.pushContent)
                    )
                )
            }
            return
        }
        PushStatEvent(PushStatEvent.Action.RECEIVE).apply {
            type to "${pushMessage.messageType}"
            pushId to "${pushMessage.pushId}"
            content to "${pushMessage.pushContent}"
        }.send()
        if (!AppUtil.background) {
            PushStatEvent(PushStatEvent.Action.IGNORE).apply {
                type to "${pushMessage.messageType}"
                pushId to "${pushMessage.pushId}"
            }.send()
        } else {
            PushStatEvent(PushStatEvent.Action.SHOW).apply {
                type to "${pushMessage.messageType}"
                pushId to "${pushMessage.pushId}"
            }.send()
            NotificationUtil.showNotification(JiehePushMessage(pushMessage, getCompatString(R.string.channel_event)))
        }
    }
}