package com.cheng.blzb.utils

import android.Manifest
import android.app.Activity
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.fragment.app.FragmentManager
import com.cheng.blzb.manager.NotificationHelper
import com.cheng.blzb.ui.dialog.PermissionTipDialog
import com.example.base.extensions.toast
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.XXPermissions

object PermissionUtils {

    /**
     * 相册和相机权限
     */
    val PHOTO_CAMERA_PERMISSION = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        mutableListOf(Manifest.permission.READ_MEDIA_IMAGES, Manifest.permission.READ_MEDIA_VIDEO, Manifest.permission.CAMERA)
    } else {
        mutableListOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
    }

    /**
     * 相册权限
     */
    val PHOTO_PERMISSION = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        mutableListOf(Manifest.permission.READ_MEDIA_IMAGES, Manifest.permission.READ_MEDIA_VIDEO)
    } else {
        mutableListOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)
    }

    /**
     * 音频权限
     */
    val AUDIO_PERMISSION = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        mutableListOf(Manifest.permission.READ_MEDIA_AUDIO)
    } else {
        mutableListOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)
    }

    /**
     * 本地读取权限
     */
    val STORAGE_PERMISSION = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        mutableListOf(Manifest.permission.READ_MEDIA_IMAGES, Manifest.permission.READ_MEDIA_VIDEO, Manifest.permission.READ_MEDIA_AUDIO)
    } else {
        mutableListOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)
    }

    /**
     * 通知权限
     */
    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    val NOTIFICATION_PERMISSION = mutableListOf(Manifest.permission.POST_NOTIFICATIONS)

    fun checkStoragePermission(activity: Activity, fm: FragmentManager, callback: ((isGranted: Boolean) -> Unit)? = null) {
        if (XXPermissions.isGranted(activity, STORAGE_PERMISSION)) {
            callback?.invoke(true)
        } else {
            var showTip = true
            var showToast = false
            var f : PermissionTipDialog? = null
            XXPermissions.with(activity).permission(STORAGE_PERMISSION).request(object : OnPermissionCallback {
                override fun onGranted(list: MutableList<String>, isGranted: Boolean) {
                    showTip = false
                    if (isGranted) {
                        callback?.invoke(true)
                    } else {
                        if (!showToast) {
                            toast("使用文件查看删除和保存功能请允许储存权限")
                            callback?.invoke(false)
                            showToast = true
                        }
                    }
                    f?.dismiss()
                }

                override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                    showTip = false
                    if (!showToast) {
                        toast("使用文件查看删除和保存功能请允许储存权限")
                        callback?.invoke(false)
                        showToast = true
                    }
                    f?.dismiss()
                }
            })
            activity.window.decorView.postDelayed({
                if (showTip && !activity.isDestroyed) {
                    f = PermissionTipDialog.newInstance("此功能需要您授权允许储存权限，以便正常查看删除文件或保存文件到本地")
                    f!!.show(fm, null)
                }
            }, 300)
        }
    }

    fun checkPhotoPermission(activity: Activity, fm: FragmentManager, needCamera: Boolean = false, callback: ((Boolean) -> Unit)? = null) {
        if (XXPermissions.isGranted(activity, if (needCamera) PHOTO_CAMERA_PERMISSION else PHOTO_PERMISSION)) {
            callback?.invoke(true)
        } else {
            var showTip = true
            var f : PermissionTipDialog? = null
            XXPermissions.with(activity).permission(if (needCamera) PHOTO_CAMERA_PERMISSION else PHOTO_PERMISSION).request(object : OnPermissionCallback {
                override fun onGranted(list: MutableList<String>, isGranted: Boolean) {
                    showTip = false
                    if (isGranted) {
                        callback?.invoke(true)
                    } else {
                        toast("使用本地视频或图像添加、查看、分享和删除等功能请允许相册,相机和储存权限")
                        callback?.invoke(false)
                    }
                    f?.dismiss()
                }

                override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                    showTip = false
                    toast("使用本地视频或图像添加、查看、分享和删除等功能请允许相册,相机和储存权限")
                    callback?.invoke(false)
                    f?.dismiss()
                }
            })
            activity.window.decorView.postDelayed({
                if (showTip && !activity.isDestroyed) {
                    f = PermissionTipDialog.newInstance("此功能需要您授权允许相册,相机和储存权限，以便正常使用相册和相机添加、查看、分享和删除本地视频或图像")
                    f!!.show(fm, null)
                }
            }, 300)
        }
    }

    fun checkAudioPermission(activity: Activity, fm: FragmentManager, callback: ((Boolean) -> Unit)? = null) {
        if (XXPermissions.isGranted(activity, AUDIO_PERMISSION)) {
            callback?.invoke(true)
        } else {
            var showTip = true
            var f : PermissionTipDialog? = null
            XXPermissions.with(activity).permission(AUDIO_PERMISSION).request(object : OnPermissionCallback {
                override fun onGranted(list: MutableList<String>, isGranted: Boolean) {
                    showTip = false
                    if (isGranted) {
                        callback?.invoke(true)
                    } else {
                        toast("使用本地音频查看、分享和删除等功能请允许音频读写权限")
                        callback?.invoke(false)
                    }
                    f?.dismiss()
                }

                override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                    showTip = false
                    toast("使用本地音频查看、分享和删除等功能请允许音频读写权限")
                    callback?.invoke(false)
                    f?.dismiss()
                }
            })
            activity.window.decorView.postDelayed({
                if (showTip && !activity.isDestroyed) {
                    f = PermissionTipDialog.newInstance("此功能需要您授权允许音频读写权限，以便正常使用本地音频查看、分享和删除等功能")
                    f!!.show(fm, null)
                }
            }, 300)
        }
    }

    fun checkNotificationPermission(activity: Activity, callback: ((isGranted: Boolean) -> Unit)? = null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            if (XXPermissions.isGranted(activity, NOTIFICATION_PERMISSION)) {
                callback?.invoke(true)
            } else {
                XXPermissions.with(activity).permission(NOTIFICATION_PERMISSION).request(object : OnPermissionCallback {
                    override fun onGranted(list: MutableList<String>, isGranted: Boolean) {
                        callback?.invoke(isGranted)
                    }

                    override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                        callback?.invoke(false)
                    }
                })
            }
        } else {
            callback?.invoke(NotificationHelper.isNotificationEnabled(activity))
        }

    }
}