package com.l7tech.external.assertions.gatewaygraphql.server.data;

import com.fasterxml.jackson.annotation.JsonIncludeProperties;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.l7tech.external.assertions.gatewaygraphql.server.input.EntityInput;
import com.l7tech.external.assertions.gatewaygraphql.server.input.EntityRefInput;
import com.l7tech.external.assertions.gatewaygraphql.server.util.SecretUtil;
import com.l7tech.gateway.common.security.password.SecurePassword;
import com.l7tech.gateway.common.security.password.SecurePassword.SecurePasswordType;
import com.l7tech.objectmodel.FindException;
import com.l7tech.security.keys.PemUtils;
import com.l7tech.server.security.password.SecurePasswordManager;
import com.l7tech.util.ExceptionUtils;
import graphql.GraphQLContext;
import graphql.GraphQLException;
import graphql.schema.DataFetchingEnvironment;
import java.io.IOException;
import java.security.KeyPair;
import java.text.ParseException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import lombok.Generated;

public class SecretDTO extends EntityDTO<SecurePassword> {
   @Generated
   private static final Logger a;
   protected final SecurePasswordManager entityManager;
   private String b = null;
   private static final String[] c;

   public SecretDTO(SecurePassword var1, SecurePasswordManager var2) {
      super(var1);
      this.entityManager = var2;
   }

   @Override
   protected Object createWrapperObject() {
      return new SecretDTO.SecurePasswordWrapper(this.entity, this.entityManager);
   }

   public SecurePasswordType getSecretType() {
      return this.entity.getType();
   }

   public boolean isVariableReferencable() {
      return this.entity.isUsageFromVariable();
   }

   public String getDescription() {
      return this.entity.getDescription();
   }

   public String getPublicKey() {
      try {
         if (!SecurePasswordType.PEM_PRIVATE_KEY.equals(this.entity.getType())) {
            return null;
         }
      } catch (FindException var4) {
         throw a(var4);
      }

      try {
         String var1 = new String(this.entityManager.decryptPassword(this.entity.getEncodedPassword()));
         KeyPair var2 = PemUtils.doReadKeyPair(var1);
         return PemUtils.writeKey(var2.getPublic(), true);
      } catch (FindException | ParseException | IOException var3) {
         a.log(Level.INFO, c[2] + ExceptionUtils.getMessageWithCause(var3), ExceptionUtils.getDebugException(var3));
         return null;
      }
   }

   public ZonedDateTime getLastModifiedDate() {
      return this.entity.getLastUpdateAsDate().toInstant().atZone(ZoneId.systemDefault());
   }

   public String getSecret(DataFetchingEnvironment var1) {
      if (this.b == null) {
         try {
            String var2 = new String(this.entityManager.decryptPassword(this.entity.getEncodedPassword()));
            GraphQLContext var10002 = var1.getGraphQlContext();
            String[] var3 = c;
            this.b = SecretUtil.encode(var2, (String)var10002.get(var3[0]));
         } catch (FindException | ParseException var4) {
            throw new GraphQLException(c[1], var4);
         }
      }

      return this.b;
   }

   static {
      String[] var5 = new String[3];
      int var3 = 0;
      String var2 = "_OB8hy\u000bSNO\u0015ah\fIQI8pz\u001a<y@O$~}_^DB8hy\u000b\u0015SDgtg\u001cHXQ>1e\u0010Y@M&h)\u001aTBS3a}\u001a^\u0001R/r|\r_\u0001Q+bz\bUSEj\u0019\u007fSS%c)\r_@E#\u007fn_JTC&xj_QDXj";
      int var4 = "_OB8hy\u000bSNO\u0015ah\fIQI8pz\u001a<y@O$~}_^DB8hy\u000b\u0015SDgtg\u001cHXQ>1e\u0010Y@M&h)\u001aTBS3a}\u001a^\u0001R/r|\r_\u0001Q+bz\bUSEj\u0019\u007fSS%c)\r_@E#\u007fn_JTC&xj_QDXj"
         .length();
      char var1 = 21;
      int var0 = -1;

      while (true) {
         String var10002 = b(46, a(var2.substring(++var0, var0 + var1)));
         byte var10001 = -1;
         var5[var3++] = var10002;
         if ((var0 += var1) >= var4) {
            c = var5;
            a = Logger.getLogger(SecretDTO.class.getName());
            return;
         }

         var1 = var2.charAt(var0);
      }
   }

