package com.hh.arome.services.keeplive

import android.app.ActivityManager
import android.content.Context
import android.text.TextUtils
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import com.hh.arome.utlis.log.AppLogUtils


class JobUtils{
    companion object{
        fun isRunningService(context: Context, name: String?): Boolean {
            val am =
                context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            val runningServices =
                am.getRunningServices(100)
            for (info in runningServices) {
                if (TextUtils.equals(info.service.className, name)) {
                    return true
                }
            }
            return false
        }


        fun isActivityDestroyed(context: Context, isRunningActivityName: String?): Boolean {

            val activityNames = getRunningActivities(context)

            AppLogUtils.i("要判断的isRunningActivityName:"+isRunningActivityName)

            for (activityName in activityNames) {
                // 获取Activity的类名
                AppLogUtils.i("运行中的activityName--"+activityName.toString())
                if (TextUtils.equals(activityName, isRunningActivityName)) {
                    AppLogUtils.i("for循环--有该Activity")
                    return false
                }
            }
            AppLogUtils.i("for循环--无该Activity")
            return true

        }



//        fun isRunningActivity1(context: Context, isRunningActivityName: String?): Boolean {
//            // 获取ActivityManager实例
//            val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
//
//            // 获取当前Activity栈
//            val taskInfo = activityManager.getRunningTasks(20)
//
//adb i
//            // 遍历栈中的每个Activity
//            for (runningTaskInfo in taskInfo) {
//                // 获取Activity的类名
//                val activityName = runningTaskInfo.topActivity?.className
//
//                AppLogUtils.i("当前运行的activityName:"+activityName)
//
//                if (TextUtils.equals(activityName, isRunningActivityName)) {
//                    AppLogUtils.i("isRunningActivity--有该Activity")
//                    return false
//                }
//            }
//            AppLogUtils.i("isRunningActivity--无该Activity")
//            return true
//
//        }



        fun getRunningActivities(context: Context): List<String?> {
            val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            // getRunningTasks()方法在Android Lollipop（API 21）以后已经弃用
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                // （API 21）及以后
                // 注意：此方法需要`android.permission.GET_TASKS`权限
                val tasks = activityManager.getAppTasks()
                val activityNames = tasks.map { task ->
                    task.taskInfo.baseActivity?.className
                }


                for (activityName in activityNames) {
                    // 获取Activity的类名
                    AppLogUtils.i("activityName--"+activityName.toString())
                }

                return activityNames
            } else {
                // 对于Android Lollipop以前的版本
                val tasks = activityManager.getRunningTasks(Integer.MAX_VALUE)
                val activityNames = tasks.map { task ->
                    task.topActivity?.className
                }

                for (activityName in activityNames) {
                    // 获取Activity的类名
                    AppLogUtils.i("activityName--"+activityName.toString())
                }

                return activityNames
            }
        }



        fun isActivityDestroyed(activity: AppCompatActivity): Boolean {
            val currentState = activity.lifecycle.currentState
            if(currentState == Lifecycle.State.DESTROYED){
                return true
            }else{
                return false
            }
        }

    }

}