package com.l7tech.identity;

import com.l7tech.common.io.NonCloseableOutputStream;
import com.l7tech.objectmodel.imp.ZoneableNamedEntityImp;
import com.l7tech.security.rbac.RbacAttribute;
import com.l7tech.security.types.CertificateValidationType;
import com.l7tech.util.Charsets;
import com.l7tech.util.DeserializeSafe;
import com.l7tech.util.PoolByteArrayOutputStream;
import com.l7tech.util.ResourceUtils;
import jakarta.persistence.Column;
import jakarta.persistence.DiscriminatorColumn;
import jakarta.persistence.DiscriminatorType;
import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Entity;
import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
import jakarta.persistence.Lob;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import jakarta.xml.bind.annotation.XmlRootElement;
import java.beans.XMLEncoder;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import javax.validation.constraints.Size;
import org.hibernate.annotations.Proxy;
import org.jetbrains.annotations.Nullable;

@XmlRootElement
@Entity
@Proxy(lazy = false)
@Table(name = "identity_provider")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue("1")
@DeserializeSafe
public class IdentityProviderConfig extends ZoneableNamedEntityImp {
   protected String description;
   protected IdentityProviderType type;
   private static final Logger logger;
   private static final String ADMIN_ENABLED;
   private static final String PROP_CERTIFICATE_VALIDATION_TYPE;
   private String propsXml;
   private Map<String, Object> props = new HashMap<>();
   private static final Set<String> classes;
   private static final Set<String> constructors;
   private static final Set<String> methods;
   static final long serialVersionUID = -5943250996493768674L;
   private static final String[] b;

   public IdentityProviderConfig(IdentityProviderType var1) {
      this.type = var1;
   }

   public IdentityProviderConfig() {
      this.type = IdentityProviderType.LDAP;
   }

   public IdentityProviderConfig(IdentityProviderConfig var1) {
      super(var1);
      this.type = var1.type;
   }

   @RbacAttribute
   @Size(min = 1, max = 128)
   @Transient
   public String getName() {
      return super.getName();
   }

   public IdentityProviderType type() {
      return this.type;
   }

   @Transient
   public boolean isWritable() {
      return true;
   }

   @RbacAttribute
   @Column(name = "description", length = Integer.MAX_VALUE)
   @Lob
   public String getDescription() {
      return this.description;
   }

   public void setDescription(String var1) {
      this.description = var1;
   }

