package com.test.basd.smartjunkcleaner.notificationclean

import android.app.Notification
import android.service.notification.NotificationListenerService
import android.service.notification.StatusBarNotification
import android.util.Log
import com.test.basd.smartjunkcleaner.helps.BaseApplication
import com.test.basd.smartjunkcleaner.helps.ConfigHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch


class AppNotificationListenerService : NotificationListenerService() {

    override fun onCreate() {
        super.onCreate()
    }

    override fun onListenerConnected() {
        super.onListenerConnected()
         Log.e("MXL", "onListenerConnected: ")
        ConfigHelper.isOpenNotification = true
    }

    override fun onNotificationPosted(sbn: StatusBarNotification) {
        // 当通知被发布时调用
        // 可以在这里获取通知的详细信息
        val notification = sbn.notification
        // 获取通知的标题和内容等
        val extras = notification.extras
        val title = extras.getCharSequence(Notification.EXTRA_TITLE)
        val text = extras.getCharSequence(Notification.EXTRA_TEXT)
        // 处理通知信息...
            // Log.e("MXL", "onNotificationPosted: $extras")
//        val data = NotificationData()
//        data.packageName = sbn.packageName;
//        data.notificationTitle = title.toString()
//        data.notificationText = text.toString()
//        db?.notificationDao()?.insert(data)
        MainScope().launch(Dispatchers.IO) {
            if (title?.isNotBlank() == true) {
                HistoryDatabase.getInstance(BaseApplication.context)?.historyDao()?.insert(
                    NotificationData(
                        sbn.packageName,
                        title.toString(),
                        text.toString(),
                        System.currentTimeMillis()
                    )
                )
            }

        }

        //dbHelper.addContacts(NotifBean(null,null,"${title}","${text}",null,null))

    }

    override fun onNotificationRemoved(sbn: StatusBarNotification) {
        // 当通知被移除时调用
    }


}