package com.l7tech.external.assertions.gatewaygraphql.server.resolver.mutation;

import com.l7tech.external.assertions.gatewaygraphql.EntityTypeInfo;
import com.l7tech.external.assertions.gatewaygraphql.server.data.EntityMappingAction;
import com.l7tech.external.assertions.gatewaygraphql.server.data.EntityMappingInstruction;
import com.l7tech.external.assertions.gatewaygraphql.server.data.EntityMutationStatus;
import com.l7tech.external.assertions.gatewaygraphql.server.data.IdpEntityMutationsPayload;
import com.l7tech.external.assertions.gatewaygraphql.server.data.IdpGroupDTO;
import com.l7tech.external.assertions.gatewaygraphql.server.error.NoMutationException;
import com.l7tech.external.assertions.gatewaygraphql.server.input.IdpEntityInput;
import com.l7tech.external.assertions.gatewaygraphql.server.resolver.ResolverExecutionContext;
import com.l7tech.external.assertions.gatewaygraphql.server.resolver.query.IdpResolverHelper;
import com.l7tech.identity.Group;
import com.l7tech.identity.GroupManager;
import com.l7tech.identity.IdentityProvider;
import com.l7tech.identity.IdentityProviderType;
import com.l7tech.identity.User;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.ObjectModelException;
import com.l7tech.objectmodel.PersistentEntity;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;