   @Column(name = "properties", length = Integer.MAX_VALUE)
   @Lob
   public String getSerializedProps() throws IOException {
      // $VF: Couldn't be decompiled
      // Please report this to the Vineflower issue tracker, at https://github.com/Vineflower/vineflower/issues with a copy of the class file (if you have the rights to distribute it!)
      // java.lang.RuntimeException: parsing failure!
      //   at org.jetbrains.java.decompiler.modules.decompiler.decompose.DomHelper.parseGraph(DomHelper.java:211)
      //   at org.jetbrains.java.decompiler.main.rels.MethodProcessor.codeToJava(MethodProcessor.java:174)
      //
      // Bytecode:
      // 00: aload 0
      // 01: getfield com/l7tech/identity/IdentityProviderConfig.propsXml Ljava/lang/String;
      // 04: ifnonnull 8e
      // 07: aload 0
      // 08: getfield com/l7tech/identity/IdentityProviderConfig.props Ljava/util/Map;
      // 0b: invokeinterface java/util/Map.size ()I 1
      // 10: bipush 1
      // 11: if_icmpge 22
      // 14: goto 18
      // 17: athrow
      // 18: aload 0
      // 19: ldc ""
      // 1b: putfield com/l7tech/identity/IdentityProviderConfig.propsXml Ljava/lang/String;
      // 1e: goto 8e
      // 21: athrow
      // 22: aconst_null
      // 23: astore 1
      // 24: aconst_null
      // 25: astore 2
      // 26: new com/l7tech/util/PoolByteArrayOutputStream
      // 29: dup
      // 2a: invokespecial com/l7tech/util/PoolByteArrayOutputStream.<init> ()V
      // 2d: astore 1
      // 2e: new java/beans/XMLEncoder
      // 31: dup
      // 32: new com/l7tech/common/io/NonCloseableOutputStream
      // 35: dup
      // 36: aload 1
      // 37: invokespecial com/l7tech/common/io/NonCloseableOutputStream.<init> (Ljava/io/OutputStream;)V
      // 3a: invokespecial java/beans/XMLEncoder.<init> (Ljava/io/OutputStream;)V
      // 3d: astore 2
      // 3e: aload 2
      // 3f: ldc com/l7tech/objectmodel/Goid
      // 41: invokestatic com/l7tech/objectmodel/Goid.getPersistenceDelegate ()Ljava/beans/PersistenceDelegate;
      // 44: invokevirtual java/beans/XMLEncoder.setPersistenceDelegate (Ljava/lang/Class;Ljava/beans/PersistenceDelegate;)V
      // 47: aload 2
      // 48: new com/l7tech/identity/a
      // 4b: dup
      // 4c: aload 0
      // 4d: invokespecial com/l7tech/identity/a.<init> (Lcom/l7tech/identity/IdentityProviderConfig;)V
      // 50: invokevirtual java/beans/XMLEncoder.setExceptionListener (Ljava/beans/ExceptionListener;)V
      // 53: aload 2
      // 54: aload 0
      // 55: getfield com/l7tech/identity/IdentityProviderConfig.props Ljava/util/Map;
      // 58: invokevirtual java/beans/XMLEncoder.writeObject (Ljava/lang/Object;)V
      // 5b: aload 2
      // 5c: invokevirtual java/beans/XMLEncoder.close ()V
      // 5f: aconst_null
      // 60: astore 2
      // 61: aload 0
      // 62: aload 1
      // 63: getstatic com/l7tech/util/Charsets.UTF8 Ljava/nio/charset/Charset;
      // 66: invokevirtual com/l7tech/util/PoolByteArrayOutputStream.toString (Ljava/nio/charset/Charset;)Ljava/lang/String;
      // 69: putfield com/l7tech/identity/IdentityProviderConfig.propsXml Ljava/lang/String;
      // 6c: aload 2
      // 6d: ifnull 74
      // 70: aload 2
      // 71: invokevirtual java/beans/XMLEncoder.close ()V
      // 74: aload 1
      // 75: invokestatic com/l7tech/util/ResourceUtils.closeQuietly (Ljava/io/Closeable;)V
      // 78: goto 8e
      // 7b: astore 3
      // 7c: aload 2
      // 7d: ifnull 88
      // 80: aload 2
      // 81: invokevirtual java/beans/XMLEncoder.close ()V
      // 84: goto 88
      // 87: athrow
      // 88: aload 1
      // 89: invokestatic com/l7tech/util/ResourceUtils.closeQuietly (Ljava/io/Closeable;)V
      // 8c: aload 3
      // 8d: athrow
      // 8e: aload 0
      // 8f: getfield com/l7tech/identity/IdentityProviderConfig.propsXml Ljava/lang/String;
      // 92: areturn
      // try (3 -> 14): 14 java/io/IOException
      // try (0 -> 8): 9 java/io/IOException
      // try (19 -> 54): 61 null
      // try (62 -> 66): 67 java/io/IOException
   }

   public void recreateSerializedProps() throws IOException {
      this.propsXml = null;
      this.getSerializedProps();
   }