   private static FindException a(FindException var0) {
      return var0;
   }

   private static char[] a(String var0) {
      char[] var10000 = var0.toCharArray();
      if (var10000.length < 2) {
         var10000[0] = (char)(var10000[0] ^ 81);
      }

      return var10000;
   }

   private static String b(int var0, char[] var1) {
      int var10003 = var1.length;
      int var10002 = var0;
      char[] var10001 = var1;
      int var10000 = var10003;

      for (int var2 = 0; var10000 > var2; var2++) {
         var10001[var2] = (char)(var10001[var2] ^ var10002 ^ switch (var2 % 7) {
            case 0 -> 20;
            case 1 -> 15;
            case 2 -> 15;
            case 3 -> 100;
            case 4 -> 63;
            case 5 -> 39;
            default -> 81;
         });
      }

      return new String(var10001).intern();
   }

   public static class SecretInput extends EntityInput {
      String g;
      SecurePasswordType h;
      Boolean i;
      String j;
      String k;
      String l;
      Integer m;
      String n;
      private static final String[] o;

      @Generated
      @Override
      public String getName() {
         return this.g;
      }

      @Generated
      public SecurePasswordType getSecretType() {
         return this.h;
      }

      @Generated
      public Boolean getVariableReferencable() {
         return this.i;
      }

      @Generated
      public String getSecret() {
         return this.j;
      }

      @Generated
      public String getDescription() {
         return this.k;
      }

      @Generated
      public String getChecksum() {
         return this.l;
      }

      @Generated
      public Integer getKeySize() {
         return this.m;
      }

      @Generated
      public String getLastModifiedDate() {
         return this.n;
      }

      @Generated
      public void setName(String var1) {
         this.g = var1;
      }

      @Generated
      public void setSecretType(SecurePasswordType var1) {
         this.h = var1;
      }

      @Generated
      public void setVariableReferencable(Boolean var1) {
         this.i = var1;
      }

      @Generated
      public void setSecret(String var1) {
         this.j = var1;
      }

      @Generated
      public void setDescription(String var1) {
         this.k = var1;
      }

      @Generated
      public void setChecksum(String var1) {
         this.l = var1;
      }

      @Generated
      public void setKeySize(Integer var1) {
         this.m = var1;
      }

      @Generated
      public void setLastModifiedDate(String var1) {
         this.n = var1;
      }

      @Generated
      @Override
      public String toString() {
         String var10000 = this.getName();
         String var10001 = String.valueOf(this.getSecretType());
         Boolean var10002 = this.getVariableReferencable();
         String var10003 = this.getSecret();
         String var10004 = this.getDescription();
         String var10005 = this.getChecksum();
         String var1 = this.getLastModifiedDate();
         Integer var2 = this.getKeySize();
         String var3 = var10005;
         String var4 = var10004;
         String var5 = var10003;
         Boolean var6 = var10002;
         String var7 = var10001;
         String var8 = var10000;
         String[] var9 = o;
         return var9[1] + var8 + var9[5] + var7 + var9[6] + var6 + var9[2] + var5 + var9[4] + var4 + var9[3] + var3 + var9[7] + var2 + var9[0] + var1 + ")";
      }