public abstract class IdpGroupMutationResolver<IDP extends IdentityProvider, IDPG extends Group, IDPU extends User, IDPGO extends IdpGroupDTO<IDPG, IDP>, IDPGI extends IdpEntityInput>
   extends IdpMemberAwareMutationResolver<IDP, IDPG, IDPGO, IDPGI> {
   protected final IdpResolverHelper idpHelper;
   protected final IdentityProviderType idpType;
   private static final String[] d;

   protected IdpGroupMutationResolver(EntityTypeInfo var1, PlatformTransactionManager var2, IdpResolverHelper var3, IdentityProviderType var4) {
      super(var1, var2);
      this.idpHelper = var3;
      this.idpType = var4;
   }

   protected boolean isIntendedIdp(IdentityProvider 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 1
      // 01: ifnull 29
      // 04: aload 1
      // 05: invokeinterface com/l7tech/identity/IdentityProvider.getConfig ()Lcom/l7tech/identity/IdentityProviderConfig; 1
      // 0a: invokevirtual com/l7tech/identity/IdentityProviderConfig.type ()Lcom/l7tech/identity/IdentityProviderType;
      // 0d: invokevirtual com/l7tech/identity/IdentityProviderType.toVal ()I
      // 10: aload 0
      // 11: getfield com/l7tech/external/assertions/gatewaygraphql/server/resolver/mutation/IdpGroupMutationResolver.idpType Lcom/l7tech/identity/IdentityProviderType;
      // 14: invokevirtual com/l7tech/identity/IdentityProviderType.toVal ()I
      // 17: if_icmpne 29
      // 1a: goto 21
      // 1d: invokestatic com/l7tech/external/assertions/gatewaygraphql/server/resolver/mutation/IdpGroupMutationResolver.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 20: athrow
      // 21: bipush 1
      // 22: goto 2a
      // 25: invokestatic com/l7tech/external/assertions/gatewaygraphql/server/resolver/mutation/IdpGroupMutationResolver.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 28: athrow
      // 29: bipush 0
      // 2a: ireturn
      // try (0 -> 10): 11 com/l7tech/external/assertions/gatewaygraphql/server/error/NoMutationException
      // try (2 -> 15): 15 com/l7tech/external/assertions/gatewaygraphql/server/error/NoMutationException
   }

   protected IDP doFindIdp(String var1) throws FindException {
      return this.idpHelper.idpByName(var1, this::isIntendedIdp);
   }

   protected GroupManager<IDPU, IDPG> getGroupManager(IDP var1) {
      return var1.getGroupManager();
   }

   protected IDPGO doCreate(IDP param1, IDPGI param2) throws ObjectModelException {
      // $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: aload 2
      // 03: invokevirtual com/l7tech/external/assertions/gatewaygraphql/server/resolver/mutation/IdpGroupMutationResolver.toEntity (Lcom/l7tech/identity/IdentityProvider;Lcom/l7tech/external/assertions/gatewaygraphql/server/input/IdpEntityInput;)Lcom/l7tech/identity/Identity;
      // 06: checkcast com/l7tech/identity/Group
      // 09: astore 3
      // 0a: aload 0
      // 0b: aload 1
      // 0c: aload 2
      // 0d: aload 3
      // 0e: invokevirtual com/l7tech/external/assertions/gatewaygraphql/server/resolver/mutation/IdpGroupMutationResolver.setEntity (Lcom/l7tech/identity/IdentityProvider;Lcom/l7tech/external/assertions/gatewaygraphql/server/input/IdpEntityInput;Lcom/l7tech/identity/Identity;)V
      // 11: aload 3
      // 12: instanceof com/l7tech/identity/PersistentGroup
      // 15: ifeq 5d
      // 18: aload 2
      // 19: invokevirtual com/l7tech/external/assertions/gatewaygraphql/server/input/IdpEntityInput.getGoid ()Ljava/lang/String;
      // 1c: invokestatic org/apache/commons/lang3/StringUtils.isNotEmpty (Ljava/lang/CharSequence;)Z
      // 1f: ifeq 3e
      // 22: goto 29
      // 25: invokestatic com/l7tech/external/assertions/gatewaygraphql/server/resolver/mutation/IdpGroupMutationResolver.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 28: athrow
      // 29: aload 3
      // 2a: checkcast com/l7tech/identity/PersistentGroup
      // 2d: aload 2
      // 2e: invokevirtual com/l7tech/external/assertions/gatewaygraphql/server/input/IdpEntityInput.getGoid ()Ljava/lang/String;
      // 31: invokestatic com/l7tech/objectmodel/Goid.parseGoid (Ljava/lang/String;)Lcom/l7tech/objectmodel/Goid;
      // 34: invokevirtual com/l7tech/identity/PersistentGroup.setGoid (Lcom/l7tech/objectmodel/Goid;)V
      // 37: goto 3e
      // 3a: invokestatic com/l7tech/external/assertions/gatewaygraphql/server/resolver/mutation/IdpGroupMutationResolver.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 3d: athrow
      // 3e: getstatic com/l7tech/objectmodel/Goid.DEFAULT_GOID Lcom/l7tech/objectmodel/Goid;
      // 41: aload 3
      // 42: checkcast com/l7tech/identity/PersistentGroup
      // 45: invokevirtual com/l7tech/identity/PersistentGroup.getGoid ()Lcom/l7tech/objectmodel/Goid;
      // 48: invokevirtual com/l7tech/objectmodel/Goid.equals (Ljava/lang/Object;)Z
      // 4b: ifne 5d
      // 4e: aload 3
      // 4f: checkcast com/l7tech/identity/PersistentGroup
      // 52: invokestatic com/l7tech/objectmodel/imp/PersistentEntityUtil.preserveId (Lcom/l7tech/objectmodel/PersistentEntity;)Z
      // 55: pop
      // 56: goto 5d
      // 59: invokestatic com/l7tech/external/assertions/gatewaygraphql/server/resolver/mutation/IdpGroupMutationResolver.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 5c: athrow
      // 5d: aload 0
      // 5e: aload 1
      // 5f: invokevirtual com/l7tech/external/assertions/gatewaygraphql/server/resolver/mutation/IdpGroupMutationResolver.getGroupManager (Lcom/l7tech/identity/IdentityProvider;)Lcom/l7tech/identity/GroupManager;
      // 62: aload 3
      // 63: aconst_null
      // 64: invokeinterface com/l7tech/identity/GroupManager.save (Lcom/l7tech/identity/Group;Ljava/util/Set;)Ljava/lang/String; 3
      // 69: pop
      // 6a: aload 0
      // 6b: aload 1
      // 6c: aload 3
      // 6d: invokevirtual com/l7tech/external/assertions/gatewaygraphql/server/resolver/mutation/IdpGroupMutationResolver.toEntityDTO (Lcom/l7tech/identity/IdentityProvider;Lcom/l7tech/identity/Identity;)Lcom/l7tech/external/assertions/gatewaygraphql/server/data/IdpEntityDTO;
      // 70: checkcast com/l7tech/external/assertions/gatewaygraphql/server/data/IdpGroupDTO
      // 73: areturn
      // try (6 -> 18): 19 com/l7tech/objectmodel/ObjectModelException
      // try (14 -> 27): 28 com/l7tech/objectmodel/ObjectModelException
      // try (30 -> 40): 41 com/l7tech/objectmodel/ObjectModelException
   }

   protected IDPGO doUpdate(IDP var1, IDPGI var2, IDPG var3) throws ObjectModelException {
      try {
         this.checkEntityProtected(var2.getExecutionContext(), var3);
         if (var3 instanceof PersistentEntity) {
            this.checkEntityVersion(var2.getExecutionContext(), var2, (PersistentEntity)var3);
         }
      } catch (ObjectModelException var4) {
         throw a(var4);
      }

      label23: {
         try {
            if (var2.isFullInput()) {
               this.resetEntitySelectively((IDP)var1, (IDPGI)var2, (IDPG)var3);
               this.setEntity((IDP)var1, (IDPGI)var2, (IDPG)var3);
               break label23;
            }
         } catch (ObjectModelException var5) {
            throw a(var5);
         }

         this.resetEntityConditionally((IDP)var1, (IDPGI)var2, (IDPG)var3);
         this.setEntityConditionally((IDP)var1, (IDPGI)var2, (IDPG)var3);
      }

      this.getGroupManager((IDP)var1).update(var3);
      return this.toEntityDTO((IDP)var1, (IDPG)var3);
   }

   protected IDPGO doDelete(IDP var1, IDPGI var2, IDPG var3) throws ObjectModelException {
      this.checkEntityProtected(var2.getExecutionContext(), var3);
      this.getGroupManager((IDP)var1).delete(var3);
      return this.toEntityDTO((IDP)var1, (IDPG)var3);
   }

   protected void doUpsertSilently(IDPGI var1, ResolverExecutionContext var2, IdpEntityMutationsPayload<IDP, IDPGO> var3, TransactionStatus var4) {
      EntityMappingAction var5 = null;

      try {
         var1.setExecutionContext(var2);
         EntityMappingInstruction var6 = var2.getBundleProperties().getEntityMappingInstruction(this.entityTypeInfo, var1);
         var5 = var6.getAction();

         try {
            if (EntityMappingAction.IGNORE.equals(var5)) {
               var3.add(var5, EntityMutationStatus.IGNORED, this.toRawEntity((IDPGI)var1), null);
               return;
            }
         } catch (NoMutationException var8) {
            throw a(var8);
         }

         IdentityProvider var7 = this.doFindIdp((IDPGI)var1, var6);
         this.doUpsert((IDP)var7, (IDPGI)var1, var6, var3, var4);
      } catch (NoMutationException var9) {
         var3.add(var5, EntityMutationStatus.NONE, var9.getMessage() + d[2], this.toRawEntity((IDPGI)var1), null);
      } catch (Exception var10) {
         this.handleMutationError(var4, var10, var3, var5, this.toRawEntity((IDPGI)var1));
      }
   }

   protected void doDeleteSilently(String var1, String var2, ResolverExecutionContext var3, IdpEntityMutationsPayload<IDP, IDPGO> var4, TransactionStatus var5) {
      EntityMappingAction var6 = EntityMappingAction.DELETE;
      IdpEntityInput var7 = null;

      try {
         EntityMappingInstruction var9 = this.toEntityMappingInstruction(var1, var2);
         var7 = this.toEntityInput(var9);
         var7.setExecutionContext(var3);
         IdentityProvider var10 = this.doFindIdp((IDPGI)var7, var9);
         Group var11 = this.doFind((IDP)var10, (IDPGI)var7);

         try {
            if (var11 == null) {
               var4.add(var6, EntityMutationStatus.NONE, d[3]);
               return;
            }
         } catch (NoMutationException var13) {
            throw a(var13);
         }

         try {
            if (!this.isDeletable((IDPG)var11)) {
               var4.add(var6, EntityMutationStatus.NONE, d[1], this.toRawEntity((IDPGI)var7), this.toEntityDTO((IDP)var10, (IDPG)var11));
               return;
            }
         } catch (NoMutationException var12) {
            throw a(var12);
         }

         IdpGroupDTO var8 = this.doDelete((IDP)var10, (IDPGI)var7, (IDPG)var11);
         var5.flush();
         var4.add(var6, EntityMutationStatus.DELETED, this.toRawEntity((IDPGI)var7), var8);
      } catch (NoMutationException var14) {
         var4.add(var6, EntityMutationStatus.NONE, var14.getMessage() + d[0], this.toRawEntity((IDPGI)var7), null);
      } catch (Exception var15) {
         this.handleMutationError(var5, var15, var4, var6, this.toRawEntity((IDPGI)var7));
      }
   }

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

   static {
      String[] var5 = new String[4];
      int var3 = 0;
      String var2 = "\u001c}7\u0005\u0018,tY39B\u0002+c\u00100+\u0016\u00177o_3=v2+\f\u0012crX8~\u0007\u00187oD$~\u0016\u001e\"r\u00104-B\u0018,r\u00109;\u000e\u00137gR1;NV*a^2,\u000b\u0018$&D5;B\u001b6rQ)7\r\u0018";
      int var4 = "\u001c}7\u0005\u0018,tY39B\u0002+c\u00100+\u0016\u00177o_3=v2+\f\u0012crX8~\u0007\u00187oD$~\u0016\u001e\"r\u00104-B\u0018,r\u00109;\u000e\u00137gR1;NV*a^2,\u000b\u0018$&D5;B\u001b6rQ)7\r\u0018"
         .length();
      char var1 = 23;
      int var7 = -1;

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

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

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

                  var2 = "@!kYDp(\u0005oe\u001e^w?LlwJKk3\u0003o.(hf\u001eDp.LgkPN?.\u0004d\"[Dk3\u0018x.\u001eCx4\u0003skPM?.\u0004d\"S_k;\u0018hmP";
                  var4 = "@!kYDp(\u0005oe\u001e^w?LlwJKk3\u0003o.(hf\u001eDp.LgkPN?.\u0004d\"[Dk3\u0018x.\u001eCx4\u0003skPM?.\u0004d\"S_k;\u0018hmP".length();
                  var1 = 23;
                  var7 = -1;
            }

            var10000 = 11;
            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] ^ 81);
      }

      return var10000;
   }

   private static String c(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 -> 103;
            case 1 -> 10;
            case 2 -> 9;
            case 3 -> 53;
            case 4 -> 33;
            case 5 -> 20;
            default -> 81;
         });
      }

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