package com.zxhy.hfilemanagermaster.knife

import android.annotation.SuppressLint
import android.content.ContentResolver
import android.content.ContentUris
import android.content.Context
import android.database.Cursor
import android.media.MediaMetadataRetriever
import android.net.Uri
import android.provider.MediaStore
import android.text.format.Formatter
import com.zxhy.hfilemanagermaster.data.MediaDataC
import com.zxhy.hfilemanagermaster.data.MediaDataC_TYPE_VIDEO
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale


/**
 * MediaStore访问最新媒体
 */
fun Context.recentPhoto(): ArrayList<Uri> {
    val list = arrayListOf<Uri>()
    var cursor: Cursor? = null
    // 查询照片的Uri和字段
    val uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    val projection = arrayOf(MediaStore.Images.Media._ID, MediaStore.Images.Media.DISPLAY_NAME)
    //DESC 降列
    //ASC 升序
    val sortOrder = MediaStore.Images.ImageColumns._ID + " DESC"
    try {
        // 执行查询
        val contentResolver: ContentResolver = contentResolver
        cursor = contentResolver.query(uri, projection, null, null, sortOrder)

        // 遍历结果
        if (cursor != null && cursor.moveToFirst()) {
            do {
                // 获取照片的ID和名称
                val id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID))
//                val name =
//                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME))

                // 根据ID构建照片的Uri
                val photoUri = ContentUris.withAppendedId(uri, id)
//            println(photoUri)
                list.add(photoUri)
                // 在此处进行照片的操作，例如显示、复制、删除等
                // ...
                if (list.size == 10) {
                    break
                }
            } while (cursor.moveToNext())
        } else {
            println("无数据")
        }
    } catch (e: Exception) {
        e.printStackTrace()
    } finally {
        cursor?.close()
    }
    return list
}


fun Context.largePhoto(): ArrayList<MediaDataC> {
    val list = arrayListOf<MediaDataC>()
    var cursor: Cursor? = null
    // 查询照片的Uri和字段
    val uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    val projection = arrayOf(MediaStore.Images.Media._ID, MediaStore.Images.Media.DISPLAY_NAME)
    //DESC 降列
    //ASC 升序
    val sortOrder = MediaStore.Images.ImageColumns._ID + " DESC"
    try {
        // 执行查询
        val contentResolver: ContentResolver = contentResolver
        cursor = contentResolver.query(uri, projection, null, null, sortOrder)

        // 遍历结果
        if (cursor != null && cursor.moveToFirst()) {
            do {
                // 获取照片的ID和名称
                val id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID))
//                val name =
//                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME))

                // 根据ID构建照片的Uri
                val photoUri = ContentUris.withAppendedId(uri, id)
                //地址
                val filePath = getFilePathByUri(this, photoUri) ?: ""
//            println(photoUri)
                list.add(MediaDataC(uri = photoUri, path = filePath))
                // 在此处进行照片的操作，例如显示、复制、删除等
                // ...
                if (list.size == 10) {
                    break
                }
            } while (cursor.moveToNext())
        } else {
            println("无数据")
        }
    } catch (e: Exception) {
        e.printStackTrace()
    } finally {
        cursor?.close()
    }
    return list
}

//@SuppressLint("SimpleDateFormat")
//fun Context.largeVideo(): ArrayList<MediaDataC> {
//    val list = arrayListOf<MediaDataC>()
//    var cursor: Cursor? = null
//    // 查询视频的Uri和字段
//    val uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
//    val projection = arrayOf(
//        MediaStore.Video.Media._ID,
//        MediaStore.Video.Media.DISPLAY_NAME,
//        MediaStore.Video.Media.SIZE,
//    )
//    try {
//        // 执行查询
//        val contentResolver: ContentResolver = contentResolver
//        cursor = contentResolver.query(uri, projection, null, null, null)
//
//        // 遍历结果
//        if (cursor != null && cursor.moveToFirst()) {
//            do {
//                // 获取视频的ID
//                val id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID))
//                //名称
//                val name =
//                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME))
//                //大小
//                val size =
//                    cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE))
//                val sizeS = Formatter.formatFileSize(this, size)
//
//                // 根据ID构建视频的Uri
//                val videoUri = ContentUris.withAppendedId(uri, id)
//
//                //时间
//                val filePath = getFilePathByUri(this, videoUri) ?: ""
//                val time = File(filePath).lastModified()
////                val timeS =
////                    SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time)
//                val timeE = SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH).format(time)
//                list.add(
//                    MediaDataC(
//                        name = name,
//                        size = sizeS,
//                        time = timeE,
//                        MediaDataC_TYPE_VIDEO,
//                        uri = videoUri,
//                        path = filePath
//                    )
//                )
//                // 在此处进行视频的操作，例如显示、复制、删除等
//                // ...
//                if (list.size == 10) {
//                    break
//                }
//            } while (cursor.moveToNext())
//        } else {
//            println("无数据")
//        }
//    } catch (e: Exception) {
//        e.printStackTrace()
//    } finally {
//        cursor?.close()
//    }
//    return list
//}

//无效
fun getMediaCreateTime(videoPath: String) {
    val retriever = MediaMetadataRetriever()
    try {
        retriever.setDataSource(videoPath)
        var creationTime =
            retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE) ?: "0"
        // 如果需要将UNIX时间戳转换为可读的日期格式
        val date = Date(creationTime.toLong() * 1000)
        val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH)
        creationTime = sdf.format(date)
        // 输出或使用创建时间
        println(creationTime)
    } catch (e: Exception) {
        e.printStackTrace()
    } finally {
        retriever.release()
    }
}