      @Generated
      @Override
      public boolean equals(Object var1) {
         if (var1 == this) {
            return true;
         } else if (!(var1 instanceof SecretDTO.SecretInput var2)) {
            return false;
         } else {
            if (!var2.canEqual(this)) {
               return false;
            }

            if (!super.equals(var1)) {
               return false;
            }

            Boolean var3 = this.getVariableReferencable();
            Boolean var4 = var2.getVariableReferencable();
            if (var3 == null ? var4 == null : var3.equals(var4)) {
               Integer var5 = this.getKeySize();
               Integer var6 = var2.getKeySize();
               if (var5 == null ? var6 == null : var5.equals(var6)) {
                  String var7 = this.getName();
                  String var8 = var2.getName();
                  if (var7 == null ? var8 == null : var7.equals(var8)) {
                     SecurePasswordType var9 = this.getSecretType();
                     SecurePasswordType var10 = var2.getSecretType();
                     if (var9 == null ? var10 == null : var9.equals(var10)) {
                        String var11 = this.getSecret();
                        String var12 = var2.getSecret();
                        if (var11 == null ? var12 == null : var11.equals(var12)) {
                           String var13 = this.getDescription();
                           String var14 = var2.getDescription();
                           if (var13 == null ? var14 == null : var13.equals(var14)) {
                              String var15 = this.getChecksum();
                              String var16 = var2.getChecksum();
                              if (var15 == null ? var16 == null : var15.equals(var16)) {
                                 String var17 = this.getLastModifiedDate();
                                 String var18 = var2.getLastModifiedDate();
                                 return var17 == null ? var18 == null : var17.equals(var18);
                              } else {
                                 return false;
                              }
                           } else {
                              return false;
                           }
                        } else {
                           return false;
                        }
                     } else {
                        return false;
                     }
                  } else {
                     return false;
                  }
               } else {
                  return false;
               }
            } else {
               return false;
            }
         }
      }

      @Generated
      @Override
      protected boolean canEqual(Object var1) {
         return var1 instanceof SecretDTO.SecretInput;
      }

      @Generated
      @Override
      public int hashCode() {
         byte var1 = 59;
         int var2 = super.hashCode();
         Boolean var3 = this.getVariableReferencable();
         var2 = var2 * 59 + (var3 == null ? 43 : var3.hashCode());
         Integer var4 = this.getKeySize();
         var2 = var2 * 59 + (var4 == null ? 43 : var4.hashCode());
         String var5 = this.getName();
         var2 = var2 * 59 + (var5 == null ? 43 : var5.hashCode());
         SecurePasswordType var6 = this.getSecretType();
         var2 = var2 * 59 + (var6 == null ? 43 : var6.hashCode());
         String var7 = this.getSecret();
         var2 = var2 * 59 + (var7 == null ? 43 : var7.hashCode());
         String var8 = this.getDescription();
         var2 = var2 * 59 + (var8 == null ? 43 : var8.hashCode());
         String var9 = this.getChecksum();
         var2 = var2 * 59 + (var9 == null ? 43 : var9.hashCode());
         String var10 = this.getLastModifiedDate();
         return var2 * 59 + (var10 == null ? 43 : var10.hashCode());
      }

      static {
         String[] var5 = new String[8];
         int var3 = 0;
         String var2 = "Xz'slG#\u001b>\"tvV\n0;?w\"\u001b'?(`zG* \u0015eAzP\u001c\u0011.\u0002|oF\u001a\\4*\u007fz\u000e\tXz8w|A\u000b\u0000g\u000bXz(zzP\u0005\u0007/&/\u000eXz/wlP\u001c\u001d*?{p]S\rXz8w|A\u000b\u0000\u000e2bz\u000e";
         int var4 = "Xz'slG#\u001b>\"tvV\n0;?w\"\u001b'?(`zG* \u0015eAzP\u001c\u0011.\u0002|oF\u001a\\4*\u007fz\u000e\tXz8w|A\u000b\u0000g\u000bXz(zzP\u0005\u0007/&/\u000eXz/wlP\u001c\u001d*?{p]S\rXz8w|A\u000b\u0000\u000e2bz\u000e"
            .length();
         char var1 = 19;
         int var7 = -1;

         label24:
         while (true) {
            byte var10000 = 100;
            String var10001 = var2.substring(++var7, var7 + var1);
            byte var10002 = -1;

            while (true) {
               String var8 = b(var10000, b(var10001));
               switch (var10002) {
                  case 0:
                     var5[var3++] = var8;
                     if ((var7 += var1) >= var4) {
                        o = var5;
                        return;
                     }

                     var1 = var2.charAt(var7);
                     break;
                  default:
                     var5[var3++] = var8;
                     if ((var7 += var1) < var4) {
                        var1 = var2.charAt(var7);
                        continue label24;
                     }

                     var2 = "'\u0005B\f\u0012%piIQ?\u0005*ty@Z\u000e\u0001.}n\u0018\n'\u0005_\b\u0019\u001fxq@\t";
                     var4 = "'\u0005B\f\u0012%piIQ?\u0005*ty@Z\u000e\u0001.}n\u0018\n'\u0005_\b\u0019\u001fxq@\t".length();
                     var1 = 23;
                     var7 = -1;
               }

               var10000 = 27;
               var10001 = var2.substring(++var7, var7 + var1);
               var10002 = 0;
            }
         }
      }

