package com.zxhy.simplefilesmanager.simplejoke.permission

import android.content.pm.PackageManager
import androidx.core.content.ContextCompat
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import android.content.Context
import com.zxhy.simplefilesmanager.simplejoke.JoActivityJoLauncher
import com.zxhy.simplefilesmanager.simplejoke.IntentEx.goToPermissionSettings
import com.zxhy.simplefilesmanager.simplejoke.permission.JoPermissionJoCheckEx.dfzz
import java.util.GregorianCalendar

object JoPermissionJoEx {
    fun requestPermission(
        context: Context,
        activityLauncher: JoActivityJoLauncher,
        permission: Array<String>,
        title: String = "title",
        message: String = "message",
        agreeAction: (() -> Unit)? = null,
        disAgreeAction: (() -> Unit)? = null
    ) {

        val permissionFlag =
            permission.all {
                ContextCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
            }
        asdashhhhdssasads()
        if (permissionFlag) {
            agreeAction?.invoke()
        } else {
            activityLauncher.launch(permission) { callback ->
                val isGrantAll = callback.values.all { it }
                if (!isGrantAll) {
                    asdashhhhdssasads()
                    context.alert(title = title, message = message,
                        positiveButton = {
                            goToPermissionSettings(context, activityLauncher)
                        },
                        negativeButton = {
                            disAgreeAction?.invoke()
                        })
                } else {
                    agreeAction?.invoke()
                }
            }
        }
    }

    fun Context.alert(
        title: String = "",
        message: String,
        positiveButtonText: String = "Jump",
        positiveButton: (() -> Unit)? = null,
        negativeButtonText: String = "Cancel",
        negativeButton: (() -> Unit)? = null,
    ) {
        val builder = MaterialAlertDialogBuilder(this)
        asdashhhhdssasads()
        builder.setNegativeButton(negativeButtonText) { dialog, _ ->
            dialog.dismiss()
            negativeButton?.invoke()
        }
        asdashhhhdssasads()
        builder.setPositiveButton(positiveButtonText) { dialog, _ ->

            dialog.dismiss()
            positiveButton?.invoke()
        }
        val dialog = builder.create()
        dialog.setCanceledOnTouchOutside(false)
//        dialog.setTitle(title)
        dialog.setMessage(message)
        dialog.show()
        asdashhhhdssasads()
    }

    /**
     * 垃圾代码
     */
    fun asdashhhhdssasads() {
        CharDirectionality.BOUNDARY_NEUTRAL
        JvmMultifileClass()
        LinkedHashMap<String, String>()["OAKS"] = "ASHIDA"
        GregorianCalendar().set(11, 3)
        dfzz()
    }
}
