package com.mytatvarnsdk.utils

import android.util.Base64
import java.security.MessageDigest
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec


object EncryptionUtil {


//    fun testFunction() {
//        try {
//
//
//            val encryptionKey = getEncryptionKey(
//                env = TATVA_ENVIRONMENT.PROD,
//                prodEncKey = TatvaEncryptionConfig.PROD_ENC_KEY,
//                stagEncKey = TatvaEncryptionConfig.STAGE_ENC_KEY
//            )
//
//            val encryptionIv = getEncryptionIv(
//                env = TATVA_ENVIRONMENT.PROD,
//                prodEncIv = TatvaEncryptionConfig.PROD_ENC_IV,
//                stagEncIv = TatvaEncryptionConfig.STAGE_ENC_IV
//            )
//
//            val message = "Chinmay"
//
//            val encryptedString = getEncryptedText(data = message, encryptionKey, encryptionIv)
//
//            println("encryptedString: $encryptedString")
//
//            val decryptedString = getDecryptedData(encryptedString, encryptionKey, encryptionIv)
//
//            println("decryptedString: $decryptedString")
//
//        } catch (e: Exception) {
//            e.printStackTrace()
//        }
//    }

    fun getEncryptionKey(env: TATVA_ENVIRONMENT, prodEncKey: String, stagEncKey: String): String {
        return if (env == TATVA_ENVIRONMENT.PROD) prodEncKey else stagEncKey;
    }

    fun getEncryptionIv(env: TATVA_ENVIRONMENT, prodEncIv: String, stagEncIv: String): String {
        return if (env == TATVA_ENVIRONMENT.PROD) prodEncIv else stagEncIv;
    }


    fun getEncryptedText(
        data: String,
        encryptionKey: String,
        encryptionIv: String
    ): String {

        val truncatedKey = truncateKey(sha256(encryptionKey))
        val secretKeySpec = SecretKeySpec(truncatedKey.toByteArray(Charsets.UTF_8), "AES")
        val ivParameterSpec = IvParameterSpec(encryptionIv.toByteArray(Charsets.UTF_8))

        val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec)

        val encryptedBytes: ByteArray = cipher.doFinal(data.toByteArray(Charsets.UTF_8))
        return Base64.encodeToString(encryptedBytes, Base64.NO_WRAP)
    }

    private fun sha256(input: String): String {
        val digest = MessageDigest.getInstance("SHA-256")
        val hash = digest.digest(input.toByteArray(Charsets.UTF_8))
        return hash.joinToString("") { "%02x".format(it) }
    }

    private fun truncateKey(fullKey: String): String {
        return fullKey.take(32) // Truncate to 32 characters
    }


    fun getDecryptedData(
        cipherText: String,
        encryptionKey: String,
        encryptionIv: String,
    ): String {

        val truncatedKey = truncateKey(sha256(encryptionKey))
        val secretKeySpec = SecretKeySpec(truncatedKey.toByteArray(Charsets.UTF_8), "AES")
        val ivParameterSpec = IvParameterSpec(encryptionIv.toByteArray(Charsets.UTF_8))

        val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec)

        val decryptedBytes =
            cipher.doFinal(Base64.decode(cipherText, Base64.NO_WRAP))
        return String(decryptedBytes, Charsets.UTF_8)
    }
}
