package com.test.basd.fastcleanerjunk.helps

import android.annotation.SuppressLint
import android.app.AppOpsManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.Settings
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.PermissionUtils
import com.test.basd.fastcleanerjunk.helps.PermissionHelp.requestUsageAccessSettings

object PermissionHelp {
    fun checkStorePermission(): Boolean {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            Environment.isExternalStorageManager()
        } else {
            PermissionUtils.isGranted(PermissionConstants.STORAGE)
        }
    }

    fun Context.manageStoreIntent(): Intent {
        val intent =
            Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
        intent.addCategory("android.intent.category.DEFAULT")
        intent.data = Uri.parse("package:${packageName}")
        return intent
    }

    fun Context.requestStorePermission(
        launcher: ActivityLauncher,
        result: (flag: Boolean) -> Unit,
        resolveNoAction: (() -> Unit)? = null,
    ) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            val intent = manageStoreIntent()
            if (intent.resolveActivity(packageManager) != null) {
                launcher.launch(intent) {
                    result.invoke(checkStorePermission())
                }
            } else {
                resolveNoAction?.invoke()
            }
        } else {
            PermissionUtils.permissionGroup(PermissionConstants.STORAGE).callback { isAllGranted, _, _, _ ->
                result.invoke(isAllGranted)
            }
        }

    }


    fun Context.checkUsageAccessSettings(): Boolean {

        val appOpsManager = getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager

        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            appOpsManager.unsafeCheckOpNoThrow(
                AppOpsManager.OPSTR_GET_USAGE_STATS,
                android.os.Process.myUid(),
                this.packageName
            ) == AppOpsManager.MODE_ALLOWED
        } else {
            appOpsManager.checkOpNoThrow(
                AppOpsManager.OPSTR_GET_USAGE_STATS,
                android.os.Process.myUid(),
                this.packageName
            ) == AppOpsManager.MODE_ALLOWED
        }
    }

    fun Context.usageAccessSettings(): Intent {
        val intent = Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)
        intent.addCategory("android.intent.category.DEFAULT")
        intent.data = Uri.parse("package:${this.packageName}")
        return intent
    }

    fun Context.requestUsageAccessSettings(
        launcher: ActivityLauncher,
        result: (flag: Boolean) -> Unit,
        resolveNoAction: (() -> Unit)? = null,
    ) {
        val intent = usageAccessSettings()
        if (intent.resolveActivity(packageManager) != null) {
            launcher.launch(intent) {
                result.invoke(checkUsageAccessSettings())
            }
        } else {
            resolveNoAction?.invoke()
        }
    }
}