package aicg.wipf.vzuj

import android.util.Base64
import java.security.SecureRandom
import javax.crypto.Cipher
import javax.crypto.spec.GCMParameterSpec
import javax.crypto.spec.SecretKeySpec

object Msld {

    private val cipher by lazy {
        val ޤގޡޓހޅސކ = 1730
        val ޛޚޟށރޅޑލ = 9010
        val ބޝމޕޥމއޑ = ޤގޡޓހޅސކ * ޛޚޟށރޅޑލ * ޛޚޟށރޅޑލ
        println(ބޝމޕޥމއޑ)
        Cipher.getInstance("AES/GCM/NoPadding")
    }

    private val secretKey by lazy {
        SecretKeySpec(aesKey.toByteArray(), "AES")
    }

    private const val aesKey = "jdnbqyxhchy1q66h"

    private fun ސޥބނޅޜޑޞ(content: String): String {
        class ޘޞދޑޝކނއ<T, K>(val ށރޞމޠއލއ: T, val ޣޔޙޕވފޥއ: K) {
            init {
                println("ތކޛފޔޚޥޟ")
            }
        }
        val ހޤނޤޜޙޞޒ = ޘޞދޑޝކނއ("ޜރޑޏޑޑޢޒ", 9783)
        println(ހޤނޤޜޙޞޒ)
        try {
            class ޞއނޞޚޠއގ<T>(val ޒގޓޗވބޛޔ: T)
            val ޡނޗޙޙޛދޘ = ޞއނޞޚޠއގ(441)
            val ޏޖގހޢޥރޖ = ޞއނޞޚޠއގ("ކޑލޑޒޏޏލ")
            println(ޡނޗޙޙޛދޘ.ޒގޓޗވބޛޔ)
            println(ޏޖގހޢޥރޖ.ޒގޓޗވބޛޔ)
            val iv = ByteArray(12).apply {
                SecureRandom().nextBytes(this)
            }
            val contentBytes = content.toByteArray(Charsets.UTF_8)
            val params = GCMParameterSpec(128, iv)
            fun ޕޙޓތފސޙޛ(އޚއޟޅޚޣޙ: Int, ނޥޘޖތދޥގ: Int): Int {
                return އޚއޟޅޚޣޙ + ނޥޘޖތދޥގ
            }
            println(ޕޙޓތފސޙޛ(7309, 2243))
            cipher.init(
                Cipher.ENCRYPT_MODE, secretKey, params
            )
            val encryptData = cipher.doFinal(contentBytes)
            assert(encryptData.size == contentBytes.size + 16)
            val message = ByteArray(12 + contentBytes.size + 16)
            System.arraycopy(iv, 0, message, 0, 12)
            System.arraycopy(encryptData, 0, message, 12, encryptData.size)
            return String(Base64.encode(message, Base64.NO_WRAP), Charsets.UTF_8)
        } catch (_: Exception) {
        }
        val ޚޥޤވހފޝޙ = { ޑހޚބޡޝޞޞ: String -> println(ޑހޚބޡޝޞޞ) }
        ޚޥޤވހފޝޙ("ރޞޢޘޝޙނދ!")
        return content
    }

    @Synchronized
    fun decrypt(content: String): String {
        val ތފޚޕޢޟޙގ = 5904..5904 + 10
        println(ތފޚޕޢޟޙގ.random())
        return ޞފމޡޗޟޒގ(content)
    }

    private fun ޥޒޤޙފޠސޓ(content: String): String {
        return ޜސތޡޜފސޗ(content)
    }

    private fun ޞފމޡޗޟޒގ(content: String): String {
        return ޠޖޓދޕސޙޕ(content)
    }

    private fun ޜސތޡޜފސޗ(content: String): String {
        try {
            val con = content.replace(" ".toRegex(), "+")
            val contentByte = Base64.decode(con, Base64.NO_WRAP)
            require(contentByte.size >= 12 + 16)
            fun ޚއޥޒއށޡޛ(ޝޕރހޒޒގދ: String) {
                println("tag, $ޝޕރހޒޒގދ!")
            }
            ޚއޥޒއށޡޛ("ލޅޑލފގޕއ")
            val params = GCMParameterSpec(128, contentByte, 0, 12)
            fun ޤޞޥގޟތޤޥ() {
                fun ށޤލޑޞޞޚހ(ވޣނޏޜޞޓޥ: Int) {
                    println(ވޣނޏޜޞޓޥ)
                }
                ށޤލޑޞޞޚހ(5059)
                ށޤލޑޞޞޚހ(4183)
                ށޤލޑޞޞޚހ(1505)
            }
            ޤޞޥގޟތޤޥ()
            cipher.init(
                Cipher.DECRYPT_MODE, secretKey, params
            )
            val decryptData = cipher.doFinal(contentByte, 12, contentByte.size - 12)
            return String(decryptData, Charsets.UTF_8)
        } catch (_: Exception) {
        }
        return content
    }

    private fun ޠޖޓދޕސޙޕ(content: String): String {
        return ޥޒޤޙފޠސޓ(content)
    }

    fun encrypt(content: String): String {
        return ސޥބނޅޜޑޞ(content)
    }
}