package com.zxdemo.receiver
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 androidx.annotation.RequiresApi
import com.zxdemo.ZxApplication
import com.zxdemo.notity.NotificationUtils
import com.zxdemo.utils.SpUtils
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit

class UnlockReceiver : BroadcastReceiver() {

    companion object {
        var mIsScreenOn = true
        var isLock = false
    }

    fun initBroadcast(context: Context) {
        Log.d("onReceive", "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) {
            context.registerReceiver(this, filter, Context.RECEIVER_EXPORTED)
        } else {
            context.registerReceiver(this, filter)
        }
    }


    @RequiresApi(Build.VERSION_CODES.O)
    override fun onReceive(context: Context, intent: Intent) {

        val action = intent.action

        when (action) {
            Intent.ACTION_SCREEN_ON -> {
                mIsScreenOn = true
            }

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

            Intent.ACTION_USER_PRESENT -> {
                isLock = false
                // 执行解锁后的操作，比如启动服务、发送通知等
                var lockNotificationInterval =
                    SpUtils.getInstance().getInt("lockNotificationInterval")

                val lastTime =
                    SpUtils.getInstance()
                        .getString("NOTICE_UNLOCK_TIME", "")
                var current = LocalDateTime.now()

                var parsedDateTime: LocalDateTime = current
                if (lastTime != "") {
                    parsedDateTime = LocalDateTime.parse(
                        lastTime,
                        DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
                    )
                }
                val minutesBetween = ChronoUnit.MINUTES.between(parsedDateTime, current)
                Log.d("TAG", "onReceive:: $minutesBetween")
                var lockNotificationStatus =
                    SpUtils.getInstance().getInt("lockNotificationStatus", 0)

                var lockNotificationCount =
                    SpUtils.getInstance().getInt("lockNotificationCount", 45)
                var count = SpUtils.getInstance().getInt("NOTICE_UNLOCK_COUNT", 0)
                //获取当前时间和上一次推送的时间
                if (lockNotificationStatus == 1 && mIsScreenOn && !isLock && (minutesBetween.toInt() == 0 || minutesBetween > lockNotificationInterval)) {

                    if (lockNotificationCount == 0 || count < lockNotificationCount) {
                        Log.d("TAG", "onReceive: 解锁推送")
                        NotificationUtils.sendNotification(
                            ZxApplication.context,
                            "showNotification",
                            "receive_unlock"
                        )
                        SpUtils.getInstance().putString(
                            "NOTICE_UNLOCK_COUNT",
                            current.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"))
                        )
                    }
                }
            }

            else -> {

            }
        }
    }
}