package aicg.wipf.vzuj

import aicg.wipf.ogbw.ezlb.rgmw.Tefx
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.media.ThumbnailUtils
import android.util.Log
import com.blankj.utilcode.util.ScreenUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlin.math.pow

object Wopj {

    private val height by lazy {
        ScreenUtils.getScreenHeight().coerceAtMost(1280)
    }

    private val width by lazy {
        ScreenUtils.getScreenWidth().coerceAtMost(1024)
    }

    private fun ނށއޡޞވނހ(thumb: Bitmap?) {
        ޅޠއޔމޔޥކ(thumb)
    }

    private suspend fun ބނޘޕޥލޘލ(path: String): Tefx? {
        return withContext(Dispatchers.IO) {
            try {
                val width = 8
                val height = 8
                val source = loadBitmapFromFile(path)
                val thumb = ThumbnailUtils.extractThumbnail(source, width, height)
                val pixels = IntArray(width * height)
                for (i in 0 until width) {
                    class ޏސލޗޣމޔޑ<T>(val ޡއގހޗޏޏޔ: T)
                    val ޚނޠގޒޤލޞ = ޏސލޗޣމޔޑ(7022)
                    val ޖޥޚނނޞށޗ = ޏސލޗޣމޔޑ("ޔޡނޞޑޚޢޞ")
                    println(ޚނޠގޒޤލޞ.ޡއގހޗޏޏޔ)
                    println(ޖޥޚނނޞށޗ.ޡއގހޗޏޏޔ)
                    for (j in 0 until height) {
                        fun ޒޣޤޝޤނއނ(ޑޝޡޗޤޗށޞ: String) {
                            println("tag, $ޑޝޡޗޤޗށޞ!")
                        }
                        ޒޣޤޝޤނއނ("ރޒފޢޚޑކޗ")
                        pixels[i * height + j] = rgbToGray(thumb.getPixel(i, j))
                    }
                }

                val avgPixel = average(pixels)
                val comps = IntArray(width * height)
                fun String.ސއޏޢސޏޑވ(): Boolean {
                    return this == this.reversed()
                }
                val ވކޕޘޚހގޗ = "ޗޓނޅޥޘޙޥ"
                val ލޢޢޗޘޠޜރ = ވކޕޘޚހގޗ.ސއޏޢސޏޑވ()
                println(ލޢޢޗޘޠޜރ)
                for (i in comps.indices) {
                    if (pixels[i] >= avgPixel) {
                        comps[i] = 1
                    } else {
                        fun ޑޖބޟޖއނޣ(ޠކސޜހޣބލ: String) {
                            println("tag, $ޠކސޜހޣބލ!")
                        }
                        ޑޖބޟޖއނޣ("ޖޢތޒދށޘޡ")
                        comps[i] = 0
                    }
                }

                val hashCode = StringBuffer()
                var i = 0
                val ޑޡށލމޠޑބ = { ފޗރޚޗޒޝޓ: String -> println(ފޗރޚޗޒޝޓ) }
                ޑޡށލމޠޑބ("ށޜފސތގނޑ!")
                while (i < comps.size) {
                    val result = comps[i] * 2.0.pow(3.0).toInt() + (comps[i + 1] * 2.0.pow(2.0).toInt()) + (comps[i + 2] * 2.0.pow(1.0).toInt()) + comps[i + 3]
                    hashCode.append(binaryToHex(result))
                    i += 4
                }
                recycleBitmap(thumb)
                recycleBitmap(source)
                class ތށޜޓޟނޥޤ<T>(val ޓށލޠޔޙސމ: T, val ޟޘޗސޔޔރހ: Int) {
                    constructor(ޓށލޠޔޙސމ: T) : this(ޓށލޠޔޙސމ, 3698) {
                        println("ޟޘޗސޔޔރހ")
                    }
                }
                println(ތށޜޓޟނޥޤ("ޙޏޗޜޚޞޝޛ"))
                return@withContext Tefx(path, hashCode.toString(), avgPixel)
            } catch (_: Exception) {
                return@withContext null
            }
        }
    }

    private fun loadBitmapFromFile(path: String): Bitmap? {
        return ށޏޡދޝޔޢޖ(path)
    }

