package com.base.browserwhite.utils

import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.provider.MediaStore
import com.base.browserwhite.bean.ConstObject.MIME_TYPE_APK
import com.base.browserwhite.bean.ConstObject.MIME_TYPE_AUDIO
import com.base.browserwhite.bean.ConstObject.MIME_TYPE_DOC
import com.base.browserwhite.bean.ConstObject.MIME_TYPE_DOCX
import com.base.browserwhite.bean.ConstObject.MIME_TYPE_IMAGE
import com.base.browserwhite.bean.ConstObject.MIME_TYPE_PDF
import com.base.browserwhite.bean.ConstObject.MIME_TYPE_PPT
import com.base.browserwhite.bean.ConstObject.MIME_TYPE_PPTX
import com.base.browserwhite.bean.ConstObject.MIME_TYPE_VIDEO
import com.base.browserwhite.bean.ConstObject.MIME_TYPE_XLS
import com.base.browserwhite.bean.ConstObject.MIME_TYPE_XLSX
import com.base.browserwhite.bean.ConstObject.MIME_TYPE_ZIP
import com.base.browserwhite.bean.MediaBean


object MediaStoreUtils {

    private val TAG = "MediaStoreUtils"

    //图片视频音频不用这个查
    fun Context.getMediaFile(selectionArgs: Array<String>? = null): ArrayList<MediaBean> {

        val list = arrayListOf<MediaBean>()

        val projection = arrayOf(
            MediaStore.Files.FileColumns._ID,
            MediaStore.Files.FileColumns.DATA,
//            MediaStore.Files.FileColumns.TITLE,
            MediaStore.Files.FileColumns.MIME_TYPE,
//            MediaStore.Files.FileColumns.SIZE,
        )

        val mimeTypeSelectionArgs = selectionArgs ?: arrayOf(
            MIME_TYPE_IMAGE,
            MIME_TYPE_VIDEO,
            MIME_TYPE_AUDIO,
            MIME_TYPE_PDF,
            MIME_TYPE_DOC,
            MIME_TYPE_DOCX,
            MIME_TYPE_XLS,
            MIME_TYPE_XLSX,
            MIME_TYPE_PPT,
            MIME_TYPE_PPTX,
            MIME_TYPE_APK
        )

        val mimeTypeSelection = if (mimeTypeSelectionArgs.size == 1) {
            "${MediaStore.Files.FileColumns.MIME_TYPE} = ?"
        } else {
            "${MediaStore.Files.FileColumns.MIME_TYPE} IN (" + mimeTypeSelectionArgs.joinToString(",") { "?" } + ")"
        }

//        val selection = "$timeSelection AND $mimeTypeSelection"
        val selection = mimeTypeSelection

        LogEx.logDebug(TAG, "selection=$selection")

        var cursor: Cursor? = null
        try {
            // 进行查询，并设置排序方式，按照文件添加时间降序排序
            cursor = this.contentResolver.query(
                MediaStore.Files.getContentUri("external"),
                projection,
                selection,
                selectionArgs ?: mimeTypeSelectionArgs,
                MediaStore.Files.FileColumns.DATE_ADDED + " DESC"
            )
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    runCatching {
                        val id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID))
                        val uri = Uri.withAppendedPath(MediaStore.Files.getContentUri("external"), id.toString())

                        val path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA))
//                        val title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.TITLE))
                        val mimeType = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE))