      private static char[] b(String var0) {
         char[] var10000 = var0.toCharArray();
         if (var10000.length < 2) {
            var10000[0] = (char)(var10000[0] ^ 10);
         }

         return var10000;
      }

      private static String b(int var0, char[] var1) {
         int var10003 = var1.length;
         int var10002 = var0;
         char[] var10001 = var1;
         int var10000 = var10003;

         for (int var2 = 0; var10000 > var2; var2++) {
            var10001[var2] = (char)(var10001[var2] ^ var10002 ^ switch (var2 % 7) {
               case 0 -> 16;
               case 1 -> 62;
               case 2 -> 47;
               case 3 -> 118;
               case 4 -> 123;
               case 5 -> 87;
               default -> 10;
            });
         }

         return new String(var10001).intern();
      }
   }

   public static class SecretPayload extends EntityMutationPayload<SecretDTO> {
      public SecretDTO getSecret() {
         return (SecretDTO)super.getItem();
      }
   }

   public static class SecretRefInput extends EntityRefInput {
      private String c;
      private static final String d;

      @Generated
      public String getName() {
         return this.c;
      }

      @Generated
      public void setName(String var1) {
         this.c = var1;
      }

      @Generated
      @Override
      public String toString() {
         return d + this.getName() + ")";
      }

      @Generated
      @Override
      public boolean equals(Object var1) {
         if (var1 == this) {
            return true;
         } else if (!(var1 instanceof SecretDTO.SecretRefInput var2)) {
            return false;
         } else {
            if (!var2.canEqual(this)) {
               return false;
            }

            if (!super.equals(var1)) {
               return false;
            }

            String var3 = this.getName();
            String var4 = var2.getName();
            return var3 == null ? var4 == null : var3.equals(var4);
         }
      }

      @Generated
      @Override
      protected boolean canEqual(Object var1) {
         return var1 instanceof SecretDTO.SecretRefInput;
      }

      @Generated
      @Override
      public int hashCode() {
         byte var1 = 59;
         int var2 = super.hashCode();
         String var3 = this.getName();
         return var2 * 59 + (var3 == null ? 43 : var3.hashCode());
      }

      static {
         String var10002 = b(99, b("MnRwr^\u007fJD\u001fVrII{\u007fc`qcUn~E-yKV{6"));
         byte var10001 = -1;
         d = var10002;
      }

      private static char[] b(String var0) {
         char[] var10000 = var0.toCharArray();
         if (var10000.length < 2) {
            var10000[0] = (char)(var10000[0] ^ 88);
         }

         return var10000;
      }

      private static String b(int var0, char[] var1) {
         int var10003 = var1.length;
         int var10002 = var0;
         char[] var10001 = var1;
         int var10000 = var10003;

         for (int var2 = 0; var10000 > var2; var2++) {
            var10001[var2] = (char)(var10001[var2] ^ var10002 ^ switch (var2 % 7) {
               case 0 -> 125;
               case 1 -> 104;
               case 2 -> 82;
               case 3 -> 102;
               case 4 -> 116;
               case 5 -> 73;
               default -> 88;
            });
         }

         return new String(var10001).intern();
      }
   }

   public static class SecretsPayload extends EntityMutationsPayload<SecretDTO> {
      public List<SecretDTO> getSecrets() {
         return super.getItems();
      }
   }

   @JsonIncludeProperties({"name", "decodedPassword", "description", "secretType", "allowedInContextVariable"})
   @JsonPropertyOrder({"name", "decodedPassword", "description", "secretType", "allowedInContextVariable"})
   public static class SecurePasswordWrapper extends SecretDTO {
      public SecurePasswordWrapper(SecurePassword var1, SecurePasswordManager var2) {
         super(var1, var2);
      }

      public String getDecodedPassword() {
         try {
            return new String(this.entityManager.decryptPassword(this.entity.getEncodedPassword()));
         } catch (Exception var2) {
            return null;
         }
      }

      public boolean isAllowedInContextVariable() {
         return this.entity.isUsageFromVariable();
      }

      @Override
      protected Object createWrapperObject() {
         return null;
      }
   }
}
