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.IdpUserDTO;
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.IdentityProvider;
import com.l7tech.identity.IdentityProviderType;
import com.l7tech.identity.PersistentUser;
import com.l7tech.identity.User;
import com.l7tech.identity.UserManager;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.IdentityHeader;
import com.l7tech.objectmodel.ObjectModelException;
import com.l7tech.objectmodel.PersistentEntity;
import java.util.HashSet;
import java.util.Set;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;

public abstract class IdpUserMutationResolver<IDP extends IdentityProvider, IDPU extends User, IDPUO extends IdpUserDTO<IDPU, IDP>, IDPUI extends IdpEntityInput>
   extends IdpMemberAwareMutationResolver<IDP, IDPU, IDPUO, IDPUI> {
   protected final IdpResolverHelper idpHelper;
   protected final IdentityProviderType idpType;
   private static final String[] g;

   protected IdpUserMutationResolver(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/IdpUserMutationResolver.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/IdpUserMutationResolver.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/IdpUserMutationResolver.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 boolean replaceGroupHeaders(IDPUI var1) {
      return false;
   }

   protected final Set<IdentityHeader> doFindGroupHeaders(IDP var1, IDPUI var2, IDPU var3) throws FindException {
      try {
         if (this.replaceGroupHeaders((IDPUI)var2)) {
            return this.doFindGroupHeaders((IDP)var1, (IDPUI)var2);
         }
      } catch (FindException var8) {
         throw a(var8);
      }

      HashSet var4 = new HashSet();
      Set var5 = this.doFindExistingGroupHeaders((IDP)var1, (IDPU)var3);

      try {
         if (var5 != null) {
            var4.addAll(var5);
         }
      } catch (FindException var7) {
         throw a(var7);
      }

      var5 = this.doFindGroupHeaders((IDP)var1, (IDPUI)var2);

      try {
         if (var5 != null) {
            var4.addAll(var5);
         }

         return var4;
      } catch (FindException var6) {
         throw a(var6);
      }
   }

   protected final Set<IdentityHeader> doFindExistingGroupHeaders(IDP var1, IDPU var2) throws FindException {
      return this.idpHelper.groupHeaders(var1, (PersistentUser)var2);
   }

   protected abstract Set<IdentityHeader> doFindGroupHeaders(IDP var1, IDPUI var2) throws FindException;

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

   protected UserManager<IDPU> getUserManager(IDP var1) {
      return var1.getUserManager();
   }

   protected IDPUO doCreate(IDP param1, IDPUI 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/IdpUserMutationResolver.toEntity (Lcom/l7tech/identity/IdentityProvider;Lcom/l7tech/external/assertions/gatewaygraphql/server/input/IdpEntityInput;)Lcom/l7tech/identity/Identity;
      // 06: checkcast com/l7tech/identity/User
      // 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/IdpUserMutationResolver.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/PersistentUser
      // 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/IdpUserMutationResolver.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 28: athrow
      // 29: aload 3
      // 2a: checkcast com/l7tech/identity/PersistentUser
      // 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/PersistentUser.setGoid (Lcom/l7tech/objectmodel/Goid;)V
      // 37: goto 3e
      // 3a: invokestatic com/l7tech/external/assertions/gatewaygraphql/server/resolver/mutation/IdpUserMutationResolver.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/PersistentUser
      // 45: invokevirtual com/l7tech/identity/PersistentUser.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/PersistentUser
      // 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/IdpUserMutationResolver.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/IdpUserMutationResolver.getUserManager (Lcom/l7tech/identity/IdentityProvider;)Lcom/l7tech/identity/UserManager;
      // 62: aload 3
      // 63: aload 0
      // 64: aload 1
      // 65: aload 2
      // 66: aload 3
      // 67: invokevirtual com/l7tech/external/assertions/gatewaygraphql/server/resolver/mutation/IdpUserMutationResolver.doFindGroupHeaders (Lcom/l7tech/identity/IdentityProvider;Lcom/l7tech/external/assertions/gatewaygraphql/server/input/IdpEntityInput;Lcom/l7tech/identity/User;)Ljava/util/Set;
      // 6a: invokeinterface com/l7tech/identity/UserManager.save (Lcom/l7tech/identity/User;Ljava/util/Set;)Ljava/lang/String; 3
      // 6f: pop
      // 70: aload 0
      // 71: aload 1
      // 72: aload 3
      // 73: invokevirtual com/l7tech/external/assertions/gatewaygraphql/server/resolver/mutation/IdpUserMutationResolver.toEntityDTO (Lcom/l7tech/identity/IdentityProvider;Lcom/l7tech/identity/Identity;)Lcom/l7tech/external/assertions/gatewaygraphql/server/data/IdpEntityDTO;
      // 76: checkcast com/l7tech/external/assertions/gatewaygraphql/server/data/IdpUserDTO
      // 79: 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 IDPUO doUpdate(IDP var1, IDPUI var2, IDPU 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, (IDPUI)var2, (IDPU)var3);
               this.setEntity((IDP)var1, (IDPUI)var2, (IDPU)var3);
               break label23;
            }
         } catch (ObjectModelException var5) {
            throw a(var5);
         }

         this.resetEntityConditionally((IDP)var1, (IDPUI)var2, (IDPU)var3);
         this.setEntityConditionally((IDP)var1, (IDPUI)var2, (IDPU)var3);
      }

      this.getUserManager((IDP)var1).update(var3, this.doFindGroupHeaders((IDP)var1, (IDPUI)var2, (IDPU)var3));
      return this.toEntityDTO((IDP)var1, (IDPU)var3);
   }

   protected IDPUO doDelete(IDP var1, IDPUI var2, IDPU var3) throws ObjectModelException {
      this.checkEntityProtected(var2.getExecutionContext(), var3);
      this.getUserManager((IDP)var1).delete(var3);
      return this.toEntityDTO((IDP)var1, (IDPU)var3);
   }

   protected void doUpsertSilently(IDPUI var1, ResolverExecutionContext var2, IdpEntityMutationsPayload<IDP, IDPUO> 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((IDPUI)var1), null);
               return;
            }
         } catch (NoMutationException var8) {
            throw a(var8);
         }

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

   protected void doDeleteSilently(String var1, String var2, ResolverExecutionContext var3, IdpEntityMutationsPayload<IDP, IDPUO> 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((IDPUI)var7, var9);
         User var11 = this.doFind((IDP)var10, (IDPUI)var7);

         try {
            if (var11 == null) {
               var4.add(var6, EntityMutationStatus.NONE, g[0], this.toRawEntity((IDPUI)var7), null);
               return;
            }
         } catch (NoMutationException var13) {
            throw a(var13);
         }

         try {
            if (!this.isDeletable((IDPU)var11)) {
               var4.add(var6, EntityMutationStatus.NONE, g[3], this.toRawEntity((IDPUI)var7), this.toEntityDTO((IDP)var10, (IDPU)var11));
               return;
            }
         } catch (NoMutationException var12) {
            throw a(var12);
         }

         IdpUserDTO var8 = this.doDelete((IDP)var10, (IDPUI)var7, (IDPU)var11);
         var5.flush();
         var4.add(var6, EntityMutationStatus.DELETED, this.toRawEntity((IDPUI)var7), var8);
      } catch (NoMutationException var14) {
         var4.add(var6, EntityMutationStatus.NONE, var14.getMessage() + g[2], this.toRawEntity((IDPUI)var7), null);
      } catch (Exception var15) {
         this.handleMutationError(var5, var15, var4, var6, this.toRawEntity((IDPUI)var7));
      }
   }

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

   static {
      String[] var5 = new String[4];
      int var3 = 0;
      String var2 = "o\u0016$>\u001c \u0019\u000b\u0019)p\u0016o\u0019C\u001a`{\u001c;\u0004_\u0006l>\u001b(\u0003D\r)p\u0015o\u0019C\u001a`s\u0007;\f_\u0016/p\u0017\u0007_)y\u001c \u001fB\u0011'>\u0006'\b\u000b\u00125j\u0013;\u0004D\u0011";
      int var4 = "o\u0016$>\u001c \u0019\u000b\u0019)p\u0016o\u0019C\u001a`{\u001c;\u0004_\u0006l>\u001b(\u0003D\r)p\u0015o\u0019C\u001a`s\u0007;\f_\u0016/p\u0017\u0007_)y\u001c \u001fB\u0011'>\u0006'\b\u000b\u00125j\u0013;\u0004D\u0011"
         .length();
      char var1 = '.';
      int var7 = -1;

      label24:
      while (true) {
         byte var10000 = 52;
         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) {
                     g = var5;
                     return;
                  }

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

                  var2 = "\u0017O9i\f0\u000fR\u00017.\u00167\u0018\u001b\u0002%z\u0003+\u0014T\u0001=}\u0000%`\u0006\u007f\tS\npk\f+\u0014O\u0016pz\n>\t\u001b\u0006#.\f0\t\u001b\u000b5b\u0007+\u001cY\u00035\"B6\u001aU\u0000\"g\f8]O\u00075.\u000f*\tZ\u001b9a\f";
                  var4 = "\u0017O9i\f0\u000fR\u00017.\u00167\u0018\u001b\u0002%z\u0003+\u0014T\u0001=}\u0000%`\u0006\u007f\tS\npk\f+\u0014O\u0016pz\n>\t\u001b\u0006#.\f0\t\u001b\u000b5b\u0007+\u001cY\u00035\"B6\u001aU\u0000\"g\f8]O\u00075.\u000f*\tZ\u001b9a\f"
                     .length();
                  var1 = 23;
                  var7 = -1;
            }

            var10000 = 36;
            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] ^ 89);
      }

      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 -> 31;
            case 1 -> 75;
            case 2 -> 116;
            case 3 -> 42;
            case 4 -> 70;
            case 5 -> 123;
            default -> 89;
         });
      }

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