package com.secspace.lib.common.utils

import android.content.Context
import android.content.pm.PackageManager
import android.graphics.drawable.Drawable
import android.provider.Settings
import com.secspace.lib.common.function.service.EduAccessibilityService
import com.skr.activation.utils.DesUtil

fun decrypt(strMi: String): String {
    return DesUtil.decrypt(strMi)
}

fun encrypt(strMi: String): String {
    return DesUtil.encrypt(strMi)
}

fun openAccessibility(ctx: Context) {
    try {
        Settings.Secure.putString(ctx.contentResolver,
                Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, ctx.packageName + "/" + EduAccessibilityService::class.java.name)
        Settings.Secure.putString(ctx.contentResolver,
                Settings.Secure.ACCESSIBILITY_ENABLED, "1")
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

fun closeAccessibility(ctx: Context) {
    try {
        Settings.Secure.putString(ctx.contentResolver,
                Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, "")
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

fun getApplicationName(context: Context?, packageName: String): String {
    val pm = context?.packageManager
    return try {
        pm?.getApplicationLabel(pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA)).toString()
    } catch (e: java.lang.Exception) {
        pm?.getApplicationInfo(packageName, PackageManager.GET_META_DATA)?.loadLabel(pm).toString()
    }
}

fun getApplicationIcon(context: Context?, packageName: String): Drawable? {
    val pm = context?.packageManager
    return try {
        pm?.getApplicationIcon(pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA))
    } catch (e: java.lang.Exception) {
        pm?.getApplicationInfo(packageName, PackageManager.GET_META_DATA)?.loadIcon(pm)
    }
}