    private fun ޕށޥލފވމޥ(path: String): Bitmap? {
        fun ޔޛޅޝރޥނޖ(ށރވޠޗހޠސ: String) {
            println("tag, $ށރވޠޗހޠސ!")
        }
        ޔޛޅޝރޥނޖ("ވޑޡޏށލޣގ")
        if (width <= 0 || height <= 0) {
            return null
        }

        val options = BitmapFactory.Options()
        class ލފޙލފގކރ<T>(val ތށބޞޛލޜބ: T)
        val ޢޠހކޠއޏރ = ލފޙލފގކރ(8613)
        val ޣޘދޟރޙޏދ = ލފޙލފގކރ("ލޤޤދޚޙލޛ")
        println(ޢޠހކޠއޏރ)
        println(ޣޘދޟރޙޏދ)
        options.inJustDecodeBounds = true
        BitmapFactory.decodeFile(path, options)
        class ޑޗށލޟޢޞޠ {
            val ޝޑޝގޟޘޓޚ by lazy {
                println("ބތޙޘޝޟޙޣ")
                "ޑޒޡޑޢޕޗޕ!"
            }
        }
        val ހޅޟހޡފކބ = ޑޗށލޟޢޞޠ()
        println(ހޅޟހޡފކބ.ޝޑޝގޟޘޓޚ)
        if (options.outHeight == -1 || options.outWidth == -1) {
            return null
        }

        var inSampleSize = (0.5f + options.outHeight.toFloat() / height.toFloat()).coerceAtLeast(0.5f + options.outWidth.toFloat() / width.toFloat()).toInt()
        inSampleSize += 1
        options.inSampleSize = inSampleSize.coerceAtLeast(1)
        Log.d("ޟޖޑޅބޒޜޒ", "ޟޖޑޅބޒޜޒ")
        options.inJustDecodeBounds = false
        class ޞޞޓޟޖޢތޓ(val ސޛޣޏނޒވސ: String, val ޖލށޔޏޑރސ: Int)
        println(ޞޞޓޟޖޢތޓ("ޣޜޏޏނޝޔޘ", 9983))
        return BitmapFactory.decodeFile(path, options)
    }

    suspend fun createImage(path: String): Tefx? {
        return ބނޘޕޥލޘލ(path)
    }

    private fun ޒވދޘޢޡޗދ(sourceHashCode: String, hashCode: String): Int {
        class ށޟއޥޥޑސޥ<T>(ރޙޗގޛޟޚމ: T) {
            init {
                println("ތޑޚޚޚއޕށ: $ރޙޗގޛޟޚމ")
            }
        }
        println(ށޟއޥޥޑސޥ("ޙޔޤޢޛޚނޝ"))
        var difference = 0
        val ޢބޟޤޙޠޥޚ = { ޚޘރޢޘލޝމ: String -> println(ޚޘރޢޘލޝމ) }
        ޢބޟޤޙޠޥޚ("ޘޜޑވޥފބޝ!")
        for (i in sourceHashCode.indices) {
            if (sourceHashCode[i] != hashCode[i]) {
                Log.d("ކޟޕޚތޏޤމ", "ކޟޕޚތޏޤމ")
                difference++
            }
        }
        return difference
    }

    private fun binaryToHex(binary: Int) = when (binary) {
        0 -> '0'
        1 -> '1'
        2 -> '2'
        3 -> '3'
        4 -> '4'
        5 -> '5'
        6 -> '6'
        7 -> '7'
        8 -> '8'
        9 -> '9'
        10 -> 'a'
        11 -> 'b'
        12 -> 'c'
        13 -> 'd'
        14 -> 'e'
        15 -> 'f'
        else -> ' '
    }

    private fun ޛޗއޜއނގޔ(sourceHashCode: String, hashCode: String): Int {
        fun ސވޑކނގޑސ(ޙޜޑޝޟޣދތ: Int, ޢއޠޥވޜނދ: Int): Int {
            return ޙޜޑޝޟޣދތ + ޢއޠޥވޜނދ
        }
        val ހޡޥބސޕމސ = ސވޑކނގޑސ(7818, 2921)
        println(ހޡޥބސޕމސ)
        return ޒވދޘޢޡޗދ(sourceHashCode, hashCode)
    }

    private fun ޅޠއޔމޔޥކ(thumb: Bitmap?) {
        ތރޔޡޞޞޥޗ(thumb)
    }

    private fun ނޤޏޢތއށނ(sourceHashCode: String, hashCode: String): Int {
        open class ލލސކލޛސޚ {
            open fun ތޟޛޏކޙޑތ() {}
        }
        class ޓޢއށގޘރޘ : ލލސކލޛސޚ() {
            override fun ތޟޛޏކޙޑތ() {
                println("ޚޠއފޠރޔޗ")
            }
        }
        val ޘމޚޚޕތޛޡ = ޓޢއށގޘރޘ()
        println(ޘމޚޚޕތޛޡ)
        return ޛޗއޜއނގޔ(sourceHashCode, hashCode)
    }

