package com.base.appzxhy.push.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import com.base.appzxhy.bean.push.NotificationSendBean
import com.base.appzxhy.bean.push.NotificationSendBean.Companion.POPUP_WHERE_LOCK
import com.base.appzxhy.bean.config.PopupConfigBean.Companion.popupConfigBean
import com.base.appzxhy.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)
            }
        }

    }
}