package com.xm.test.myfilemaster.util

import android.annotation.SuppressLint
import android.content.ContentResolver
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.provider.MediaStore
import android.util.TypedValue
import com.xm.test.myfilemaster.model.FileBean
import java.io.File

object MediaUtil {

    fun cropImage(imagePath: String, desiredWidth: Int, desiredHeight: Int, context: Context): Bitmap {
        val desiredHeight_ = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            desiredHeight.toFloat(),
            context.resources.displayMetrics
        ).toInt()
        val desiredWidth_ = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            desiredWidth.toFloat(),
            context.resources.displayMetrics
        ).toInt()
        // 从图片路径中加载 Bitmap
        val options = BitmapFactory.Options()
        options.inJustDecodeBounds = true
        BitmapFactory.decodeFile(imagePath, options)

        // 计算采样率以便加载图片时节省内存
        options.inSampleSize = calculateInSampleSize(options, desiredWidth_, desiredHeight_)

        // 加载 Bitmap
        options.inJustDecodeBounds = false
        val bitmap = BitmapFactory.decodeFile(imagePath, options)

        // 裁剪 Bitmap
        return Bitmap.createScaledBitmap(bitmap, desiredWidth_, desiredHeight_, false)
    }

    fun calculateInSampleSize(options: BitmapFactory.Options, desiredWidth: Int, desiredHeight: Int): Int {
        val height = options.outHeight
        val width = options.outWidth
        var inSampleSize = 1

        if (height > desiredHeight || width > desiredWidth) {
            val halfHeight = height / 2
            val halfWidth = width / 2
            while ((halfHeight / inSampleSize) >= desiredHeight && (halfWidth / inSampleSize) >= desiredWidth) {
                inSampleSize *= 2
            }
        }
        return inSampleSize
    }

    @SuppressLint("Range")
    fun getRecentImages(context: Context, limit: Int): ArrayList<FileBean> {
//        val images = mutableListOf<String>()
        val images = ArrayList<FileBean>()
        val contentResolver: ContentResolver = context.contentResolver
        val projection = arrayOf(MediaStore.Images.Media.DATA)
        val sortOrder = "${MediaStore.Images.Media.DATE_MODIFIED} DESC"
        val cursor = contentResolver.query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            projection,
            null,
            null,
            sortOrder
        )
        cursor?.use {
            var count = 0
            while (it.moveToNext() && count < limit) {
                val fileBean = FileBean()
                val imagePath = it.getString(it.getColumnIndex(MediaStore.Images.Media.DATA))
//                images.add(imagePath)
                val file = File(imagePath)
                fileBean.fileName = file.name
                fileBean.filePath = file.absolutePath
                fileBean.fileType = FileUtil.getFileType(file)
//                fileBean.fileBitmap = BitmapFactory.decodeFile(imagePath)
                fileBean.drawable = getDrawable(context, imagePath)
                images.add(fileBean)
                count++
            }
        }
        cursor?.close()
        return images
    }

//    fun findSimilarImages(baseImagePath: String, imageDirectoryPath: String, threshold: Double): List<String> {
//        val similarImages = mutableListOf<String>()
//
//        val baseImage = File(baseImagePath)
//        val baseImageHash = ImageHash.getHash(baseImage)
//
//        val imageDirectory = File(imageDirectoryPath)
//        val imageFiles = imageDirectory.listFiles { file -> file.isFile }
//
//        imageFiles?.let {
//            for (imageFile in it) {
//                val imageHash = ImageHash.getHash(imageFile)
//                val similarity = ImageHash.getSimilarity(baseImageHash, imageHash)
//                if (similarity >= threshold) {
//                    similarImages.add(imageFile.absolutePath)
//                }
//            }
//        }
//
//        return similarImages
//    }

    fun getDrawable(context: Context,filePath: String): Drawable {
        return BitmapDrawable(context.resources, filePath)
    }



}