package com.l7tech.server.service.resolution;

import com.l7tech.gateway.common.audit.AuditFactory;
import com.l7tech.gateway.common.service.PublishedService;
import com.l7tech.message.Message;
import com.l7tech.objectmodel.EntityType;
import com.l7tech.objectmodel.Goid;
import com.l7tech.util.GoidUpgradeMapper;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public abstract class NameValueServiceResolver<T> extends ServiceResolver<T> {
   protected final Map<T, Map<Goid, PublishedService>> _valueToServiceMapMap = new HashMap<>();
   protected final Map<Serializable, List<T>> serviceIdToValueListMap = new HashMap<>();
   protected final ReadWriteLock _rwlock = new ReentrantReadWriteLock(false);

   public NameValueServiceResolver(AuditFactory var1) {
      super(var1);
   }

   @Override
   public void serviceDeleted(PublishedService var1) {
      Goid var2 = var1.getGoid();
      this._rwlock.writeLock().lock();

      try {
         this.serviceIdToValueListMap.remove(var2);
         if (GoidUpgradeMapper.prefixMatches(EntityType.SERVICE, var2.getHi())) {
            this.serviceIdToValueListMap.remove(var2.getLow());
         }

         for (Map var4 : this._valueToServiceMapMap.values()) {
            var4.remove(var2);
         }
      } finally {
         this._rwlock.writeLock().unlock();
      }
   }

   @Override
   public void serviceUpdated(PublishedService var1) throws ServiceResolutionException {
      this._rwlock.writeLock().lock();

      try {
         this.serviceDeleted(var1);
         this.serviceCreated(var1);
      } finally {
         this._rwlock.writeLock().unlock();
      }
   }

   @Override
   protected void updateServiceValues(PublishedService var1, List<T> var2) {
      Goid var3 = var1.getGoid();
      this._rwlock.writeLock().lock();

      try {
         this.serviceIdToValueListMap.put(var3, var2);
         if (GoidUpgradeMapper.prefixMatches(EntityType.SERVICE, var3.getHi())) {
            this.serviceIdToValueListMap.put(var3.getLow(), var2);
         }

         for (Object var5 : var2) {
            Map var6 = this.getServiceMap((T)var5);
            var6.put(var3, var1);
         }
      } finally {
         this._rwlock.writeLock().unlock();
      }
   }

   protected List<T> getTargetValues(PublishedService var1) throws ServiceResolutionException {
      try {
         if (Goid.isDefault(var1.getGoid())) {
            return this.buildTargetValues(var1);
         }
      } catch (ServiceResolutionException var19) {
         throw a(var19);
      }

      Goid var2 = var1.getGoid();
      Lock var3 = this._rwlock.readLock();
      var3.lock();

      try {
         List var4 = this.serviceIdToValueListMap.get(var2);
         if (var4 == null) {
            var4 = this.buildTargetValues(var1);
            var3.unlock();
            var3 = null;
            this._rwlock.writeLock().lock();

            try {
               this.serviceIdToValueListMap.put(var2, var4);
               if (GoidUpgradeMapper.prefixMatches(EntityType.SERVICE, var2.getHi())) {
                  this.serviceIdToValueListMap.put(var2.getLow(), var4);
               }
            } finally {
               this._rwlock.writeLock().unlock();
            }
         }

         return var4;
      } finally {
         try {
            if (var3 != null) {
               var3.unlock();
            }
         } catch (ServiceResolutionException var17) {
            throw a(var17);
         }
      }
   }

   protected abstract T getRequestValue(Message var1) throws ServiceResolutionException;

   protected Map<Goid, PublishedService> getServiceMap(T var1) {
      Lock var2 = this._rwlock.readLock();
      var2.lock();

      try {
         Map var3 = this._valueToServiceMapMap.get(var1);
         if (var3 == null) {
            var3 = new HashMap();
            var2.unlock();
            var2 = null;
            this._rwlock.writeLock().lock();

            try {
               this._valueToServiceMapMap.put((T)var1, var3);
            } finally {
               this._rwlock.writeLock().unlock();
            }
         }

         return var3;
      } finally {
         if (var2 != null) {
            var2.unlock();
         }
      }
   }

   @Override
   public void populateResolutionParameters(Message var1, Map<String, Object> var2) throws ServiceResolutionException {
      if (this.isApplicableToMessage(var1)) {
         Object var3 = this.getRequestValue(var1);
         var2.put(this.PROP_VALUE, var3);
      }
   }

   @Override
   public Collection<Map<String, Object>> generateResolutionParameters(PublishedService var1, Collection<Map<String, Object>> var2) throws ServiceResolutionException {
      try {
         if (!this.isApplicableToConflicts()) {
            return var2;
         }
      } catch (ServiceResolutionException var3) {
         throw a(var3);
      }

      return super.generateResolutionParameters(var1, var2);
   }

   @Override
   public final Result resolve(Map<String, Object> var1, Collection<PublishedService> var2) throws ServiceResolutionException {
      try {
         if (!var1.containsKey(this.PROP_VALUE)) {
            return Result.NOT_APPLICABLE;
         }
      } catch (ServiceResolutionException var4) {
         throw a(var4);
      }

      Object var3 = var1.get(this.PROP_VALUE);
      return this.resolve((T)var3, var2);
   }

   protected Result resolve(T param1, Collection param2) throws ServiceResolutionException {
      // $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: invokevirtual com/l7tech/server/service/resolution/NameValueServiceResolver.getServiceMap (Ljava/lang/Object;)Ljava/util/Map;
      // 05: astore 3
      // 06: aload 3
      // 07: ifnull 1a
      // 0a: aload 3
      // 0b: invokeinterface java/util/Map.isEmpty ()Z 1
      // 10: ifeq 22
      // 13: goto 1a
      // 16: invokestatic com/l7tech/server/service/resolution/NameValueServiceResolver.a (Lcom/l7tech/server/service/resolution/ServiceResolutionException;)Lcom/l7tech/server/service/resolution/ServiceResolutionException;
      // 19: athrow
      // 1a: getstatic com/l7tech/server/service/resolution/Result.NO_MATCH Lcom/l7tech/server/service/resolution/Result;
      // 1d: areturn
      // 1e: invokestatic com/l7tech/server/service/resolution/NameValueServiceResolver.a (Lcom/l7tech/server/service/resolution/ServiceResolutionException;)Lcom/l7tech/server/service/resolution/ServiceResolutionException;
      // 21: athrow
      // 22: new java/util/HashSet
      // 25: dup
      // 26: invokespecial java/util/HashSet.<init> ()V
      // 29: astore 4
      // 2b: aload 3
      // 2c: invokeinterface java/util/Map.values ()Ljava/util/Collection; 1
      // 31: invokeinterface java/util/Collection.iterator ()Ljava/util/Iterator; 1
      // 36: astore 5
      // 38: aload 5
      // 3a: invokeinterface java/util/Iterator.hasNext ()Z 1
      // 3f: ifeq c0
      // 42: aload 5
      // 44: invokeinterface java/util/Iterator.next ()Ljava/lang/Object; 1
      // 49: checkcast com/l7tech/gateway/common/service/PublishedService
      // 4c: astore 6
      // 4e: aload 2
      // 4f: aload 6
      // 51: invokeinterface java/util/Collection.contains (Ljava/lang/Object;)Z 2
      // 56: ifeq bd
      // 59: aload 0
      // 5a: aload 6
      // 5c: invokevirtual com/l7tech/server/service/resolution/NameValueServiceResolver.getTargetValues (Lcom/l7tech/gateway/common/service/PublishedService;)Ljava/util/List;
      // 5f: astore 7
      // 61: aload 7
      // 63: invokeinterface java/util/List.iterator ()Ljava/util/Iterator; 1
      // 68: astore 8
      // 6a: aload 8
      // 6c: invokeinterface java/util/Iterator.hasNext ()Z 1
      // 71: ifeq bd
      // 74: aload 8
      // 76: invokeinterface java/util/Iterator.next ()Ljava/lang/Object; 1
      // 7b: astore 9
      // 7d: aload 9
      // 7f: ifnull 92
      // 82: aload 9
      // 84: aload 1
      // 85: invokevirtual java/lang/Object.equals (Ljava/lang/Object;)Z
      // 88: ifne a9
      // 8b: goto 92
      // 8e: invokestatic com/l7tech/server/service/resolution/NameValueServiceResolver.a (Lcom/l7tech/server/service/resolution/ServiceResolutionException;)Lcom/l7tech/server/service/resolution/ServiceResolutionException;
      // 91: athrow
      // 92: aload 9
      // 94: ifnonnull ba
      // 97: goto 9e
      // 9a: invokestatic com/l7tech/server/service/resolution/NameValueServiceResolver.a (Lcom/l7tech/server/service/resolution/ServiceResolutionException;)Lcom/l7tech/server/service/resolution/ServiceResolutionException;
      // 9d: athrow
      // 9e: aload 1
      // 9f: ifnonnull ba
      // a2: goto a9
      // a5: invokestatic com/l7tech/server/service/resolution/NameValueServiceResolver.a (Lcom/l7tech/server/service/resolution/ServiceResolutionException;)Lcom/l7tech/server/service/resolution/ServiceResolutionException;
      // a8: athrow
      // a9: aload 4
      // ab: aload 6
      // ad: invokeinterface java/util/Set.add (Ljava/lang/Object;)Z 2
      // b2: pop
      // b3: goto ba
      // b6: invokestatic com/l7tech/server/service/resolution/NameValueServiceResolver.a (Lcom/l7tech/server/service/resolution/ServiceResolutionException;)Lcom/l7tech/server/service/resolution/ServiceResolutionException;
      // b9: athrow
      // ba: goto 6a
      // bd: goto 38
      // c0: new com/l7tech/server/service/resolution/Result
      // c3: dup
      // c4: aload 4
      // c6: invokespecial com/l7tech/server/service/resolution/Result.<init> (Ljava/util/Set;)V
      // c9: areturn
      // try (4 -> 9): 10 com/l7tech/server/service/resolution/ServiceResolutionException
      // try (6 -> 14): 14 com/l7tech/server/service/resolution/ServiceResolutionException
      // try (48 -> 54): 55 com/l7tech/server/service/resolution/ServiceResolutionException
      // try (50 -> 59): 60 com/l7tech/server/service/resolution/ServiceResolutionException
      // try (57 -> 64): 65 com/l7tech/server/service/resolution/ServiceResolutionException
      // try (62 -> 71): 72 com/l7tech/server/service/resolution/ServiceResolutionException
   }

   protected boolean isApplicableToConflicts() {
      return false;
   }

   protected abstract boolean isApplicableToMessage(Message var1) throws ServiceResolutionException;

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