package com.xm.test.myfilemaster.notification

import android.app.ActivityManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.BatteryManager
import android.util.Log
import com.xm.test.myfilemaster.MyApplication
import com.xm.test.myfilemaster.ad.ComUtils
import com.xm.test.myfilemaster.ad.PushManager
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_BOOST_PUSH
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_CHARGE
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_INSTALL_PACKAGE_PUSH
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_LOW_BATTERY_PUSH
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_LOW_JUNK_PUSH
import com.xm.test.myfilemaster.ad.bean.PushCfg.Companion.ID_UNINSTALL_PACKAGE_PUSH
import com.xm.test.myfilemaster.notification.NotificationHelper.postNotification
import com.xm.test.myfilemaster.common.notification.NotificationEx
import java.io.File


class ActionBroadcast : BroadcastReceiver() {


    private var mIsScreenOn = false
    private var isLock = false

    override fun onReceive(context: Context?, intent: Intent?) {

        val action = intent?.action ?: 0
        if (action == Intent.ACTION_SCREEN_ON) {
            Log.d("glc", "屏幕解锁广播...")
            Log.d("glc", "屏幕亮广播")
            mIsScreenOn = true
            if (!isLock && mIsScreenOn) {
                val id = NotificationEx.getPresentPushId()
                val flag = PushManager.isPush(id)
                if (flag) {
                    MyApplication.fContext.postNotification(id)
                }
            }
        } else if (action == Intent.ACTION_SCREEN_OFF) {
            Log.d("glc", "屏幕加锁广播...")
            Log.d("glc", "屏幕熄灭广播")
            mIsScreenOn = false
            isLock = true
        } else if (action == Intent.ACTION_USER_PRESENT) {
            isLock = false
//            Log.d("glc", "解锁")
            if (mIsScreenOn && !isLock) {
                // 展示主动推送
                val id = NotificationEx.getPresentPushId()
                val flag = PushManager.isPush(id)
                if (flag) {
                    MyApplication.fContext.postNotification(id)
                }
            }
        } else if (action == Intent.ACTION_PACKAGE_REMOVED) {
//            Log.d("glc", "app卸载")
            Log.d("glc", "app卸载")
            val flag = PushManager.isPush(ID_UNINSTALL_PACKAGE_PUSH)
            if (flag) {
                MyApplication.fContext.postNotification(ID_UNINSTALL_PACKAGE_PUSH)
            }
        } else if (action == Intent.ACTION_PACKAGE_ADDED) {
//            Log.d("glc", "app安装")
            Log.d("glc","app安装")
            val flag = PushManager.isPush(ID_INSTALL_PACKAGE_PUSH)
            if (flag) {
                MyApplication.fContext.postNotification(ID_INSTALL_PACKAGE_PUSH)
            }
        } else if (action == Intent.ACTION_POWER_DISCONNECTED || action == Intent.ACTION_POWER_CONNECTED) {
//            Log.d("glc", "拔电和插电")
            Log.d("glc","拔电和插电")
            val flag = PushManager.isPush(ID_CHARGE)
            if (flag) {
                MyApplication.fContext.postNotification(ID_CHARGE)
            }
        } else if (action == Intent.ACTION_BATTERY_CHANGED) {
            //电量发生变化
            val level = intent?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1
            val scale: Int? = intent?.getIntExtra(BatteryManager.EXTRA_SCALE, -1)

//            Log.d("mxl", "ACTION_BATTERY_CHANGED")
            //低电量阀值推送
            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)) {
                MyApplication.fContext.postNotification(ID_LOW_BATTERY_PUSH)
            }
        } else if (action == Intent.ACTION_MEDIA_MOUNTED || action == Intent.ACTION_MEDIA_REMOVED || action == Intent.ACTION_MEDIA_UNMOUNTED || action == Intent.ACTION_MEDIA_BAD_REMOVAL) {
            val path = intent?.data?.path
            val file = File(path)
            val availableSpace = file.usableSpace
            val totalSpace = file.totalSpace
            val num = totalSpace - availableSpace
            val entity = ComUtils.getPushTypeData("22007")
            if (num <= entity?.feature_ex1 ?: 0) {
                //展示垃圾文件过大推送

                val flag = PushManager.isPush(ID_LOW_JUNK_PUSH)
                if (flag) {
                    MyApplication.fContext.postNotification(ID_LOW_JUNK_PUSH)
                }
            }
        } else if (action == Intent.ACTION_DEVICE_STORAGE_LOW || action == Intent.ACTION_DEVICE_STORAGE_OK) {
            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(ID_BOOST_PUSH)
                if (flag) {
                    MyApplication.fContext.postNotification(ID_BOOST_PUSH)
                }
            }
        }
    }

}