package com.fastest.pass.app

import android.util.Base64
import android.util.Log
import com.fastest.pass.login.data.model.UserInfo
import java.security.MessageDigest
import javax.crypto.Cipher
import javax.crypto.KeyGenerator
import javax.crypto.SecretKey
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec

class Utils {
    companion object {

        fun hashing_sha256(text: String): String {
            val bytes = MessageDigest.getInstance("SHA-256").digest(text.toByteArray())
            return bytes.joinToString("") { "%02x".format(it) }
        }

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

        fun generateKey() : SecretKey {
            val keyGen = KeyGenerator.getInstance("AES")
            keyGen.init(256)
            return keyGen.generateKey()
        }

        fun encryptData(data: String, secretKeyString: String, ivString: String, ) : String {
            val ivKey: IvParameterSpec = IvParameterSpec(ivString.toByteArray(Charsets.UTF_8))
            val secKey: SecretKey = SecretKeySpec(secretKeyString.toByteArray(Charsets.UTF_8), "AES")

            val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
            cipher.init(Cipher.ENCRYPT_MODE, secKey, ivKey)
            val encryptedData = cipher.doFinal(data.toByteArray())

            val ivBase64 = Base64.encodeToString(ivString.toByteArray(Charsets.UTF_8), Base64.DEFAULT)
            val encryptedDataBase64 = Base64.encodeToString(encryptedData, Base64.DEFAULT)
            return encryptedDataBase64
        }

        fun decryptData(ivKey: String, encryptedBase64: String, secretKeyString: String) : String {
//            val iv = Base64.decode(ivBase64, Base64.DEFAULT)
            val encryptedBytes = Base64.decode(encryptedBase64, Base64.DEFAULT)
            val ivSpec = IvParameterSpec(ivKey.toByteArray(Charsets.UTF_8))
            val secKey: SecretKey = SecretKeySpec(secretKeyString.toByteArray(Charsets.UTF_8), "AES")

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

            val decryptedData = cipher.doFinal(encryptedBytes)
            val decryptedString = String(decryptedData, Charsets.UTF_8)
            return decryptedString
        }

        fun customSaltKey(userinfo: UserInfo) : String {
            val id = userinfo.id
//            val email = userinfo.email
//            val saltKey = "${id}_${email}_${id}"
//            val filterSaltKey = saltKey.replace("[@.]".toRegex(), "")
//            val encodedSaltKey = Base64.encodeToString(filterSaltKey.toByteArray(Charsets.UTF_8), Base64.DEFAULT)
//            val salt16Length = encodedSaltKey.take(16)

            val saltKey = id.toString().repeat(16).reversed().takeLast(16)
            return saltKey
        }

        fun customSecretKey(userinfo: UserInfo) : String {
            val id = userinfo.id
            val secretKey = id.toString().repeat(16).take(16)
            return secretKey
        }
    }
}