   public void setSerializedProps(String param1) {
      // $VF: Couldn't be decompiled
      // Please report this to the Vineflower issue tracker, at https://github.com/Vineflower/vineflower/issues with a copy of the class file (if you have the rights to distribute it!)
      // java.lang.RuntimeException: parsing failure!
      //   at org.jetbrains.java.decompiler.modules.decompiler.decompose.DomHelper.parseGraph(DomHelper.java:211)
      //   at org.jetbrains.java.decompiler.main.rels.MethodProcessor.codeToJava(MethodProcessor.java:174)
      //
      // Bytecode:
      // 00: aload 0
      // 01: aload 1
      // 02: putfield com/l7tech/identity/IdentityProviderConfig.propsXml Ljava/lang/String;
      // 05: aload 1
      // 06: ifnull 15
      // 09: aload 1
      // 0a: invokevirtual java/lang/String.length ()I
      // 0d: bipush 2
      // 0e: if_icmpge 22
      // 11: goto 15
      // 14: athrow
      // 15: aload 0
      // 16: getfield com/l7tech/identity/IdentityProviderConfig.props Ljava/util/Map;
      // 19: invokeinterface java/util/Map.clear ()V 1
      // 1e: goto 63
      // 21: athrow
      // 22: new java/io/ByteArrayInputStream
      // 25: dup
      // 26: aload 1
      // 27: invokestatic com/l7tech/util/HexUtils.encodeUtf8 (Ljava/lang/String;)[B
      // 2a: invokespecial java/io/ByteArrayInputStream.<init> ([B)V
      // 2d: astore 2
      // 2e: new com/l7tech/util/SafeXMLDecoderBuilder
      // 31: dup
      // 32: aload 2
      // 33: invokespecial com/l7tech/util/SafeXMLDecoderBuilder.<init> (Ljava/io/InputStream;)V
      // 36: new com/l7tech/util/StringClassFilter
      // 39: dup
      // 3a: getstatic com/l7tech/identity/IdentityProviderConfig.classes Ljava/util/Set;
      // 3d: getstatic com/l7tech/identity/IdentityProviderConfig.constructors Ljava/util/Set;
      // 40: getstatic com/l7tech/identity/IdentityProviderConfig.methods Ljava/util/Set;
      // 43: invokespecial com/l7tech/util/StringClassFilter.<init> (Ljava/util/Set;Ljava/util/Set;Ljava/util/Set;)V
      // 46: invokevirtual com/l7tech/util/SafeXMLDecoderBuilder.addClassFilter (Lcom/l7tech/util/ClassFilter;)Lcom/l7tech/util/SafeXMLDecoderBuilder;
      // 49: new com/l7tech/identity/b
      // 4c: dup
      // 4d: aload 0
      // 4e: invokespecial com/l7tech/identity/b.<init> (Lcom/l7tech/identity/IdentityProviderConfig;)V
      // 51: invokevirtual com/l7tech/util/SafeXMLDecoderBuilder.setExceptionListener (Ljava/beans/ExceptionListener;)Lcom/l7tech/util/SafeXMLDecoderBuilder;
      // 54: invokevirtual com/l7tech/util/SafeXMLDecoderBuilder.build ()Lcom/l7tech/util/SafeXMLDecoder;
      // 57: astore 3
      // 58: aload 0
      // 59: aload 3
      // 5a: invokevirtual com/l7tech/util/SafeXMLDecoder.readObject ()Ljava/lang/Object;
      // 5d: checkcast java/util/Map
      // 60: putfield com/l7tech/identity/IdentityProviderConfig.props Ljava/util/Map;
      // 63: return
      // try (0 -> 9): 10 java/lang/IllegalArgumentException
      // try (5 -> 15): 15 java/lang/IllegalArgumentException
   }

   @Transient
   public String getExportableSerializedProps() throws IOException {
      try {
         if (this.props.size() < 1) {
            return "";
         }
      } catch (IOException var13) {
         throw var13;
      }

      HashMap var1 = new HashMap<>(this.props);

      for (String var5 : this.getUnexportablePropKeys()) {
         var1.remove(var5);
      }

      PoolByteArrayOutputStream var14 = null;
      XMLEncoder var15 = null;

      try {
         var14 = new PoolByteArrayOutputStream();
         var15 = new XMLEncoder(new NonCloseableOutputStream(var14));
         var15.setExceptionListener(new c(this));
         var15.writeObject(var1);
         var15.close();
         var15 = null;
         return var14.toString(Charsets.UTF8);
      } finally {
         try {
            if (var15 != null) {
               var15.close();
            }
         } catch (IOException var11) {
            throw var11;
         }

         ResourceUtils.closeQuietly(var14);
      }
   }

