package com.dumpster.cleaner.business.push.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import com.dumpster.cleaner.bean.push.NotificationSendBean
import com.dumpster.cleaner.bean.push.NotificationSendBean.Companion.POPUP_WHERE_LOCK
import com.dumpster.cleaner.bean.config.PopupConfigBean.Companion.popupConfigBean
import com.dumpster.cleaner.business.push.notification.MyNotificationManager
import java.util.Objects

class ScreenStatusReceiver : BroadcastReceiver() {


    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action
        when (Objects.requireNonNull<String?>(action)) {
            Intent.ACTION_SCREEN_ON -> isDeviceInteractive = true
            Intent.ACTION_SCREEN_OFF -> {
                isDeviceInteractive = false
                isSecureLockActive = true
            }

            Intent.ACTION_USER_PRESENT -> {
                isSecureLockActive = false
                if (isDeviceInteractive && !isSecureLockActive) {
                    if (popupConfigBean.screenS) {
                        MyNotificationManager.submitSendBean(NotificationSendBean(context, POPUP_WHERE_LOCK))
                    }
                }
            }
        }
    }


    companion object {
        private val TAG = "ScreenStatusReceiver"

        var isDeviceInteractive: Boolean = true
        var isSecureLockActive: Boolean = false

        fun registerScreenStatusReceiver(context: Context) {
            val intentFilter = IntentFilter()
            intentFilter.addAction(Intent.ACTION_SCREEN_OFF)
            intentFilter.addAction(Intent.ACTION_SCREEN_ON)
            intentFilter.addAction(Intent.ACTION_USER_PRESENT)

            val applicationContext = context.applicationContext
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                applicationContext.registerReceiver(ScreenStatusReceiver(), intentFilter, Context.RECEIVER_EXPORTED)
            } else {
                applicationContext.registerReceiver(ScreenStatusReceiver(), intentFilter)
            }
        }

    }
}