using System.Security.Cryptography;
using System.Text;

namespace BukiVedi.Shared.Services
{
    public static class Cryptor
    {
        private static byte[] Key = new byte[32]
            {
                130, 189, 173, 185, 152,
                24,  60,  206, 21,  166,
                163, 255, 94,  131, 245,
                48,  58,  52,  49,  57,
                72,  85,  52,  225, 34,
                48,  64,  134, 1,   134,
                95,  46
            };
        private static byte[] IV = new byte[16]
        {
                245, 171, 219, 81,
                75,  227, 63,  152,
                139, 58,  140, 187,
                17,  111, 35,  64
        };

        public static byte[] Encrypt(byte[] data)
        {
            using (var aes = Aes.Create())
            {
                aes.KeySize = 128;
                aes.BlockSize = 128;
                aes.Padding = PaddingMode.Zeros;

                aes.Key = Key;
                aes.IV = IV;

                using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))
                {
                    return PerformCryptography(data, encryptor);
                }
            }
        }

        public static byte[] Decrypt(byte[] data)
        {
            using (var aes = Aes.Create())
            {
                aes.KeySize = 128;
                aes.BlockSize = 128;
                aes.Padding = PaddingMode.Zeros;

                aes.Key = Key;
                aes.IV = IV;

                using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))
                {
                    return PerformCryptography(data, decryptor);
                }
            }
        }

        private static byte[] PerformCryptography(byte[] data, ICryptoTransform cryptoTransform)
        {
            using (var ms = new MemoryStream())
            using (var cryptoStream = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Write))
            {
                cryptoStream.Write(data, 0, data.Length);
                cryptoStream.FlushFinalBlock();

                return ms.ToArray();
            }
        }

        private const string _passwordSalt = "Booukii@EkfasVerfaildia@";

        public static string Encrypt(string text)
        {
            var data = Encoding.UTF8.GetBytes(text);
            data = Encrypt(data);
            return Encoding.UTF8.GetString(data);
        }

        public static string Decrypt(string text)
        {
            var data = Encoding.UTF8.GetBytes(text);
            data = Decrypt(data);
            return Encoding.UTF8.GetString(data);
        }

        public static string HashPassword(string password)
        {
            var hash = new StringBuilder(); ;
            byte[] secretkeyBytes = Encoding.UTF8.GetBytes(_passwordSalt);
            byte[] inputBytes = Encoding.UTF8.GetBytes(password);
            using (var hmac = new HMACSHA512(secretkeyBytes))
            {
                byte[] hashValue = hmac.ComputeHash(inputBytes);
                foreach (var theByte in hashValue)
                {
                    hash.Append(theByte.ToString("x2"));
                }
            }
            return hash.ToString();
        }
    }
}
