package importantedto.growedto.umbrellaedto

import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import importantedto.gallonedto.Tumbleedto
import importantedto.gallonedto.Waitedto

class Busyedto : BroadcastReceiver() {

    private val TAG = "CloseNotificationReceiver"

    companion object {
        var Action = "CANCEL_NOTIFICATION"
        var NotificationId = "NOTIFICATION_ID"
    }

    override fun onReceive(context: Context?, intent: Intent?) {
        Tumbleedto.logDebug(TAG, "onReceive")
        val action = intent?.action ?: ""
        Appleedto.stopNotificationHandler()
        if (action == Action) {
            var notificationId = intent?.extras?.getInt(NotificationId, 0) ?: 0
            Tumbleedto.logDebug(TAG, "notificationId=$notificationId")
            val notificationManager = context!!.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            val actionS = Waitedto.getInstance().getString("actionS", "0") ?: "0"
            runCatching {
                if (actionS.toInt() == 0) {
                    notificationId = 0
                }
            }
            notificationManager.cancel(notificationId)
        }
    }
}