package com.l7tech.policy.validator;

import com.l7tech.objectmodel.GuidBasedEntityManager;
import com.l7tech.policy.AssertionLicense;
import com.l7tech.policy.AssertionPath;
import com.l7tech.policy.IncludeAssertionDereferenceTranslator;
import com.l7tech.policy.Policy;
import com.l7tech.policy.PolicyPathBuilderFactory;
import com.l7tech.policy.PolicyPathResult;
import com.l7tech.policy.PolicyValidator;
import com.l7tech.policy.PolicyValidatorResult;
import com.l7tech.policy.assertion.Assertion;
import com.l7tech.policy.assertion.AssertionTranslator;
import com.l7tech.policy.assertion.CurrentAssertionTranslator;
import com.l7tech.policy.assertion.Include;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.composite.CompositeAssertion;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jetbrains.annotations.Nullable;

public abstract class AbstractPolicyValidator implements PolicyValidator {
   protected final GuidBasedEntityManager<Policy> policyFinder;
   private final PolicyPathBuilderFactory a;
   private static final String[] c;

   protected AbstractPolicyValidator(GuidBasedEntityManager<Policy> var1, PolicyPathBuilderFactory var2) {
      this.policyFinder = var1;
      this.a = var2;
   }

   @Override
   public PolicyValidatorResult validate(@Nullable Assertion var1, PolicyValidationContext var2, AssertionLicense var3) throws InterruptedException {
      try {
         return CurrentAssertionTranslator.doWithAssertionTranslator(this.getAssertionTranslator(), new a(this, var1, var2, var3));
      } catch (InterruptedException var5) {
         throw var5;
      } catch (RuntimeException var6) {
         throw var6;
      } catch (Exception var7) {
         throw new RuntimeException(var7);
      }
   }

   protected PolicyValidatorResult validateWithCurrentAssertionTranslator(@Nullable Assertion var1, PolicyValidationContext var2, AssertionLicense var3) throws InterruptedException {
      try {
         if (var1 != null) {
            var1.treeChanged();
         }
      } catch (InterruptedException var6) {
         throw a(var6);
      }

      PolicyValidatorResult var4 = this.a(var1);

      try {
         if (!var4.getErrors().isEmpty()) {
            return var4;
         }
      } catch (InterruptedException var5) {
         throw a(var5);
      }

      this.doValidation(var1, var2, var3, var4);
      return var4;
   }

   protected AssertionTranslator getAssertionTranslator() {
      try {
         if (this.policyFinder == null) {
            return null;
         }
      } catch (RuntimeException var1) {
         throw a(var1);
      }

      return new IncludeAssertionDereferenceTranslator(this.policyFinder);
   }

   protected void doValidation(Assertion var1, PolicyValidationContext var2, AssertionLicense var3, PolicyValidatorResult var4) throws InterruptedException {
      PolicyPathResult var5 = null;

      try {
         var5 = this.a.makePathBuilder().generate(var1);
      } catch (PolicyAssertionException var8) {
         var4.addError(new PolicyValidatorResult.Error(var8.getAssertion(), var8.getMessage(), var8));
      }

      if (var5 != null) {
         for (AssertionPath var7 : var5.paths()) {
            this.validatePath(var7, var2, var3, var4);
         }
      }
   }

   public abstract void validatePath(AssertionPath var1, PolicyValidationContext var2, AssertionLicense var3, PolicyValidatorResult var4) throws InterruptedException;

   @Override
   public void checkForCircularIncludes(String var1, String var2, @Nullable Assertion var3, PolicyValidatorResult var4) {
      HashMap var5 = new HashMap();
      var5.put(var1, var2);
      this.a(var3, var5, var4);
   }

   // $VF: Could not verify finally blocks. A semaphore variable has been added to preserve control flow.
   // 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!)
   private void a(@Nullable Assertion var1, Map<String, String> var2, PolicyValidatorResult var3) {
      if (var1 instanceof CompositeAssertion var4) {
         Iterator var5 = var4.children();

         while (var5.hasNext()) {
            Assertion var6 = (Assertion)var5.next();
            this.a(var6, var2, var3);
         }
      } else if (var1 instanceof Include var17) {
         String var18 = var17.getPolicyGuid();
         if (var2.keySet().contains(var18)) {
            String var10003 = (String)var2.get(var18);
            String[] var9 = c;
            PolicyAssertionException var19 = new PolicyAssertionException(var17, var9[2] + var10003);
            var3.addError(new PolicyValidatorResult.Error(var17, var19.getMessage(), var19));
         } else {
            Policy var20 = var17.retrieveFragmentPolicy();
            if (var20 == null) {
               try {
                  var20 = this.policyFinder.findByGuid(var17.getPolicyGuid());
                  if (var20 == null) {
                     return;
                  }
               } catch (Exception var14) {
                  return;
               }
            }

            var2.put(var20.getGuid(), var20.getName());
            String var10001 = String.valueOf(var20.getGoid());
            String[] var21 = c;
            var2.put(var21[1] + var10001, var20.getName());
            boolean var13 = false /* VF: Semaphore variable */;

            label79: {
               try {
                  var13 = true;
                  this.a(var20.getAssertion(), var2, var3);
                  var13 = false;
                  break label79;
               } catch (IOException var15) {
                  var13 = false;
               } finally {
                  if (var13) {
                     var2.remove(var20.getGuid());
                     var2.remove(c[0] + var20.getGoid());
                  }
               }

               var2.remove(var20.getGuid());
               var10001 = String.valueOf(var20.getGoid());
               var21 = c;
               var2.remove(var21[0] + var10001);
               return;
            }

            var2.remove(var20.getGuid());
            var2.remove(var21[0] + var20.getGoid());
         }
      }
   }

