package com.zxhy.hfilemanagermaster.knife

import android.content.ContentResolver
import android.content.ContentUris
import android.content.Context
import android.database.Cursor
import android.os.Environment
import android.provider.MediaStore
import com.zxhy.hfilemanagermaster.data.MediaDataC
import com.zxhy.hfilemanagermaster.data.file2MediaDataC
import java.io.File
import java.text.SimpleDateFormat
import java.util.Locale

fun Context.dupImageMedia(): 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) ?: ""
                val md5 = File(filePath).digestMd5().byteArrayToHexString()
                val time = File(filePath).lastModified()
                val timeE = SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH).format(time)
//            println(photoUri)
                list.add(MediaDataC(uri = photoUri, path = filePath, time = timeE, md5 = md5))
                // 在此处进行照片的操作，例如显示、复制、删除等
                // ...
//                if (list.size == 20) {
//                    break
//                }
            } while (cursor.moveToNext())
        } else {
            println("无数据")
        }
    } catch (e: Exception) {
        e.printStackTrace()
    } finally {
        cursor?.close()
    }
    return list
}

fun Context.dupImageStore(): ArrayList<MediaDataC> {
    val list = arrayListOf<MediaDataC>()
    try {
        val root = Environment.getExternalStorageDirectory()
        traverseImageFolder(root) { file ->
            val md5 = file.digestMd5().byteArrayToHexString()
            list.add(file.file2MediaDataC(this, md5 = md5))
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return list
}

//获取重复图片
fun getDupImageList(list: List<MediaDataC>): List<List<MediaDataC>> {
    val dupList = arrayListOf<List<MediaDataC>>()
    val origList = list.toMutableList()

    list.forEach { data ->
        val dup = origList.filter { it.md5 == data.md5 }
        if (dup.size > 1) {
            dupList.add(dup)
            origList.removeAll(dup)
        }
    }

    return dupList
}