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 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_INSTALL_PACKAGE_PUSH
import com.zxhyis.toolss.ad.bean.PushCfg.Companion.ID_LOW_BATTERY_PUSH
import com.zxhyis.toolss.ad.bean.PushCfg.Companion.ID_UNINSTALL_PACKAGE_PUSH
import com.zxhyis.toolss.common.notification.NotificationHelper.postActionNotification
import com.zxhyis.toolss.logic.LogEx

/**
 * 只监听安装卸载
 */
class PackageBroadcast : BroadcastReceiver() {

    companion object {
        private val TAG = "PackageBroadcast"
        fun Context.newInitBroadcast() {
//            LogEx.logDebug(TAG, "initBroadcast")
            val filter = IntentFilter()
            filter.addAction(Intent.ACTION_PACKAGE_ADDED)
            filter.addAction(Intent.ACTION_PACKAGE_REMOVED)
            filter.addDataScheme("package")
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                registerReceiver(PackageBroadcast(), filter, Context.RECEIVER_EXPORTED)
            } else {
                registerReceiver(PackageBroadcast(), filter)
            }
        }
    }


    override fun onReceive(context: Context, intent: Intent?) {
        LogEx.logDebug(TAG, "onReceive")
        val action = intent?.action ?: 0
        LogEx.logDebug(TAG, "onReceive action=$action")
        when (action) {

            Intent.ACTION_PACKAGE_REMOVED -> {
                FileApp.fContext.postActionNotification(ID_UNINSTALL_PACKAGE_PUSH)
            }

            Intent.ACTION_PACKAGE_ADDED -> {
                FileApp.fContext.postActionNotification(ID_INSTALL_PACKAGE_PUSH)
            }
        }
    }

    fun lowJunkPush(context: Context) {
        val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val memoryInfo = ActivityManager.MemoryInfo()
        activityManager.getMemoryInfo(memoryInfo)
        val availableMemory = memoryInfo.availMem
        val totalMemory = memoryInfo.totalMem
        val num = totalMemory - availableMemory
        val entity = ComUtils.getPushTypeData("22005")
        if (num >= (entity?.feature_ex1 ?: 0)) {
            // 展示性能变差推送
            val flag = PushManager.isPush(PushCfg.ID_LOW_RAM_PUSH)
            if (flag) {
                FileApp.fContext.postActionNotification(PushCfg.ID_LOW_RAM_PUSH)
            }
        }

    }

    fun charging(intent: Intent?) {
        //电量发生变化
        val level = intent?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1
        val scale: Int? = intent?.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
        //低电量阀值推送
        val config = ComUtils.getPushTypeData(ID_LOW_BATTERY_PUSH.toString())
        val flag = PushManager.isPush(ID_LOW_BATTERY_PUSH)
        val lowValue = config?.feature_ex1 ?: 0
        if (flag && (level < lowValue)) {
            FileApp.fContext.postActionNotification(ID_LOW_BATTERY_PUSH)
        }
    }
}