package  com.base.easyfilemanager.display

import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.text.TextUtils
import com.base.easyfilemanager.MyApplication
import com.base.easyfilemanager.helps.EventHelper
import com.base.easyfilemanager.helps.LogEx

class CloseNotificationReceiver : BroadcastReceiver() {
    private val TAG = "CloseNotificationReceiver"
    override fun onReceive(context: Context?, intent: Intent?) {
        LogEx.logDebug(TAG, "onReceive intent.action=${intent?.action}")
        //cancel事件,经过测试Intent.action这里无法传递参数
        if (intent?.action == null) {
            EventHelper.event("notification_cancel", "use cancel notification!")
        }
        intent?.let {
            EventHelper.event("notification_userCancel", "userCancel=true")
            val notificationId = it.getIntExtra("notificationId", -1)

            LogEx.logDebug(TAG, "notificationId=$notificationId action=$action")
            if (notificationId != -1 && it.action != "DELETE_NOTIFICATION") {
                MyApplication.isDeleteNotification = true
                val notificationManager =
                    context?.getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager
                notificationManager?.cancel(NotificationUtils.NOTIFICATION_TAG, notificationId)
            } else if (TextUtils.equals(it.action, "DELETE_NOTIFICATION")) {
                MyApplication.isDeleteNotification = true
            } else {

            }
        }
    }

    companion object {
        const val action = "com.file.notification.CloseNotificationReceiver"
    }
}
