import * as CryptoJS from 'crypto-js';
import { JSEncrypt } from 'jsencrypt';
export class securityFunctions {

    static yek = "$uPer@dm1n~dI@z7";
    static sayek = "$uper@dmin~diaZ1"
    static savi = "$uper@dmin~diaZ1"
    static rsa_public_key = `-----BEGIN PUBLIC KEY-----
    MIICITANBgkqhkiG9w0BAQEFAAOCAg4AMIICCQKCAgB0rRSHGZK/FtK15IRfQxtl
    dN5ToPtfXGzTNBe1vGhow5xqp6BLubMKKPwCj3zOJiAm1Ip1VNqqYxWnaL8ZErBF
    rErtE0r7kn50QgGgJCpdrWJ4unhaoe1su207d9oY6D6OBH90v1aXA6ZjN7zTJmiq
    FJZOOTQE6t/XwNAOphq/B6Lcr1iQSQmSqaMozy4OQ4NvIzNTQn5tcs9qJBMsKFeU
    cSp8VK76JBNEqSuEHNmbV2d/IQ5XAqVUiAs34kH2xWRSbBmy0WK3c3QKGzC1MRWM
    9JauLN56TKUFOmUSjfWY3caJru6HxsiMTGKnvflXHfgA2aLAPxu72pIHoCTy5Hxj
    AUoKM7qXUn/kQj4Ci0M/0LRF4boqd37wpbyVNeXNvqm+dLjxklf/+NHMfNL2Wvb4
    ekHUVI8nMVPgswIcQ7q/Njf9DC+LA+/duf8m9JDjI3J6r9y1gQugdkLETGzF2Gj1
    ToU0Pocrg6CFHImqq2z+cOeWnURrV0f0I1UWWGCb79OZxZ5oDjM9uCLRsoe8+Npg
    ACZNf4AbnM7OUNj5TEYs6t/tUYouvTB7gH452I3EH6KWBl+ZTgCg0RFkOMazNOjn
    4J0yfbaPvpVgKzF2WRyn63mTO2tdLRl5s9uqMFhXmCs6IzEqBpxMKL1aS6ONZLDG
    5qPtpCPJnUFQEBTVlSO15QIDAQAB
    -----END PUBLIC KEY-----`
    /**
     * encrypting text using cryptoJS with 128/8 key size 
     * used CBC mode 
     * used Pkcs7 padding
     * returning encrypted text
     * @param plainText text to be encrypted
     */
    //encrypt text using AES 
    public static encryptText(plainText) {
        var key = CryptoJS.enc.Utf8.parse(this.yek);
        var iv = CryptoJS.enc.Utf8.parse(this.yek);

        var encryptedText = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(plainText), key,
            {
                keySize: 128 / 8,
                iv: iv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.Pkcs7
            }).toString();
        return encryptedText;
    }

    /**
     * decryption method for decrypt text that was decrypted in encrypt text method
     * @param cipherText text to be encrypt
     */
    //decrypt cypher text using AES
    public static decryptText(cipherText) {
        var key = CryptoJS.enc.Utf8.parse(this.yek);
        var iv = CryptoJS.enc.Utf8.parse(this.yek);

        var decryptedText = CryptoJS.AES.decrypt(cipherText, key,
            {
                keySize: 128 / 8,
                iv: iv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.Pkcs7
            }).toString(CryptoJS.enc.Utf8);
        return decryptedText;
    }

    public static setLocalStoage(key: string, value) {
        localStorage.setItem(key, this.encryptText(value));
    }

    public static getLocalStoage(key: string) {
        var value = localStorage.getItem(key);
        if (value) {
            return this.decryptText(value);
        }
    }

    public static setSessionStorage(key: string, value) {
        sessionStorage.setItem(key, this.encryptText(value));
    }

    public static getSessionStorage(key: string) {
        var value = sessionStorage.getItem(key);
        if (value) {
            return this.decryptText(value);
        }
    }
    public static resetLocalStoage(key: string) {
        localStorage.removeItem(key);
    }
    public static resetSessionStoage(key: string) {
        sessionStorage.removeItem(key);
    }
    public static getSaUrlKey(userId) {
        var key = CryptoJS.enc.Utf8.parse(this.sayek);
        var iv = CryptoJS.enc.Utf8.parse(this.savi);

        var encryptedText = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(userId), key,
            {
                keySize: 128 / 8,
                iv: iv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.Pkcs7
            }).toString();
        return encryptedText;
    }

    public static DecryptReq(data) {
        var key = CryptoJS.enc.Utf8.parse(this.sayek);
        var iv = CryptoJS.enc.Utf8.parse(this.savi);

        // encrypt data using AES
        var encrypted = CryptoJS.AES.decrypt(data, key, { iv: iv });

        return encrypted.toString(CryptoJS.enc.Utf8)
    }

    /**
     * method to add restrict  for priority
     * 
     */
    checkLength(e, input) {
        const functionalKeys = ['Backspace', 'ArrowRight', 'ArrowLeft'];
        if (functionalKeys.indexOf(e.key) !== -1) {
            return;
        }
        const keyValue = +e.key;
        if (isNaN(keyValue)) {
            e.preventDefault();
            return;
        }
        let newValue;
        newValue = input.value + keyValue.toString();
        if (+newValue > 100 || +newValue == 0 || newValue.length > 3) {
            e.preventDefault();
        }
    }
    public static EncryptReq(data) {
        var key = CryptoJS.enc.Utf8.parse(this.sayek);
        var iv = CryptoJS.enc.Utf8.parse(this.savi);

        // encrypt data using AES
        var encrypted = CryptoJS.AES.encrypt(data, key, { iv: iv });

        // convert encrypted data to base64 string for transport
        var encryptedBase64 = encrypted.toString();
        return encryptedBase64
    }

    public static encryptRSA(publicKey: string, jsonString: string) {
        const encryptor = new JSEncrypt();
        encryptor.setPublicKey(publicKey);

        const encrypted = encryptor.encrypt(jsonString);

        return encrypted;
    }
}