   private PolicyValidatorResult a(@Nullable Assertion var1) {
      PolicyValidatorResult var2 = new PolicyValidatorResult();
      List var3 = this.a.makePathBuilder().preProcessIncludeFragments(var1);
      if (!var3.isEmpty()) {
         for (PolicyAssertionException var5 : var3) {
            var2.addError(new PolicyValidatorResult.Error(var5.getAssertion(), var5.getMessage(), var5));
         }
      }

      return var2;
   }

   public static boolean isX509CredentialSource(Assertion param0) {
      // $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: invokevirtual com/l7tech/policy/assertion/Assertion.isCredentialSource ()Z
      // 04: ifne 0d
      // 07: bipush 0
      // 08: ireturn
      // 09: invokestatic com/l7tech/policy/validator/AbstractPolicyValidator.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 0c: athrow
      // 0d: aload 0
      // 0e: instanceof com/l7tech/policy/assertion/xmlsec/RequireWssX509Cert
      // 11: ifne 30
      // 14: aload 0
      // 15: instanceof com/l7tech/policy/assertion/xmlsec/SecureConversation
      // 18: ifne 30
      // 1b: goto 22
      // 1e: invokestatic com/l7tech/policy/validator/AbstractPolicyValidator.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 21: athrow
      // 22: aload 0
      // 23: instanceof com/l7tech/policy/assertion/SslAssertion
      // 26: ifeq 36
      // 29: goto 30
      // 2c: invokestatic com/l7tech/policy/validator/AbstractPolicyValidator.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 2f: athrow
      // 30: bipush 1
      // 31: ireturn
      // 32: invokestatic com/l7tech/policy/validator/AbstractPolicyValidator.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 35: athrow
      // 36: aload 0
      // 37: invokevirtual com/l7tech/policy/assertion/Assertion.meta ()Lcom/l7tech/policy/assertion/AssertionMetadata;
      // 3a: getstatic com/l7tech/policy/validator/AbstractPolicyValidator.c [Ljava/lang/String;
      // 3d: bipush 3
      // 3e: aaload
      // 3f: invokeinterface com/l7tech/policy/assertion/AssertionMetadata.get (Ljava/lang/String;)Ljava/lang/Object; 2
      // 44: checkcast com/l7tech/util/Functions$Unary
      // 47: astore 1
      // 48: aload 1
      // 49: ifnonnull 52
      // 4c: bipush 0
      // 4d: ireturn
      // 4e: invokestatic com/l7tech/policy/validator/AbstractPolicyValidator.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 51: athrow
      // 52: aload 1
      // 53: aload 0
      // 54: invokeinterface com/l7tech/util/Functions$Unary.call (Ljava/lang/Object;)Ljava/lang/Object; 2
      // 59: checkcast java/util/Set
      // 5c: astore 2
      // 5d: aload 2
      // 5e: ifnull 7c
      // 61: aload 2
      // 62: getstatic com/l7tech/policy/validator/ValidatorFlag.GATHERS_X509_CREDENTIALS Lcom/l7tech/policy/validator/ValidatorFlag;
      // 65: invokeinterface java/util/Set.contains (Ljava/lang/Object;)Z 2
      // 6a: ifeq 7c
      // 6d: goto 74
      // 70: invokestatic com/l7tech/policy/validator/AbstractPolicyValidator.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 73: athrow
      // 74: bipush 1
      // 75: goto 7d
      // 78: invokestatic com/l7tech/policy/validator/AbstractPolicyValidator.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 7b: athrow
      // 7c: bipush 0
      // 7d: ireturn
      // try (0 -> 5): 5 java/lang/RuntimeException
      // try (7 -> 13): 14 java/lang/RuntimeException
      // try (10 -> 19): 20 java/lang/RuntimeException
      // try (16 -> 24): 24 java/lang/RuntimeException
      // try (34 -> 38): 38 java/lang/RuntimeException
      // try (45 -> 51): 52 java/lang/RuntimeException
      // try (47 -> 56): 56 java/lang/RuntimeException
   }

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

   static {
      String[] var5 = new String[4];
      int var3 = 0;
      String var2 = "RSN\\B&eF\u0006\tRSN\\B&eF\u0006";
      int var4 = "RSN\\B&eF\u0006\tRSN\\B&eF\u0006".length();
      char var1 = '\t';
      int var7 = -1;

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

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

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

                  var2 = ";\u000f\n\f\u000ei7\nF\b\u0000\u0017l5\u0001F\u0011\u0001\u0018i#\u001c\u0003X\t\u0014wv(\t\u0014\u0006\u0018|v\u001b\b\t\u0014\u0006\u0018|\u0000\u0019\n\u0011\u000b\u001aq9\n \u0014\u000e\u001cv\u0010\u0019\u0005\f\u0000\t|";
                  var4 = ";\u000f\n\f\u000ei7\nF\b\u0000\u0017l5\u0001F\u0011\u0001\u0018i#\u001c\u0003X\t\u0014wv(\t\u0014\u0006\u0018|v\u001b\b\t\u0014\u0006\u0018|\u0000\u0019\n\u0011\u000b\u001aq9\n \u0014\u000e\u001cv\u0010\u0019\u0005\f\u0000\t|"
                     .length();
                  var1 = '#';
                  var7 = -1;
            }

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

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

      return var10000;
   }

   private static String a(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 -> 8;
            case 1 -> 22;
            case 2 -> 8;
            case 3 -> 31;
            case 4 -> 11;
            case 5 -> 117;
            default -> 38;
         });
      }

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