package com.l7tech.server;

import com.l7tech.objectmodel.EntityHeader;
import com.l7tech.objectmodel.EntityManager;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.PersistentEntity;
import com.l7tech.server.event.EntityInvalidationEvent;
import com.l7tech.util.Option;
import com.l7tech.util.Functions.Unary;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.context.ApplicationListener;

public abstract class EntityCacheSupport<ET extends PersistentEntity, HT extends EntityHeader, EM extends EntityManager<ET, HT>>
   implements ApplicationListener<EntityInvalidationEvent> {
   protected final Logger logger = Logger.getLogger(this.getClass().getName());
   private final EM a;
   private final ReadWriteLock b = new ReentrantReadWriteLock();
   private boolean c = true;
   private boolean d = false;
   private final Map<Goid, ET> e = new HashMap<>();
   private static final String[] g;

   public final Option<ET> findByPrimaryKey(Goid var1) {
      this.b.readLock().lock();

      try {
         return Option.optional(this.e.get(var1));
      } finally {
         this.b.readLock().unlock();
      }
   }

   public final void onApplicationEvent(EntityInvalidationEvent var1) {
      if (this.getEntityClass().isAssignableFrom(var1.getEntityClass())) {
         this.a();
         Goid var2 = PersistentEntity.DEFAULT_GOID;

         try {
            for (Goid var6 : var1.getEntityIds()) {
               var2 = var6;
               PersistentEntity var7 = this.a.findByPrimaryKey(var6);

               try {
                  if (var7 == null) {
                     this.a(var6);
                     continue;
                  }
               } catch (FindException var9) {
                  throw a(var9);
               }

               this.a(this.readOnly((ET)var7));
            }
         } catch (FindException var10) {
            this.b();
            Logger var10000 = this.logger;
            Level var10001 = Level.WARNING;
            String var10002 = String.valueOf(var2);
            String[] var8 = g;
            var10000.log(var10001, var8[5] + var10002 + var8[4], var10);
         }
      }
   }

   protected EntityCacheSupport(EM var1) {
      this.a = (EM)var1;
   }

   protected abstract Class<ET> getEntityClass();

   protected abstract ET readOnly(ET var1);

   protected final <R> R doWithCacheReadOnly(Unary<R, Map<Goid, ET>> var1) {
      this.b.readLock().lock();

      try {
         return (R)var1.call(this.e);
      } finally {
         this.b.readLock().unlock();
      }
   }

   protected final void initializeCache() {
      // $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/server/EntityCacheSupport.b Ljava/util/concurrent/locks/ReadWriteLock;
      // 04: invokeinterface java/util/concurrent/locks/ReadWriteLock.writeLock ()Ljava/util/concurrent/locks/Lock; 1
      // 09: astore 1
      // 0a: aload 1
      // 0b: invokeinterface java/util/concurrent/locks/Lock.lock ()V 1
      // 10: aload 0
      // 11: bipush 1
      // 12: putfield com/l7tech/server/EntityCacheSupport.d Z
      // 15: aload 0
      // 16: getfield com/l7tech/server/EntityCacheSupport.a Lcom/l7tech/objectmodel/EntityManager;
      // 19: invokeinterface com/l7tech/objectmodel/EntityManager.findAll ()Ljava/util/Collection; 1
      // 1e: astore 2
      // 1f: aload 0
      // 20: getfield com/l7tech/server/EntityCacheSupport.e Ljava/util/Map;
      // 23: invokeinterface java/util/Map.clear ()V 1
      // 28: aload 2
      // 29: invokeinterface java/util/Collection.iterator ()Ljava/util/Iterator; 1
      // 2e: astore 3
      // 2f: aload 3
      // 30: invokeinterface java/util/Iterator.hasNext ()Z 1
      // 35: ifeq 7f
      // 38: aload 3
      // 39: invokeinterface java/util/Iterator.next ()Ljava/lang/Object; 1
      // 3e: checkcast com/l7tech/objectmodel/PersistentEntity
      // 41: astore 4
      // 43: getstatic com/l7tech/server/EntityCacheSupport.f Z
      // 46: ifne 69
      // 49: aload 4
      // 4b: invokeinterface com/l7tech/objectmodel/PersistentEntity.getGoid ()Lcom/l7tech/objectmodel/Goid; 1
      // 50: invokestatic com/l7tech/objectmodel/Goid.isDefault (Lcom/l7tech/objectmodel/Goid;)Z
      // 53: ifeq 69
      // 56: goto 5d
      // 59: invokestatic com/l7tech/server/EntityCacheSupport.a (Lcom/l7tech/objectmodel/FindException;)Lcom/l7tech/objectmodel/FindException;
      // 5c: athrow
      // 5d: new java/lang/AssertionError
      // 60: dup
      // 61: invokespecial java/lang/AssertionError.<init> ()V
      // 64: athrow
      // 65: invokestatic com/l7tech/server/EntityCacheSupport.a (Lcom/l7tech/objectmodel/FindException;)Lcom/l7tech/objectmodel/FindException;
      // 68: athrow
      // 69: aload 0
      // 6a: getfield com/l7tech/server/EntityCacheSupport.e Ljava/util/Map;
      // 6d: aload 4
      // 6f: invokeinterface com/l7tech/objectmodel/PersistentEntity.getGoid ()Lcom/l7tech/objectmodel/Goid; 1
      // 74: aload 4
      // 76: invokeinterface java/util/Map.put (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; 3
      // 7b: pop
      // 7c: goto 2f
      // 7f: aload 0
      // 80: bipush 0
      // 81: putfield com/l7tech/server/EntityCacheSupport.c Z
      // 84: aload 1
      // 85: invokeinterface java/util/concurrent/locks/Lock.unlock ()V 1
      // 8a: goto b6
      // 8d: astore 2
      // 8e: aload 0
      // 8f: invokevirtual com/l7tech/server/EntityCacheSupport.b ()V
      // 92: aload 0
      // 93: getfield com/l7tech/server/EntityCacheSupport.logger Ljava/util/logging/Logger;
      // 96: getstatic java/util/logging/Level.WARNING Ljava/util/logging/Level;
      // 99: getstatic com/l7tech/server/EntityCacheSupport.g [Ljava/lang/String;
      // 9c: bipush 2
      // 9d: aaload
      // 9e: aload 2
      // 9f: invokevirtual java/util/logging/Logger.log (Ljava/util/logging/Level;Ljava/lang/String;Ljava/lang/Throwable;)V
      // a2: aload 1
      // a3: invokeinterface java/util/concurrent/locks/Lock.unlock ()V 1
      // a8: goto b6
      // ab: astore 5
      // ad: aload 1
      // ae: invokeinterface java/util/concurrent/locks/Lock.unlock ()V 1
      // b3: aload 5
      // b5: athrow
      // b6: return
      // try (28 -> 39): 39 com/l7tech/objectmodel/FindException
      // try (26 -> 32): 33 com/l7tech/objectmodel/FindException
      // try (6 -> 52): 55 com/l7tech/objectmodel/FindException
      // try (6 -> 52): 69 null
      // try (55 -> 66): 69 null
      // try (69 -> 70): 69 null
   }

   private void a(ET var1) {
      if (this.logger.isLoggable(Level.FINE)) {
         this.logger.log(Level.FINE, g[0], var1.getGoid().toHexString());
      }

      Lock var2 = this.b.writeLock();
      var2.lock();

      try {
         this.e.put(var1.getGoid(), (ET)var1);
      } finally {
         var2.unlock();
      }
   }

   private void a(Goid var1) {
      Lock var2 = this.b.writeLock();
      var2.lock();

      try {
         PersistentEntity var3 = this.e.get(var1);
         if (var3 == null) {
            String[] var5 = g;
            this.logger.log(Level.FINE, var5[3], var1);
         } else {
            this.logger.log(Level.FINE, g[1], var1);
            this.e.remove(var1);
         }
      } finally {
         var2.unlock();
      }
   }

   private void a() {
      Lock var2 = this.b.readLock();
      var2.lock();

      boolean var1;
      try {
         var1 = this.d && this.c;
      } finally {
         var2.unlock();
      }

      if (var1) {
         this.initializeCache();
      }
   }

   private void b() {
      Lock var1 = this.b.writeLock();
      var1.lock();

      try {
         this.c = true;
      } finally {
         var1.unlock();
      }
   }

   static {
      String[] var5 = new String[6];
      int var3 = 0;
      String var2 = "\u0003~bM9\u0001<ek&Ym\u0010}50tA(\u0016<%bsE9\u001dxf\u007fd\u00048\bx'ds@vXi6twP$\u0016{fswG%\u001do0\u0003~bM9\u0001<ek&Ym\u0010}50tA#Xx#|sP(\u001c'fbsI\"\u000eu(w6B?\u0017qfswG%\u001d\u0016\u0003bdK?Xl)`cH,\fu(w6G,\u001bt#%\b\u007f6A#\fu2i6B\"\rr\"0pK?Xu\"05_}\u00050fyqJ\"\nu(w";
      int var4 = "\u0003~bM9\u0001<ek&Ym\u0010}50tA(\u0016<%bsE9\u001dxf\u007fd\u00048\bx'ds@vXi6twP$\u0016{fswG%\u001do0\u0003~bM9\u0001<ek&Ym\u0010}50tA#Xx#|sP(\u001c'fbsI\"\u000eu(w6B?\u0017qfswG%\u001d\u0016\u0003bdK?Xl)`cH,\fu(w6G,\u001bt#%\b\u007f6A#\fu2i6B\"\rr\"0pK?Xu\"05_}\u00050fyqJ\"\nu(w"
         .length();
      char var1 = '8';
      int var7 = -1;

      label30:
      while (true) {
         byte var10000 = 105;
         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) {
                     g = var5;
                     f = !EntityCacheSupport.class.desiredAssertionStatus();
                     return;
                  }

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

                  var2 = "8('\b3E#{&-\u0015]<:\u0015a\u0006.w/,\u0013}Ab} <\u0013g_b";
                  var4 = "8('\b3E#{&-\u0015]<:\u0015a\u0006.w/,\u0013}Ab} <\u0013g_b".length();
                  var1 = '\n';
                  var7 = -1;
            }

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

   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] ^ 117);
      }

      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 -> 47;
            case 1 -> 121;
            case 2 -> 127;
            case 3 -> 77;
            case 4 -> 36;
            case 5 -> 17;
            default -> 117;
         });
      }

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