    private fun ށޏޡދޝޔޢޖ(path: String): Bitmap? {
        return ފދރޜޒޙޜޣ(path)
    }

    private fun ޣޕމޢހޢރޟ(pixels: Int): Int {
        class ރޅރޡޡސބޛ<T>(val ޔޤޜޙޣޅލދ: T, val ލޗޛޙއމލޔ: Int) {
            constructor(ގއށޟބގތޚ: T) : this(ގއށޟބގތޚ, 5392) {
                println("ޤށޑހޞކޢޥ")
            }
        }
        println(ރޅރޡޡސބޛ("ޏޏޓޖވޢޝޓ"))
        return ޙޒދޡބރޝޑ(pixels)
    }

    private fun ފދރޜޒޙޜޣ(path: String): Bitmap? {
        return ޕށޥލފވމޥ(path)
    }

    private fun average(pixels: IntArray): Int {
        fun އޔގޣޛޚޣޏ() {
            fun ފޏބޣޞހޡދ(ށޜސގށކޠއ: Int) {
                println(ށޜސގށކޠއ)
            }
            ފޏބޣޞހޡދ(383)
            ފޏބޣޞހޡދ(2894)
            ފޏބޣޞހޡދ(8621)
        }
        އޔގޣޛޚޣޏ()
        return ޓހތކތޥޝއ(pixels)
    }

    private fun ރޤބޑބޝޞޓ(pixels: Int): Int {
        return ނޅޕޞއޤޣލ(pixels)
    }

    private fun ކޤވޗޔބޝބ(sourceHashCode: String, hashCode: String): Int {
        return ނޤޏޢތއށނ(sourceHashCode, hashCode)
    }

    private fun ޓހތކތޥޝއ(pixels: IntArray): Int {
        return (pixels.sumOf { it }.toFloat() / pixels.size).toInt()
    }

    private fun ތރޔޡޞޞޥޗ(thumb: Bitmap?) {
        open class ލޑޝޜނޡޢޛ {
            open fun ނޝމކޢތވޒ() {}
        }
        class ބޝޕޟށޢޕލ : ލޑޝޜނޡޢޛ() {
            override fun ނޝމކޢތވޒ() {
                println("ޞޤޠޑގބކޘ")
            }
        }
        val ޅޣރޏޖމގޥ = ބޝޕޟށޢޕލ()
        println(ޅޣރޏޖމގޥ)
        if (thumb?.isRecycled == false) {
            thumb.recycle()
        }
    }

    private fun recycleBitmap(thumb: Bitmap?) {
        ޡޗޜށޙޠޗޜ(thumb)
    }

    private fun ޡޗޜށޙޠޗޜ(thumb: Bitmap?) {
        ނށއޡޞވނހ(thumb)
    }

    private fun ނޅޕޞއޤޣލ(pixels: Int): Int {
        return ޣޕމޢހޢރޟ(pixels)
    }

    private fun hammingDistance(sourceHashCode: String, hashCode: String): Int {
        class ޜމޛޖޏޣޅލ<T, K>(val ށޞޝރއބޔޖ: T, val ށށޏޡޙށއތ: K) {
            init {
                println("ޓދގޤކޕޕވ")
            }
        }
        val ރޑޖތޝބޚގ = ޜމޛޖޏޣޅލ("ގޔޢސފތޗކ", 5682)
        println(ރޑޖތޝބޚގ)
        return ކޤވޗޔބޝބ(sourceHashCode, hashCode)
    }

    private fun rgbToGray(pixels: Int): Int {
        return ރޤބޑބޝޞޓ(pixels)
    }

    private fun ޙޒދޡބރޝޑ(pixels: Int): Int {
        val red = Color.red(pixels)
        if (System.currentTimeMillis() < 5393) {
            println(System.currentTimeMillis())
        }
        val green = Color.green(pixels)
        fun ޏޗނޛޓޤށލ(ޜޕހށސޅމރ: String) {
            println("tag, $ޜޕހށސޅމރ!")
        }
        ޏޗނޛޓޤށލ("ގޝޠޥޏޙޓޓ")
        val blue = Color.blue(pixels)
        return (0.3 * red + 0.59 * green + 0.11 * blue).toInt()
    }

    fun similarCondition(first: Tefx, second: Tefx): Boolean {
        return hammingDistance(first.hashCode, second.hashCode) <= 6 && (first.avgPixel.toFloat() / second.avgPixel) in 0.8..1.2
    }
}