package com.zxhyis.toolss.ui.page.applock

import android.app.Service
import android.app.usage.UsageEvents
import android.app.usage.UsageStatsManager
import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.IBinder
import android.util.Log
import com.blankj.utilcode.util.SPUtils

class ProtectorService : Service() {
    private var packageName: String? = ""
    private val handler = Handler()

    private val runnable: Runnable = object : Runnable {
        override fun run() {
            initProtector()
            handler.postDelayed(this, 1000)
        }
    }

    private val usageStatsManager by lazy { getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager }

    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        handler.postDelayed(runnable, 1000)
        return START_STICKY
    }

    override fun onDestroy() {
        handler.removeCallbacks(runnable)
        super.onDestroy()
    }

    private fun initProtector() {
        var previousEventApp = ""
        var recentEventApp = ""
        var recentEventTime = 0L
        var tempRecentEventApp = ""
        var tempRecentEventTime = 0L

        val currentTime = System.currentTimeMillis()
        val usageEvents = usageStatsManager.queryEvents(currentTime - 5 * 1000, currentTime)

        val scannedEvent = UsageEvents.Event()

        while (usageEvents.hasNextEvent()) {
            usageEvents.getNextEvent(scannedEvent)

            val scannedEventType = scannedEvent.eventType
            val scannedEventApp = scannedEvent.packageName
            val scannedEventTime = scannedEvent.timeStamp

            if (scannedEventType == UsageEvents.Event.ACTIVITY_RESUMED && scannedEventTime > recentEventTime) {
                tempRecentEventApp = scannedEventApp
                tempRecentEventTime = scannedEventTime
            }
        }
        Log.e("MXL", "启动: ")
        if (tempRecentEventTime > recentEventTime) {
            if (tempRecentEventApp == recentEventApp) previousEventApp = packageName ?: ""

            recentEventApp = tempRecentEventApp
            recentEventTime = tempRecentEventTime
        }

        if (recentEventApp != previousEventApp) {
            previousEventApp = recentEventApp

            //在这里判断包名启动lockactivity
            Log.e("MXL", "initProtector: " + previousEventApp)
            val stringSet =
                SPUtils.getInstance().getStringSet("lockApps", mutableSetOf())
            stringSet.forEach {
                if (previousEventApp == it) {
                    Log.e("MXL", "相同要跳转锁屏: " )
                    val intent = Intent(this, AppLockActivity::class.java)
                    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                    startActivity(intent)
                }
            }

            //
        }
    }

    override fun onBind(intent: Intent): IBinder? {
        return null
    }
}

fun startProtectorService(context: Context, packageName: String) {
    val intent = Intent(context, ProtectorService::class.java)
    intent.putExtra("package_name", packageName)
    context.startService(intent)
}

fun stopProtectorService(context: Context) {
    val intent = Intent(context, ProtectorService::class.java)
    context.stopService(intent)
}
