package com.base.easyfilemanager.display

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.util.Log
import com.base.easyfilemanager.utils.SPUtils


class ActionBroadcast : BroadcastReceiver() {

    private var mIsScreenOn = false
    private var isLock = false

    companion object {
        private val TAG = "ActionBroadcast"
        fun Context.initBroadcast() {
            val filter = IntentFilter()
            filter.addAction(Intent.ACTION_SCREEN_OFF)
            filter.addAction(Intent.ACTION_SCREEN_ON)
            filter.addAction(Intent.ACTION_USER_PRESENT)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                registerReceiver(ActionBroadcast(), filter, Context.RECEIVER_EXPORTED)
            } else {
                registerReceiver(ActionBroadcast(), filter)
            }
        }
    }


    override fun onReceive(context: Context, intent: Intent?) {
        val action = intent?.action ?: 0
        when (action) {
            Intent.ACTION_SCREEN_ON -> {
                mIsScreenOn = true
            }

            Intent.ACTION_SCREEN_OFF -> {
                mIsScreenOn = false
                isLock = true
            }

            Intent.ACTION_USER_PRESENT -> {
                isLock = false
                Log.d("glc", "解锁")
                if (mIsScreenOn && !isLock) {
                    // 展示主动推送
                    val locks = SPUtils.getInstance().getInt("notification_lockS", 0)
                    if (locks == 1) {
                        NotificationUtils.sendTimerPush()
                    }
                }
            }
        }
    }

}