   @RbacAttribute
   @Column(name = "type", insertable = false, updatable = false)
   public int getTypeVal() {
      return this.type.toVal();
   }

   public void setTypeVal(int var1) {
      this.type = IdentityProviderType.fromVal(var1);
   }

   @RbacAttribute
   @Transient
   public boolean isAdminEnabled() {
      // $VF: Couldn't be decompiled
      // Please report this to the Vineflower issue tracker, at https://github.com/Vineflower/vineflower/issues with a copy of the class file (if you have the rights to distribute it!)
      // java.lang.RuntimeException: parsing failure!
      //   at org.jetbrains.java.decompiler.modules.decompiler.decompose.DomHelper.parseGraph(DomHelper.java:211)
      //   at org.jetbrains.java.decompiler.main.rels.MethodProcessor.codeToJava(MethodProcessor.java:174)
      //
      // Bytecode:
      // 00: aload 0
      // 01: getstatic com/l7tech/identity/IdentityProviderConfig.b [Ljava/lang/String;
      // 04: bipush 4
      // 05: aaload
      // 06: invokevirtual com/l7tech/identity/IdentityProviderConfig.getProperty (Ljava/lang/String;)Ljava/lang/Object;
      // 09: checkcast java/lang/Boolean
      // 0c: astore 1
      // 0d: aload 1
      // 0e: ifnull 21
      // 11: aload 1
      // 12: invokevirtual java/lang/Boolean.booleanValue ()Z
      // 15: ifeq 21
      // 18: goto 1c
      // 1b: athrow
      // 1c: bipush 1
      // 1d: goto 22
      // 20: athrow
      // 21: bipush 0
      // 22: ireturn
      // try (7 -> 12): 13 java/lang/IllegalArgumentException
      // try (9 -> 16): 16 java/lang/IllegalArgumentException
   }

   public void setAdminEnabled(boolean var1) {
      this.setProperty(b[2], var1);
   }

   @Transient
   public boolean canIssueCertificates() {
      return true;
   }

   @Transient
   public boolean isCredentialsRequiredForTest() {
      return false;
   }

   @Transient
   public CertificateValidationType getCertificateValidationType() {
      CertificateValidationType var1 = null;
      String[] var3 = b;
      Object var2 = this.getProperty(var3[1]);
      if (var2 instanceof CertificateValidationType) {
         var1 = (CertificateValidationType)var2;
      } else if (var2 instanceof String) {
         var1 = this.getEnumProperty(b[0], null, CertificateValidationType.class);
      }

      return var1;
   }

   public void setCertificateValidationType(CertificateValidationType var1) {
      try {
         if (var1 == null) {
            this.setProperty(b[0], null);
            return;
         }
      } catch (IllegalArgumentException var2) {
         throw var2;
      }

      this.setProperty(b[0], var1.toString());
   }

   public final void copyFrom(IdentityProviderConfig var1) {
      this.setDescription(var1.getDescription());
      this.setName(var1.getName());
      this.setSecurityZone(var1.getSecurityZone());
      this.type = var1.type();
      this.props = var1.props;
      this.propsXml = null;
   }

   protected <T> T getProperty(String var1) {
      return (T)this.props.get(var1);
   }

   protected void setProperty(String var1, @Nullable Object var2) {
      label16: {
         try {
            if (var2 == null) {
               this.props.remove(var1);
               break label16;
            }
         } catch (IllegalArgumentException var3) {
            throw var3;
         }

         this.props.put(var1, var2);
      }

      this.propsXml = null;
   }

   protected boolean getBooleanProperty(String var1, boolean var2) {
      Boolean var3 = (Boolean)this.props.get(var1);

      try {
         if (var3 == null) {
            return var2;
         }
      } catch (IllegalArgumentException var4) {
         throw var4;
      }

      return var3;
   }

