package com.secspace.lib.common.function.service

import android.content.Context
import android.util.Log
import com.secspace.lib.common.function.database.DatabaseManager
import com.secspace.lib.common.function.model.UseApp
import com.secspace.lib.common.function.model.UseTime
import com.secspace.lib.common.utils.getApplicationIcon
import com.secspace.lib.common.utils.getApplicationName

class UseAppManager private constructor(val context: Context) {
    private val TAG = "UseAppManager"
    private val pkgQueue: ArrayList<UseTime> = arrayListOf()
    private var ignorePkg = arrayListOf("android", "com.eduspace.mobilemanager", "com.vmall.client", "com.android.systemui")
    suspend fun disposePackageName(packageName: String) {
        val useTime = UseTime()
        useTime.appPackage = packageName
        useTime.useStartTime = System.currentTimeMillis()
        useTime.uploadTime = 0
        // 是否是应该被忽略的应用
        if(ignorePkg.contains(packageName)){
            if (pkgQueue.isEmpty()) {
                return
            }
            if(pkgQueue.size == 1){
                val existPkg = pkgQueue[0]
                // 本次应用与上次应用一样,清空缓存
                if (existPkg.appPackage == useTime.appPackage){
                    pkgQueue.clear()
                    return
                }
                // 本次应用与上次不一样，计算时间，如果大于65s，则将赋予上次应用结束时间，存入数据库，并将上次应用移除
                Log.i(TAG, "上次时间 ${existPkg.useStartTime}, 本次时间 ${useTime.useStartTime} 差: ${useTime.useStartTime - existPkg.useStartTime}")
                if (useTime.useStartTime - existPkg.useStartTime >= 65 * 1000){
                    // 给应用赋予结束时间
                    existPkg.useEndTime = System.currentTimeMillis()
                    Log.i(TAG, "insertUseTime:${DatabaseManager.getInstance(context).getUseTimeDao()}")
                    // 存入数据库
                    val result = DatabaseManager.getInstance(context).getUseTimeDao()?.insertUseTime(existPkg)
                    Log.i(TAG, "insertUseTime result:$result")
                    pkgQueue.clear()
                }
            }
            return
        }
        // 第一次打开某应用，将其放到第一位
        if (pkgQueue.isEmpty()) {
            Log.i(TAG, "pkgQueue is Empty")
            pkgQueue.add(0, useTime)
            return
        }
        if(pkgQueue.size == 1){
            // 取出上一轮存储的应用
            val existPkg = pkgQueue[0]
            // 本次应用与上次应用一样,什么也不做
            Log.i(TAG, "上次应用 ${existPkg.appPackage}, 本次应用 ${useTime.appPackage}")
            if (existPkg.appPackage == useTime.appPackage){
                return
            }
            // 本次应用与上次不一样，计算时间，如果大于65s，则将赋予上次应用结束时间，存入数据库，并将上次应用移除
            Log.i(TAG, "上次时间 ${existPkg.useStartTime}, 本次时间 ${useTime.useStartTime} 差: ${useTime.useStartTime - existPkg.useStartTime}")
            if (useTime.useStartTime - existPkg.useStartTime >= 65 * 1000){
                // 给应用赋予结束时间
                existPkg.useEndTime = System.currentTimeMillis()
                // 存入数据库
                DatabaseManager.getInstance(context).getUseTimeDao()?.insertUseTime(existPkg)
            }
            // 无论是否存数据库，都要替换掉
            pkgQueue[0] = useTime
            return
        }
    }

    private fun disposeApp(packageName: String){
        val appName = getApplicationName(context, packageName)
        val icon = getApplicationIcon(context, packageName)
        val useApp = UseApp()
    }

    companion object {
        @Volatile
        private var instance: UseAppManager? = null

        fun getInstance(context: Context): UseAppManager {
            if (instance == null) {
                synchronized(UseAppManager::class) {
                    if (instance == null) {
                        instance = UseAppManager(context.applicationContext)
                    }
                }
            }
            return instance!!
        }
    }
}