package com.base.filerecoveryrecyclebin.utils

import android.graphics.BitmapFactory
import android.provider.MediaStore.Audio.Radio
import com.base.filerecoveryrecyclebin.bean.MediaBean
//import com.base.filerecoveryrecyclebin.utils.OpencvImageHelp.opencvCompareSimilar
import java.io.File
import kotlin.random.Random

object SimilarHelper {

    private val TAG = "SimilarHelper"


    fun calculateSimilar(list: List<MediaBean>): HashMap<String, ArrayList<MediaBean>> {

        val result = HashMap<String, ArrayList<MediaBean>>()
        val eachArrayList = arrayListOf<MediaBean>().apply {
            addAll(list)
        }
        val haveComperedList: ArrayList<String> = arrayListOf()

        val iterator = list.iterator()


        while (iterator.hasNext()) {
            val item = iterator.next()
            LogEx.logDebug(TAG, "next item=$item")

            LogEx.logDebug(TAG, "eachArrayList=${eachArrayList.size}")

            if (!haveComperedList.contains(item.path)) {
                val compareIterator = eachArrayList.iterator()
                while (compareIterator.hasNext()) {
                    val compareItem = compareIterator.next()
//                    LogEx.logDebug(TAG, "compareItem=$compareItem")

                    if (item.path != compareItem.path) {
//                        val percent = opencvCompareSimilar(item.path, compareItem.path)
                        val percent = similarPercent(File(item.path), File(compareItem.path))
                        if (percent > 60) {
                            if (result[item.path] == null) {
                                LogEx.logDebug(TAG, "item=$item")
                                result[item.path] = arrayListOf()
                            }
                            if (result[item.path]?.isEmpty() == true) {
                                result[item.path]?.add(item)
                            }
                            result[item.path]?.add(compareItem)
                            LogEx.logDebug(TAG, "percent=$percent  ${item.path} vs ${compareItem.path}")
                            LogEx.logDebug(TAG, "compareItem=$compareItem")
                            haveComperedList.add(compareItem.path)
                            compareIterator.remove()
                        }
                    } else {
                        compareIterator.remove()
                    }
                }
            }

        }
        return result
    }


    //    private fun similarPercent(srcFile: File, compareFile: File): Int {
//        var similarityScore: Double = 0.00
//        try {
//            val bitmap1 = BitmapFactory.decodeFile(srcFile.absolutePath)
//            val bitmap2 = BitmapFactory.decodeFile(compareFile.absolutePath)
//
//            if (bitmap1.width != bitmap2.width || bitmap1.height != bitmap2.height) {
//                return 0 // 如果尺寸不同，则直接返回0
//            }
//
//            var totalDifference = 0L
//            for (x in 0 until bitmap1.width) {
//                for (y in 0 until bitmap1.height) {
//                    val pixel1 = bitmap1.getPixel(x, y)
//                    val pixel2 = bitmap2.getPixel(x, y)
//                    val redDifference = (pixel1 shr 16 and 0xff) - (pixel2 shr 16 and 0xff)
//                    val greenDifference = (pixel1 shr 8 and 0xff) - (pixel2 shr 8 and 0xff)
//                    val blueDifference = (pixel1 and 0xff) - (pixel2 and 0xff)
//                    totalDifference += (redDifference * redDifference + greenDifference * greenDifference + blueDifference * blueDifference)
//                }
//            }
//
//            // 计算相似度得分
//            val maxDifference = bitmap1.width * bitmap1.height * 3 * 255 * 255
//            similarityScore = (1 - totalDifference.toDouble() / maxDifference) * 100
//
//        } catch (e: Exception) {
//            e.printStackTrace()
//        }
//
//        // 将相似度得分四舍五入到整数
//        return similarityScore.toInt().coerceIn(0, 100)
//    }
    fun similarPercent(srcFile: File, compareFile: File): Int {
        val bitmap1 = BitmapFactory.decodeFile(srcFile.absolutePath)
        val bitmap2 = BitmapFactory.decodeFile(compareFile.absolutePath)

        if (bitmap1.width != bitmap2.width || bitmap1.height != bitmap2.height) {
            return 0 // 如果尺寸不同，则直接返回0
        }

        val width = bitmap1.width
        val height = bitmap1.height
        val pixels1 = IntArray(width * height)
        val pixels2 = IntArray(width * height)

        bitmap1.getPixels(pixels1, 0, width, 0, 0, width, height)
        bitmap2.getPixels(pixels2, 0, width, 0, 0, width, height)

        var totalDifference = 0L
        for (i in pixels1.indices) {
            val pixel1 = pixels1[i]
            val pixel2 = pixels2[i]
            if (pixel1 != pixel2) {
                val redDifference = (pixel1 shr 16 and 0xff) - (pixel2 shr 16 and 0xff)
                val greenDifference = (pixel1 shr 8 and 0xff) - (pixel2 shr 8 and 0xff)
                val blueDifference = (pixel1 and 0xff) - (pixel2 and 0xff)
                totalDifference += (redDifference * redDifference + greenDifference * greenDifference + blueDifference * blueDifference)
            }
        }

        val maxDifference = width * height * 3 * 255 * 255
        val similarityScore = (1 - totalDifference.toDouble() / maxDifference) * 100

        return similarityScore.toInt().coerceIn(0, 100)
    }

}