package com.test.basd.supercleanermax.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.blankj.utilcode.util.SPUtils
import com.test.basd.supercleanermax.bean.ConfigBean.Companion.ID_CHARGE
import com.test.basd.supercleanermax.bean.ConfigBean.Companion.ID_LOW_BATTERY_PUSH
import com.test.basd.supercleanermax.display.NotificationHelper.postActionNotification
import com.test.basd.supercleanermax.helps.BaseApplication
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() {
            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
        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) {
                    // 展示主动推送
                    NotificationTimerTask(null).oneShotNotification()
                }
            }

            Intent.ACTION_POWER_CONNECTED -> {
                BaseApplication.context.postActionNotification(ID_CHARGE)
            }

            Intent.ACTION_POWER_DISCONNECTED -> {

            }

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

        }
    }

    private fun lowBattery(intent: Intent?) {
        val level = intent?.getIntExtra("level", 0) ?: 0
        if (level < 50) {

            val lastPushTime = SPUtils.getInstance().getLong(ID_LOW_BATTERY_PUSH.toString(), 0)
            val flag2 = (System.currentTimeMillis() - lastPushTime) > 20.minutes.toLong(DurationUnit.MILLISECONDS)
            val isPushCfg = PushStrategy.isPush(ID_LOW_BATTERY_PUSH)

            if (flag2 && isPushCfg) {
                BaseApplication.context.postActionNotification(ID_LOW_BATTERY_PUSH, extra = level)
            }
        }
    }


}