package com.zxhyis.toolss.notification.broadcast

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.zxhyis.toolss.FileApp
import com.zxhyis.toolss.ad.PushStrategy
import com.zxhyis.toolss.ad.bean.PushCfg.Companion.ID_CHARGE
import com.zxhyis.toolss.ad.bean.PushCfg.Companion.ID_LOW_BATTERY_PUSH
import com.zxhyis.toolss.ad.createByActionId
import com.zxhyis.toolss.ad.queryDataStoreBlock
import com.zxhyis.toolss.logic.LogEx
import com.zxhyis.toolss.notification.NotificationHelper.postActionNotification
import com.zxhyis.toolss.notification.NotificationUtils
import kotlin.time.Duration.Companion.minutes
import kotlin.time.DurationUnit


class ActionBroadcast : BroadcastReceiver() {


    private var mIsScreenOn = false
    private var isLock = false

    companion object {
        private val TAG = "com.zxhyis.toolss.notification.broadcast.ActionBroadcast"
        fun Context.initBroadcast() {
            LogEx.logDebug(TAG, "initBroadcast")
            val filter = IntentFilter()
            filter.addAction(Intent.ACTION_SCREEN_OFF)
            filter.addAction(Intent.ACTION_SCREEN_ON)
            filter.addAction(Intent.ACTION_USER_PRESENT)
            filter.addAction(Intent.ACTION_POWER_CONNECTED)
            filter.addAction(Intent.ACTION_POWER_DISCONNECTED)
            filter.addAction(Intent.ACTION_BATTERY_CHANGED)
            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
        LogEx.logDebug(TAG, "onReceive action=$action", isMust = false)
        when (action) {
            Intent.ACTION_SCREEN_ON -> {
                LogEx.logDebug(TAG, "开屏")
                mIsScreenOn = true
            }

            Intent.ACTION_SCREEN_OFF -> {
                LogEx.logDebug(TAG, "息屏")
                mIsScreenOn = false
                isLock = true
            }

            Intent.ACTION_USER_PRESENT -> {
                isLock = false
                LogEx.logDebug(TAG, "解锁")
                Log.d("glc", "解锁")
                if (mIsScreenOn && !isLock) {
                    // 展示主动推送
                    NotificationUtils.sendTimerPush(null)
                }
            }

            Intent.ACTION_POWER_CONNECTED -> {
                LogEx.logDebug(TAG, "充电")
                FileApp.fContext.postActionNotification(ID_CHARGE)
            }

            Intent.ACTION_POWER_DISCONNECTED -> {

            }

            Intent.ACTION_BATTERY_CHANGED -> {//电量改变
                lowBattery(intent)
            }

        }
    }

    fun lowBattery(intent: Intent?) {
        val level = intent?.getIntExtra("level", 0) ?: 0
        LogEx.logDebug(TAG, "电量改变 level=$level", isMust = true)
        if (level < 50) {

            val lastPushTime = FileApp.fContext.queryDataStoreBlock(createByActionId(ID_LOW_BATTERY_PUSH), 0)
            val flag2 = (System.currentTimeMillis() - lastPushTime) > 20.minutes.toLong(DurationUnit.MILLISECONDS)
            val isPushCfg = PushStrategy.isPush(ID_LOW_BATTERY_PUSH)

            if (flag2 && isPushCfg) {
                FileApp.fContext.postActionNotification(ID_LOW_BATTERY_PUSH, extra = level)
            }
        }
    }
}