//                        val size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.SIZE))

                        LogEx.logDebug(TAG, "path=$path mimeType=$mimeType")
                        list.add(
                            MediaBean(path = path, uri = uri, mimeType = mimeType)
                        )
                    }
                }
            }

        } catch (e: Exception) {
            LogEx.logDebug(TAG, "Exception $e")
        } finally {
            cursor?.close()
        }

        return list

    }

    fun Context.getMediaPhotoSize(): Int {
        var count = 0

        runCatching {
            val contentResolver = this.contentResolver

            // 定义查询的Uri和列
            val uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
            val projection = arrayOf(MediaStore.Images.Media._ID)

            // 执行查询
            val cursor = contentResolver.query(uri, projection, null, null, null)

            // 检查cursor是否包含数据
            count = cursor?.count ?: 0

            // 关闭cursor
            cursor?.close()
        }


        return count
    }

    fun Context.getMediaPhoto(): List<MediaBean> {

        val list = arrayListOf<MediaBean>()
        val contentResolver = this.contentResolver

        // 定义查询的Uri和列
        val baseUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
        val projection = arrayOf(MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA)

        // 执行查询
        var cursor: Cursor? = null

        try {
            cursor = contentResolver.query(baseUri, projection, null, null, null)

            if (cursor != null) {
                while (cursor.moveToNext()) {
                    val id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID))
                    val uri = Uri.withAppendedPath(baseUri, id.toString())
                    val path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA))
                    list.add(MediaBean(path = path, uri = uri))
                }
            }
        } catch (e: Exception) {

        } finally {
            cursor?.close()
        }

        return list
    }

    fun Context.getMediaVideo(): List<MediaBean> {

        val list = arrayListOf<MediaBean>()
        val contentResolver = this.contentResolver

        // 定义查询的Uri和列
        val baseUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
        val projection = arrayOf(MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA, MediaStore.Video.Media.MIME_TYPE)

        // 执行查询
        var cursor: Cursor? = null

        try {
            cursor = contentResolver.query(baseUri, projection, null, null, null)

            if (cursor != null) {
                while (cursor.moveToNext()) {
                    val id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID))
                    val uri = Uri.withAppendedPath(baseUri, id.toString())
                    LogEx.logDebug(TAG, "getMediaVideo uri=$uri")
                    val path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA))
                    val mimeType = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE))
                    list.add(MediaBean(path = path, uri = uri, mimeType = mimeType))
                }
            }
        } catch (e: Exception) {

        } finally {
            cursor?.close()
        }

        return list
    }

    fun Context.getMediaAudio(): List<MediaBean> {

        val list = arrayListOf<MediaBean>()
        val contentResolver = this.contentResolver

        // 定义查询的Uri和列
        val baseUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
        val projection = arrayOf(MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.MIME_TYPE)

        // 执行查询
        var cursor: Cursor? = null

        try {
            cursor = contentResolver.query(baseUri, projection, null, null, null)

            if (cursor != null) {
                while (cursor.moveToNext()) {
                    val id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID))
                    val uri = Uri.withAppendedPath(baseUri, id.toString())
                    val path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA))
                    val mimeType = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE))
                    list.add(MediaBean(path = path, uri = uri, mimeType = mimeType))
                }
            }
        } catch (e: Exception) {

        } finally {
            cursor?.close()
        }

        return list
    }


    fun Context.getMediaVideoSize(): Int {
        var count = 0
        runCatching {
            val contentResolver = this.contentResolver

            // 定义查询的Uri和列
            val uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
            val projection = arrayOf(MediaStore.Video.Media._ID)

            // 执行查询
            val cursor = contentResolver.query(uri, projection, null, null, null)

            // 检查cursor是否包含数据
            count = cursor?.count ?: 0

            // 关闭cursor
            cursor?.close()
        }
        return count
    }

    fun Context.getMediaAudioSize(): Int {
        var count = 0
        runCatching {
            val contentResolver = this.contentResolver

            // 定义查询的Uri和列
            val uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
            val projection = arrayOf(MediaStore.Audio.Media._ID)

            // 执行查询
            val cursor = contentResolver.query(uri, projection, null, null, null)

            // 检查cursor是否包含数据
            count = cursor?.count ?: 0

            // 关闭cursor
            cursor?.close()
        }
        return count
    }


    fun Context.getMediaDocumentSize(): Int {
        var count = 0
        runCatching {
            val contentResolver = this.contentResolver

            val mimeTypeSelectionArgs = arrayOf(
                MIME_TYPE_PDF,
                MIME_TYPE_DOC,
                MIME_TYPE_DOCX,
                MIME_TYPE_XLS,
                MIME_TYPE_XLSX,
                MIME_TYPE_PPT,
                MIME_TYPE_PPTX,
            )
            val mimeTypeSelection =
                "${MediaStore.Files.FileColumns.MIME_TYPE} IN (" + mimeTypeSelectionArgs.joinToString(",") { "?" } + ")"

            // 定义查询的Uri和列
            val uri = MediaStore.Files.getContentUri("external")
            val projection = arrayOf(MediaStore.Files.FileColumns._ID)

            // 执行查询
            val cursor = contentResolver.query(uri, projection, mimeTypeSelection, mimeTypeSelectionArgs, null)

            // 检查cursor是否包含数据
            count = cursor?.count ?: 0

            // 关闭cursor
            cursor?.close()
        }
        return count
    }

    fun Context.getMediaApkSize(): Int {
        var count = 0
        runCatching {
            val contentResolver = this.contentResolver

            val mimeTypeSelectionArgs = arrayOf(
                MIME_TYPE_APK
            )
            val mimeTypeSelection = "${MediaStore.Files.FileColumns.MIME_TYPE} = ?"

            // 定义查询的Uri和列
            val uri = MediaStore.Files.getContentUri("external")
            val projection = arrayOf(MediaStore.Files.FileColumns._ID)

            // 执行查询
            val cursor = contentResolver.query(uri, projection, mimeTypeSelection, mimeTypeSelectionArgs, null)

            // 检查cursor是否包含数据
            count = cursor?.count ?: 0

            // 关闭cursor
            cursor?.close()
        }
        return count
    }

    fun Context.getMediaZipSize(): Int {
        var count = 0
        runCatching {
            val contentResolver = this.contentResolver

            val mimeTypeSelectionArgs = arrayOf(
                MIME_TYPE_ZIP
            )
            val mimeTypeSelection = "${MediaStore.Files.FileColumns.MIME_TYPE} = ?"

            // 定义查询的Uri和列
            val uri = MediaStore.Files.getContentUri("external")
            val projection = arrayOf(MediaStore.Files.FileColumns._ID)

            // 执行查询
            val cursor = contentResolver.query(uri, projection, mimeTypeSelection, mimeTypeSelectionArgs, null)

            // 检查cursor是否包含数据
            count = cursor?.count ?: 0

            // 关闭cursor
            cursor?.close()
        }
        return count
    }
}