package com.swiftcleaner.chovey.business.push

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.swiftcleaner.chovey.CleanApplication
import com.swiftcleaner.chovey.util.SPUtils
import com.swiftcleaner.chovey.util.Utils.toDate

class UnlockReceiver : BroadcastReceiver() {

    companion object {
        private const val LOCK_NOTIFICATION_STATUS_KEY = "lockNotificationStatus"
        private const val LOCK_NOTIFICATION_INTERVAL_KEY = "lockNotificationInterval"
        private const val LOCK_NOTIFICATION_COUNT_KEY = "lockNotificationCount"
        private const val LOCK_NOTIFICATION_CURRENT_KEY = "lockNotificationCurrent"
        private const val LOCK_NOTIFICATION_LAST_TIME_KEY = "lockNotificationLastTime"

        var isScreenOn = false
        var isLock = false

        fun register(context: Context) {
            val intentFilter = IntentFilter().apply {
                addAction(Intent.ACTION_SCREEN_OFF)
                addAction(Intent.ACTION_SCREEN_ON)
                addAction(Intent.ACTION_USER_PRESENT)
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                context.registerReceiver(UnlockReceiver(), intentFilter, Context.RECEIVER_EXPORTED)
            } else {
                context.registerReceiver(UnlockReceiver(), intentFilter)
            }
        }
    }

    override fun onReceive(context: Context, intent: Intent) {
        when (intent.action) {
            Intent.ACTION_SCREEN_OFF -> {
                isScreenOn  = false
                isLock = true
            }
            Intent.ACTION_SCREEN_ON -> {
                isScreenOn  = true
            }
            Intent.ACTION_USER_PRESENT -> {
                isLock = false
                if (isScreenOn && !isLock) {
                    handle(context)
                }
            }
            else -> return
        }
    }

    private fun handle(context: Context) {
        val locks = SPUtils.getInstance().getInt(LOCK_NOTIFICATION_STATUS_KEY, 1)
        if (locks != 1) return
        val interval = SPUtils.getInstance().getInt(LOCK_NOTIFICATION_INTERVAL_KEY, 1) * 60 * 1000L
        val count = SPUtils.getInstance().getInt(LOCK_NOTIFICATION_COUNT_KEY, 100)

        val currentKey = "${LOCK_NOTIFICATION_CURRENT_KEY}_${context.toDate()}"
        val current = SPUtils.getInstance().getInt(currentKey)
        val lastTimeKey = "${LOCK_NOTIFICATION_LAST_TIME_KEY}_${context.toDate()}"
        val lastTime = SPUtils.getInstance().getLong(lastTimeKey, 0)
        val currentTime = System.currentTimeMillis()
        if (currentTime - lastTime > interval && current < count) {
            SPUtils.getInstance().putInt(currentKey, current + 1)
            SPUtils.getInstance().putLong(lastTimeKey, currentTime)
            NotificationUtils.sendNotification(CleanApplication.context, "unlock_receiver")
        }
    }
}