package de.factoryfx.data.attribute.types;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class EncryptedString implements Comparable<EncryptedString> {
    private final String encryptedString;

    @JsonCreator
    protected EncryptedString(@JsonProperty("encryptedString")String encryptedString) {
        this.encryptedString=encryptedString;
    }

    public EncryptedString(String value, String key) {
        try {
            byte[] decodedKey = Base64.getDecoder().decode(key);
            SecretKey secKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");

            Cipher AesCipher = Cipher.getInstance("AES");
            AesCipher.init(Cipher.ENCRYPT_MODE, secKey);
            encryptedString=Base64.getEncoder().encodeToString(AesCipher.doFinal(value.getBytes(StandardCharsets.UTF_8)));
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        }
    }

    public String decrypt(String key) {
        try {
            byte[] decodedKey = Base64.getDecoder().decode(key);
            SecretKey secKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");

            Cipher AesCipher = Cipher.getInstance("AES");
            AesCipher.init(Cipher.DECRYPT_MODE, secKey);
            return new String(AesCipher.doFinal(Base64.getDecoder().decode(encryptedString.getBytes(StandardCharsets.UTF_8))), StandardCharsets.UTF_8);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
            throw new RuntimeException(e);
        }
    }

    public String getEncryptedString(){
        return encryptedString;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        EncryptedString that = (EncryptedString) o;

        return encryptedString != null ? encryptedString.equals(that.encryptedString) : that.encryptedString == null;
    }

    @Override
    public int hashCode() {
        return encryptedString != null ? encryptedString.hashCode() : 0;
    }

    @Override
    public int compareTo(EncryptedString o) {
        if (this == o) return 0;
        if (o == null)
            return 1;
        int cmp = getClass().getName().compareTo(o.getClass().getName());
        if (cmp != 0)
            return cmp;

        EncryptedString that = (EncryptedString) o;
        if (encryptedString == null) {
            if (that.encryptedString == null)
                return 0;
            return -1;
        }
        if (that.encryptedString == null)
            return 1;
        return this.encryptedString.compareTo(that.encryptedString);
    }
}

@@ -2577,16 +2577,20 @@
 String o
+ther
 ) %7B%0A    
@@ -2598,32 +2598,36 @@
    if (this == o
+ther
 ) return 0;%0A    
@@ -2627,32 +2627,36 @@
 0;%0A        if (o
+ther
  == null)%0A      
@@ -2721,16 +2721,20 @@
 pareTo(o
+ther
 .getClas
@@ -2800,60 +2800,8 @@
 p;%0A%0A
-        EncryptedString that = (EncryptedString) o;%0A
     
@@ -2843,36 +2843,37 @@
             if (
+o
 th
-at
+er
 .encryptedString
@@ -2953,20 +2953,21 @@
     if (
+o
 th
-at
+er
 .encrypt
@@ -3052,20 +3052,21 @@
 mpareTo(
+o
 th
-at
+er
 .encrypt

remove unneeded variable