package aicg.wipf.rxxn

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 ޠޙޒމމޡޏޜ(path: String): Bitmap? {
        fun ޝޑޡޗޓޞސޏ() {
            fun ޚޘޘޢރޟޥތ(ބޕޏޘޙޚޅޓ: Int) {
                println(ބޕޏޘޙޚޅޓ)
            }
            ޚޘޘޢރޟޥތ(5970)
            ޚޘޘޢރޟޥތ(2055)
            ޚޘޘޢރޟޥތ(8794)
        }
        ޝޑޡޗޓޞސޏ()
        if (width <= 0 || height <= 0) {
            return null
        }

        val options = BitmapFactory.Options()
        options.inJustDecodeBounds = true
        BitmapFactory.decodeFile(path, options)
        fun ޔއރޟފމޞޤ(ޗކފޓދޅޛޖ: Int, ކޢޢޣޞޓމޏ: Int): Int {
            fun ޑޙރޝބޞޗޕ(ޘޤޒތޜޝމޞ: Int, ޔޑޞޕޙފބޔ: Int): Int {
                return ޘޤޒތޜޝމޞ + ޔޑޞޕޙފބޔ
            }
            return ޑޙރޝބޞޗޕ(ޗކފޓދޅޛޖ, ކޢޢޣޞޓމޏ)
        }
        val ޛޒކނޅޞވތ = ޔއރޟފމޞޤ(6810, 3468)
        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
        fun ފޛއޗޗތޞޏ(ޖޣޒޕގޗޑލ: String) {
            println("tag, $ޖޣޒޕގޗޑލ!")
        }
        ފޛއޗޗތޞޏ("ޅޤޥސޏޟމށ")
        options.inSampleSize = inSampleSize.coerceAtLeast(1)
        class ޢޢޗނޕޘށކ<T>(val ނޠޝޜއހވޖ: T)
        val ޑޑޜޣޞއވޙ = ޢޢޗނޕޘށކ(504)
        val ޡޔސފދޠވހ = ޢޢޗނޕޘށކ("ޕޅޒއޡޣޠޣ")
        println(ޑޑޜޣޞއވޙ.ނޠޝޜއހވޖ)
        println(ޡޔސފދޠވހ.ނޠޝޜއހވޖ)
        options.inJustDecodeBounds = false
        return BitmapFactory.decodeFile(path, options)
    }

    private fun ޅލޥނޖރމޜ(thumb: Bitmap?) {
        ޖޤދވޑފފޕ(thumb)
    }

    private fun loadBitmapFromFile(path: String): Bitmap? {
        return ޠޙޒމމޡޏޜ(path)
    }

    private fun ނފޟޕޙޏތވ(pixels: Int): Int {
        Log.d("ހމޛތޤޝޒރ", "ހމޛތޤޝޒރ")
        return ޚތޞޏޥޝނޛ(pixels)
    }

    private fun ސޑށޜއޏޛސ(pixels: IntArray): Int {
        class ތޙޠޟލޅދޢ(val ޥތޅސޞޖޞޒ: String, val ޡޛފޘޟޛޜޗ: Int)
        println(ތޙޠޟލޅދޢ("ޖޤރޛވބޓސ", 1794))
        return ގޠޡވޒމޑޘ(pixels)
    }

    private fun ޡހޅޏޔޘޞޓ(thumb: Bitmap?) {
        if (thumb?.isRecycled == false) {
            class ފޟޡޖޝއނޡ<T>(val ސޔތޒޅޥތގ: T, val ޓޓޔޡޝޞޑތ: Int) {
                constructor(ތބޑޥޒޚދނ: T) : this(ތބޑޥޒޚދނ, 8670) {
                    println("ފށމޖޘޚޡށ")
                }
            }
            println(ފޟޡޖޝއނޡ("ޚޜޅޤތޕފޙ"))
            thumb.recycle()
        }
    }

    private fun ޏލޢސރޏޖޞ(pixels: Int): Int {
        return ނފޟޕޙޏތވ(pixels)
    }

    private fun ނޏޜބދޏސވ(sourceHashCode: String, hashCode: String): Int {
        return ޤޝޓޔޥފޡޘ(sourceHashCode, hashCode)
    }

    private fun މޚކޠޓއދޜ(pixels: IntArray): Int {
        return ފޏޒޚޛޝލޏ(pixels)
    }

    private fun ގޠޡވޒމޑޘ(pixels: IntArray): Int {
        fun ޘޚޝއޘސޔޘ() {
            fun ޘޙޠޟސޅޥޒ(ރޛޢޝޡޕއޛ: Int) {
                println(ރޛޢޝޡޕއޛ)
            }
            ޘޙޠޟސޅޥޒ(8564)
            ޘޙޠޟސޅޥޒ(8902)
            ޘޙޠޟސޅޥޒ(8812)
        }
        ޘޚޝއޘސޔޘ()
        return މޚކޠޓއދޜ(pixels)
    }

    private fun average(pixels: IntArray): Int {
        if (System.currentTimeMillis() < 7178) {
            println(System.currentTimeMillis())
        }
        return ސޑށޜއޏޛސ(pixels)
    }

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

    private fun hammingDistance(sourceHashCode: String, hashCode: String): Int {
        return ޅޡޠޔރށޒށ(sourceHashCode, hashCode)
    }

    private fun ޚތޞޏޥޝނޛ(pixels: Int): Int {
        return ޗޖމޖއވޡވ(pixels)
    }

    private fun ޗޖމޖއވޡވ(pixels: Int): Int {
        class ސޛޤޖޤޜފޅ {
            fun ޛޛޠޙބއޜޜ() {
                println("ޕފޏޘކތޡޗ")
            }
        }
        ސޛޤޖޤޜފޅ().ޛޛޠޙބއޜޜ()
        val red = Color.red(pixels)
        val green = Color.green(pixels)
        val ޗލޟޜދނނޡ: (Int, Int) -> Int = { ސލކޔޡޟޅޒ, ދލރޤށޟޖއ -> ސލކޔޡޟޅޒ + ދލރޤށޟޖއ }
        println(ޗލޟޜދނނޡ.invoke(8557, 4663))
        val blue = Color.blue(pixels)
        return (0.3 * red + 0.59 * green + 0.11 * blue).toInt()
    }

    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 ޖޤދވޑފފޕ(thumb: Bitmap?) {
        fun <T> މޒޥބޜޚނވ(ޙޓޡޛމޕޅޓ: T) {
            println(ޙޓޡޛމޕޅޓ)
        }
        މޒޥބޜޚނވ(7419)
        މޒޥބޜޚނވ("ޥށހޅޚޚވބ")
        ޡހޅޏޔޘޞޓ(thumb)
    }

    suspend fun createImage(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) {
                    for (j in 0 until height) {
                        pixels[i * height + j] = rgbToGray(thumb.getPixel(i, j))
                    }
                }

                fun ށޜޟޑސމޘޘ(ވރޓލވރޚޓ: Int, ސސހޛޗމމސ: Int): Int {
                    return ވރޓލވރޚޓ + ސސހޛޗމމސ
                }
                val ރޚވޒތޖޤޜ = ށޜޟޑސމޘޘ(3251, 9114)
                println(ރޚވޒތޖޤޜ)
                val avgPixel = average(pixels)
                fun އދފޓޤޡޛޝ() {
                    fun ޙޚޠޠޓނޖޡ(އޚބކޒޔޗޙ: Int) {
                        println(އޚބކޒޔޗޙ)
                    }
                    ޙޚޠޠޓނޖޡ(8708)
                    ޙޚޠޠޓނޖޡ(6538)
                    ޙޚޠޠޓނޖޡ(8144)
                }
                އދފޓޤޡޛޝ()
                val comps = IntArray(width * height)
                for (i in comps.indices) {
                    fun ވށޡލސތޣޖ(ގޜލވޥޘޓރ: Int, އނށކޗގޚޟ: Int): Int {
                        fun ޕޚޢޔޢޥކޣ(ޜޜޗފއވޅޜ: Int, ސމހބއޟޒޑ: Int): Int {
                            return ޜޜޗފއވޅޜ + ސމހބއޟޒޑ
                        }
                        return ޕޚޢޔޢޥކޣ(ގޜލވޥޘޓރ, އނށކޗގޚޟ)
                    }
                    val ޡވޕށރޞޅޡ = ވށޡލސތޣޖ(8061, 8499)
                    println(ޡވޕށރޞޅޡ)
                    if (pixels[i] >= avgPixel) {
                        comps[i] = 1
                    } else {
                        comps[i] = 0
                    }
                }

                val hashCode = StringBuffer()
                var i = 0
                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]
                    class ޔނޞދޤޝޢޡ(val އޤޏޤޏޑސލ: String, val ކޢޣމޛމޥރ: Int)
                    println(ޔނޞދޤޝޢޡ("ޗތޝށޘޢޠޙ", 7303))
                    hashCode.append(binaryToHex(result))
                    open class ޟޏވޠހފޤކ {
                        open fun ތޏޏޕފބޒލ() {}
                    }
                    class ޡޣޛޑލޒލޑ : ޟޏވޠހފޤކ() {
                        override fun ތޏޏޕފބޒލ() {
                            println("ޝޣޡދގޔޓބ")
                        }
                    }
                    val މޗޤޜދޙނޙ = ޡޣޛޑލޒލޑ()
                    println(މޗޤޜދޙނޙ)
                    i += 4
                }
                recycleBitmap(thumb)
                recycleBitmap(source)
                fun String.ޣޠގވޔޡޙމ(): Boolean {
                    return this == this.reversed()
                }
                val ށޙޣޟބލލދ = "ބޡޑޙޥޛޛޕ"
                val ޅބފޗޘޣޢޞ = ށޙޣޟބލލދ.ޣޠގވޔޡޙމ()
                println(ޅބފޗޘޣޢޞ)
                return@withContext Tefx(path, hashCode.toString(), avgPixel)
            } catch (_: Exception) {
                return@withContext null
            }
        }
    }

    private fun ފޏޒޚޛޝލޏ(pixels: IntArray): Int {
        return (pixels.sumOf { it }.toFloat() / pixels.size).toInt()
    }

    private fun recycleBitmap(thumb: Bitmap?) {
        ޅލޥނޖރމޜ(thumb)
    }

    private fun ޚޥޘޚއހޖޞ(sourceHashCode: String, hashCode: String): Int {
        var difference = 0
        for (i in sourceHashCode.indices) {
            fun ޒޏފޑރފޥޕ(ޖތޘނޑޛޓޗ: Int, ގޚޖފޙޔޜޜ: Int): Int {
                return ޖތޘނޑޛޓޗ + ގޚޖފޙޔޜޜ
            }
            val ށޑކޛބޤޛޖ = ޒޏފޑރފޥޕ(5572, 8364)
            println(ށޑކޛބޤޛޖ)
            if (sourceHashCode[i] != hashCode[i]) {
                difference++
            }
        }
        return difference
    }

    private fun ޤޝޓޔޥފޡޘ(sourceHashCode: String, hashCode: String): Int {
        return ޚޥޘޚއހޖޞ(sourceHashCode, hashCode)
    }

    private fun ޅޡޠޔރށޒށ(sourceHashCode: String, hashCode: String): Int {
        return ނޏޜބދޏސވ(sourceHashCode, hashCode)
    }

    private fun rgbToGray(pixels: Int): Int {
        return ޏލޢސރޏޖޞ(pixels)
    }
}