package com.secspace.sms.receivers

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.secspace.sms.extensions.conversationsDB
import com.secspace.sms.extensions.markThreadMessagesRead
import com.secspace.sms.helpers.MARK_AS_READ
import com.secspace.sms.helpers.THREAD_ID
import com.secspace.sms.helpers.refreshMessages
import com.simplemobiletools.commons.extensions.notificationManager
import com.simplemobiletools.commons.helpers.ensureBackgroundThread

class MarkAsReadReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        when (intent.action) {
            MARK_AS_READ -> {
                val threadId = intent.getLongExtra(THREAD_ID, 0L)
                context.notificationManager.cancel(threadId.hashCode())
                ensureBackgroundThread {
                    context.markThreadMessagesRead(threadId)
                    context.conversationsDB.markRead(threadId)
                    refreshMessages()
                }
            }
        }
    }
}