   protected <E extends Enum<E>> E getEnumProperty(String var1, E var2, Class<E> var3) {
      Enum var4 = var2;
      String var5 = (String)this.props.get(var1);
      if (var5 != null) {
         try {
            var4 = Enum.valueOf(var3, var5);
         } catch (IllegalArgumentException var7) {
         }
      }

      return (E)var4;
   }

   @Transient
   protected String[] getUnexportablePropKeys() {
      return new String[0];
   }

   static {
      String[] var5 = new String[7];
      int var3 = 0;
      String var2 = "@wJ4cQ\u0011@sL%\\V\u0014JvY4cX\u0016wkH%\u0019@wJ4cQ\u0011@sL%\\V\u0014JvY4cX\u0016wkH%\fBvU)dr\u0016BpT%n5@}Unf\u0000\fFqPnyR\u001bV`Q4s\u0019\fZb]3$t\u001dQfQ&cT\u0019Wwn!f^\u001cBfQ/dc\u0001Sw\u0010i\fBvU)dr\u0016BpT%n";
      int var4 = "@wJ4cQ\u0011@sL%\\V\u0014JvY4cX\u0016wkH%\u0019@wJ4cQ\u0011@sL%\\V\u0014JvY4cX\u0016wkH%\fBvU)dr\u0016BpT%n5@}Unf\u0000\fFqPnyR\u001bV`Q4s\u0019\fZb]3$t\u001dQfQ&cT\u0019Wwn!f^\u001cBfQ/dc\u0001Sw\u0010i\fBvU)dr\u0016BpT%n"
         .length();
      char var1 = 25;
      int var7 = -1;

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

         while (true) {
            String var14 = a(b(var10000));
            switch (var10001) {
               case 0:
                  var5[var3++] = var14;
                  if ((var7 += var1) >= var4) {
                     b = var5;
                     String[] var10 = b;
                     ADMIN_ENABLED = b[2];
                     PROP_CERTIFICATE_VALIDATION_TYPE = var10[0];
                     logger = Logger.getLogger(IdentityProviderConfig.class.getName());
                     String[] var17 = new String[1];
                     String[] var8 = b;
                     var17[0] = var8[6];
                     classes = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(var17)));
                     constructors = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(var8[3])));
                     methods = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(var8[5])));
                     return;
                  }

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

                  var2 = "@}Unf\u0000\fFqPnyR\u001bV`Q4s\u0019\fZb]3$t\u001dQfQ&cT\u0019Wwn!f^\u001cBfQ/dc\u0001Sw\u00166k[\rF]^h`V\u000eB<T!dPVpfJ)dPQ3@}Unf\u0000\fFqPnyR\u001bV`Q4s\u0019\fZb]3$t\u001dQfQ&cT\u0019Wwn!f^\u001cBfQ/dc\u0001Sw";
                  var4 = "@}Unf\u0000\fFqPnyR\u001bV`Q4s\u0019\fZb]3$t\u001dQfQ&cT\u0019Wwn!f^\u001cBfQ/dc\u0001Sw\u00166k[\rF]^h`V\u000eB<T!dPVpfJ)dPQ3@}Unf\u0000\fFqPnyR\u001bV`Q4s\u0019\fZb]3$t\u001dQfQ&cT\u0019Wwn!f^\u001cBfQ/dc\u0001Sw"
                     .length();
                  var1 = 'M';
                  var7 = -1;
            }

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

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

      return var10000;
   }

   private static String a(char[] var0) {
      int var10002 = var0.length;
      char[] var10001 = var0;
      int var10000 = var10002;

      for (int var1 = 0; var10000 > var1; var1++) {
         var10001[var1] = (char)(var10001[var1] ^ switch (var1 % 7) {
            case 0 -> 35;
            case 1 -> 18;
            case 2 -> 56;
            case 3 -> 64;
            case 4 -> 10;
            case 5 -> 55;
            default -> 120;
         });
      }

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