package com.base.superfilemanager.helps.recentapp

import android.app.usage.UsageEvents
import android.app.usage.UsageStatsManager
import android.content.Context
import java.util.LinkedList

class ScreenTimeStat(val beginTs: Long, val endTs: Long) {

    private val TAG = "ScreenTimeStat"
    fun run(context: Context): Map<String, RecentBean> {
        var obj: UsageEvents.Event?
        var obj2: UsageEvents.Event?
        val usageStatsManager = context.getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
        val hashMap = HashMap<String, RecentBean>()
        var j12: Long = 0
        if (endTs - beginTs > 259200000) {
            val queryUsageStats = usageStatsManager.queryUsageStats(0, beginTs, endTs)
            if (queryUsageStats != null) {
                for (usageStats in queryUsageStats) {
                    if (usageStats.totalTimeInForeground > j12 && usageStats.lastTimeUsed > beginTs) {
                        val bVar = hashMap[usageStats.packageName]
                        if (bVar == null) {
                            hashMap[usageStats.packageName] = RecentBean(
                                getAppName(context, usageStats.packageName),
                                usageStats.packageName, usageStats.totalTimeInForeground
                            )
                        } else {
                            bVar.c += usageStats.totalTimeInForeground
                        }
                    }
                    j12 = 0
                }
            }
        } else {
            val linkedList = LinkedList<LinkedList<UsageEvents.Event>>()
            val queryEvents = usageStatsManager.queryEvents(beginTs, endTs)
            while (queryEvents != null && queryEvents.hasNextEvent()) {
                val event = UsageEvents.Event()
                if (queryEvents.getNextEvent(event) && event.eventType != 23 && event.eventType != 11) {
                    if (!linkedList.isEmpty() && linkedList.getLast().getLast().packageName == event.packageName) {
                        linkedList.getLast().addLast(event)
                    } else {
                        val linkedList2 = LinkedList<UsageEvents.Event>()
                        linkedList2.addLast(event)
                        linkedList.addLast(linkedList2)
                    }
                }
            }
            for (linkedList3 in linkedList) {
                val packageName = linkedList3.getFirst().packageName
                val it2: Iterator<UsageEvents.Event> = linkedList3.iterator()
                while (true) {
                    obj = null
                    if (it2.hasNext()) {
                        obj2 = it2.next()
                        if (obj2.eventType == 1) {
                            break
                        }
                    } else {
                        obj2 = null
                        break
                    }
                }
                val event2 = obj2
                val listIterator: ListIterator<UsageEvents.Event> = linkedList3.listIterator(linkedList3.size)
                while (true) {
                    if (listIterator.hasPrevious()) {
                        val previous = listIterator.previous()
                        if (previous.eventType == 2) {
                            obj = previous
                        }
                    } else {
                        break
                    }
                }
                val event3 = obj
                if (event2 != null && event3 != null && event2.timeStamp < event3.timeStamp) {
                    var bVar2 = hashMap[packageName]
                    if (bVar2 == null) {
                        bVar2 = RecentBean(getAppName(context, packageName), packageName, 0L)
                    }
                    bVar2.c += event3.timeStamp - event2.timeStamp
                    hashMap[packageName] = bVar2
                }
            }
        }

//        hashMap.forEach { (key, value) ->
//            LogEx.logDebug(TAG, "key=$key value=$value")
//        }
        return hashMap
    }

    companion object {
        fun getAppName(context: Context, str: String?): String {
            val packageManager = context.packageManager
            return try {
                packageManager.getApplicationLabel(packageManager.getApplicationInfo(str!!, 0)).toString()
            } catch (e10: Exception) {
                ""
            }
        }
    }
}