package com.zxhyis.toolss.common.notification

import android.app.ActivityManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Build
import android.util.Log
import com.zxhyis.toolss.FileApp
import com.zxhyis.toolss.ad.ComUtils
import com.zxhyis.toolss.ad.PushManager
import com.zxhyis.toolss.ad.bean.PushCfg
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.bean.PushCfg.Companion.ID_LOW_RAM_PUSH
import com.zxhyis.toolss.ad.createByActionId
import com.zxhyis.toolss.ad.queryDataStoreBlock
import com.zxhyis.toolss.ad.saveDataStoreBlock
import com.zxhyis.toolss.common.notification.NotificationHelper.postActionNotification
import com.zxhyis.toolss.logic.LogEx
import com.zxhyis.toolss.ui.receiver.BatteryReceiver
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 = "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) {
                    // 展示主动推送
                    NotificationTimerTask(null).oneShotNotification()
                }
            }

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

            Intent.ACTION_POWER_DISCONNECTED -> {

            }

            Intent.ACTION_BATTERY_CHANGED -> {//电量改变
                val level = BatteryReceiver.level
                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 = PushManager.isPush(ID_LOW_BATTERY_PUSH)

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

        }
    }

}