package com.l7tech.server;

import com.l7tech.gateway.common.LicenseManager;
import com.l7tech.server.util.ApplicationEventProxy;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.logging.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;

public abstract class LifecycleBean implements Lifecycle, InitializingBean, ApplicationContextAware, ServerComponentLifecycle, DisposableBean {
   private final Logger a;
   private final String b;
   private final String c;
   private boolean d;
   private final ReadWriteLock e;
   private ApplicationContext f;
   private LicenseManager g;
   private static final String[] R;

   public void afterPropertiesSet() throws Exception {
      this.init();
   }

   public void setApplicationContext(ApplicationContext var1) throws BeansException {
      if (this.f == null) {
         this.f = var1;
         ApplicationEventProxy var2 = (ApplicationEventProxy)var1.getBean(R[1], ApplicationEventProxy.class);
         var2.addApplicationListener(new aq(this));
      }
   }

   public boolean isLicensed() {
      boolean var1 = false;

      try {
         if (this.c != null && !this.g.isFeatureEnabled(this.c)) {
            return var1;
         }
      } catch (BeansException var2) {
         throw b(var2);
      }

      return true;
   }

   @Override
   public final void start() throws LifecycleException {
      try {
         if (this.isStarted()) {
            return;
         }
      } catch (LifecycleException var4) {
         throw b(var4);
      }

      if (this.isLicensed()) {
         this.e.writeLock().lock();

         try {
            if (!this.isStarted()) {
               this.doStart();
               this.d = true;
               return;
            }
         } finally {
            this.e.writeLock().unlock();
         }
      }
   }

   @Override
   public final void stop() throws LifecycleException {
      try {
         if (!this.isStarted()) {
            return;
         }
      } catch (LifecycleException var5) {
         throw b(var5);
      }

      this.e.writeLock().lock();

      try {
         if (this.isStarted()) {
            this.doStop();
            this.d = false;
            return;
         }
      } finally {
         this.e.writeLock().unlock();
      }
   }

   @Override
   public void close() throws LifecycleException {
      this.doClose();
   }

   public void destroy() throws Exception {
      this.stop();
      this.close();
   }

   protected boolean isStartingByAnotherThread() {
      // $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/LifecycleBean.d Z
      // 04: ifne 3a
      // 07: aload 0
      // 08: getfield com/l7tech/server/LifecycleBean.e Ljava/util/concurrent/locks/ReadWriteLock;
      // 0b: checkcast java/util/concurrent/locks/ReentrantReadWriteLock
      // 0e: invokevirtual java/util/concurrent/locks/ReentrantReadWriteLock.isWriteLocked ()Z
      // 11: ifeq 3a
      // 14: goto 1b
      // 17: invokestatic com/l7tech/server/LifecycleBean.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 1a: athrow
      // 1b: aload 0
      // 1c: getfield com/l7tech/server/LifecycleBean.e Ljava/util/concurrent/locks/ReadWriteLock;
      // 1f: checkcast java/util/concurrent/locks/ReentrantReadWriteLock
      // 22: invokevirtual java/util/concurrent/locks/ReentrantReadWriteLock.writeLock ()Ljava/util/concurrent/locks/ReentrantReadWriteLock$WriteLock;
      // 25: invokevirtual java/util/concurrent/locks/ReentrantReadWriteLock$WriteLock.isHeldByCurrentThread ()Z
      // 28: ifne 3a
      // 2b: goto 32
      // 2e: invokestatic com/l7tech/server/LifecycleBean.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 31: athrow
      // 32: bipush 1
      // 33: goto 3b
      // 36: invokestatic com/l7tech/server/LifecycleBean.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 39: athrow
      // 3a: bipush 0
      // 3b: ireturn
      // try (0 -> 8): 9 org/springframework/beans/BeansException
      // try (3 -> 17): 18 org/springframework/beans/BeansException
      // try (11 -> 22): 22 org/springframework/beans/BeansException
   }

   protected boolean isStoppingByAnotherThread() {
      // $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/LifecycleBean.d Z
      // 04: ifeq 3a
      // 07: aload 0
      // 08: getfield com/l7tech/server/LifecycleBean.e Ljava/util/concurrent/locks/ReadWriteLock;
      // 0b: checkcast java/util/concurrent/locks/ReentrantReadWriteLock
      // 0e: invokevirtual java/util/concurrent/locks/ReentrantReadWriteLock.isWriteLocked ()Z
      // 11: ifeq 3a
      // 14: goto 1b
      // 17: invokestatic com/l7tech/server/LifecycleBean.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 1a: athrow
      // 1b: aload 0
      // 1c: getfield com/l7tech/server/LifecycleBean.e Ljava/util/concurrent/locks/ReadWriteLock;
      // 1f: checkcast java/util/concurrent/locks/ReentrantReadWriteLock
      // 22: invokevirtual java/util/concurrent/locks/ReentrantReadWriteLock.writeLock ()Ljava/util/concurrent/locks/ReentrantReadWriteLock$WriteLock;
      // 25: invokevirtual java/util/concurrent/locks/ReentrantReadWriteLock$WriteLock.isHeldByCurrentThread ()Z
      // 28: ifne 3a
      // 2b: goto 32
      // 2e: invokestatic com/l7tech/server/LifecycleBean.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 31: athrow
      // 32: bipush 1
      // 33: goto 3b
      // 36: invokestatic com/l7tech/server/LifecycleBean.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 39: athrow
      // 3a: bipush 0
      // 3b: ireturn
      // try (0 -> 8): 9 org/springframework/beans/BeansException
      // try (3 -> 17): 18 org/springframework/beans/BeansException
      // try (11 -> 22): 22 org/springframework/beans/BeansException
   }

   public boolean isStarted() {
      this.e.readLock().lock();

      try {
         return this.d;
      } finally {
         this.e.readLock().unlock();
      }
   }

   @Override
   public String toString() {
      return this.b;
   }

   protected LifecycleBean(@NotNull String var1, @NotNull Logger var2, @NotNull String var3, @Nullable LicenseManager var4) {
      if (var1 == null) {
         a(0);
      }

      if (var2 == null) {
         a(1);
      }

      if (var3 == null) {
         a(2);
      }

      super();
      this.e = new ReentrantReadWriteLock(false);
      this.b = var1;
      this.a = var2;
      this.c = var3;
      this.g = var4;
      this.e.writeLock().lock();

      try {
         this.d = false;
      } finally {
         this.e.writeLock().unlock();
      }
   }

   protected void setLicenseManager(LicenseManager var1) {
      try {
         if (this.g == null) {
            this.g = var1;
         }
      } catch (BeansException var2) {
         throw b(var2);
      }
   }

   protected void init() {
   }

   protected void doStart() throws LifecycleException {
   }

   protected void doStop() throws LifecycleException {
   }

   protected void doClose() throws LifecycleException {
   }

   protected ApplicationContext getApplicationContext() {
      return this.f;
   }

   protected void onApplicationEvent(ApplicationEvent 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 0
      // 01: getfield com/l7tech/server/LifecycleBean.c Ljava/lang/String;
      // 04: ifnull 72
      // 07: aload 1
      // 08: instanceof com/l7tech/server/event/system/LicenseChangeEvent
      // 0b: ifeq 44
      // 0e: goto 15
      // 11: invokestatic com/l7tech/server/LifecycleBean.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 14: athrow
      // 15: aload 0
      // 16: invokevirtual com/l7tech/server/LifecycleBean.isStarted ()Z
      // 19: ifeq 28
      // 1c: goto 23
      // 1f: invokestatic com/l7tech/server/LifecycleBean.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 22: athrow
      // 23: return
      // 24: invokestatic com/l7tech/server/LifecycleBean.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 27: athrow
      // 28: aload 0
      // 29: invokevirtual com/l7tech/server/LifecycleBean.isLicensed ()Z
      // 2c: ifeq 72
      // 2f: new com/l7tech/server/b5
      // 32: dup
      // 33: aload 0
      // 34: invokespecial com/l7tech/server/b5.<init> (Lcom/l7tech/server/LifecycleBean;)V
      // 37: ldc2_w 250
      // 3a: invokestatic com/l7tech/util/Background.scheduleOneShot (Ljava/util/TimerTask;J)V
      // 3d: goto 72
      // 40: invokestatic com/l7tech/server/LifecycleBean.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 43: athrow
      // 44: aload 1
      // 45: instanceof com/l7tech/server/event/system/Stopped
      // 48: ifeq 72
      // 4b: aload 0
      // 4c: invokevirtual com/l7tech/server/LifecycleBean.stop ()V
      // 4f: goto 72
      // 52: invokestatic com/l7tech/server/LifecycleBean.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 55: athrow
      // 56: astore 2
      // 57: aload 0
      // 58: getfield com/l7tech/server/LifecycleBean.a Ljava/util/logging/Logger;
      // 5b: getstatic java/util/logging/Level.SEVERE Ljava/util/logging/Level;
      // 5e: aload 2
      // 5f: invokestatic com/l7tech/util/ExceptionUtils.getMessage (Ljava/lang/Throwable;)Ljava/lang/String;
      // 62: getstatic com/l7tech/server/LifecycleBean.R [Ljava/lang/String;
      // 65: bipush 6
      // 67: aaload
      // 68: swap
      // 69: invokedynamic makeConcatWithConstants (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; bsm=java/lang/invoke/StringConcatFactory.makeConcatWithConstants (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite; args=[ "\u0001\u0001" ]
      // 6e: aload 2
      // 6f: invokevirtual java/util/logging/Logger.log (Ljava/util/logging/Level;Ljava/lang/String;Ljava/lang/Throwable;)V
      // 72: return
      // try (18 -> 28): 28 com/l7tech/server/LifecycleException
      // try (9 -> 16): 16 com/l7tech/server/LifecycleException
      // try (3 -> 12): 13 com/l7tech/server/LifecycleException
      // try (0 -> 6): 7 com/l7tech/server/LifecycleException
      // try (33 -> 35): 38 com/l7tech/server/LifecycleException
      // try (30 -> 36): 36 org/springframework/beans/BeansException
   }

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

   static {
      String[] var5 = new String[8];
      int var3 = 0;
      String var2 = "^)}V?\\\u0015\u00030cS\"\u0001X\u0016)|Q\u000e\u0014\\\f4CM$\u001a@\u001f\u0001/~\u0010'UM\u0007#{\u00108\u0007K\u0014%a\u0010\u0007\u000b_\u0007#j\\'\u0007{\u0007!}\u0006\u000e/tX.\u0010>#2tJ&\u0007W\u0016`uP9By,/gq>\u000eUB0rM*\u000f\\\u0016%a\u001flGJE`|YkGJLe`\u001f&\u0017J\u0016`}P?B[\u0007`}J'\u000e\u0004\f!~Z";
      int var4 = "^)}V?\\\u0015\u00030cS\"\u0001X\u0016)|Q\u000e\u0014\\\f4CM$\u001a@\u001f\u0001/~\u0010'UM\u0007#{\u00108\u0007K\u0014%a\u0010\u0007\u000b_\u0007#j\\'\u0007{\u0007!}\u0006\u000e/tX.\u0010>#2tJ&\u0007W\u0016`uP9By,/gq>\u000eUB0rM*\u000f\\\u0016%a\u001flGJE`|YkGJLe`\u001f&\u0017J\u0016`}P?B[\u0007`}J'\u000e\u0004\f!~Z"
         .length();
      char var1 = 6;
      int var7 = -1;

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

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

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

                  var2 = "=$xW-\r\u0013\u001c%9F5\u0007CH9lW2\u0011@\u001c/t\u000fa\u000e\u0004#zP/\u001bV./xA4\u001aV";
                  var4 = "=$xW-\r\u0013\u001c%9F5\u0007CH9lW2\u0011@\u001c/t\u000fa\u000e\u0004#zP/\u001bV./xA4\u001aV".length();
                  var1 = 26;
                  var7 = -1;
            }

            var10000 = 71;
            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] ^ 116);
      }

      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 -> 13;
            case 2 -> 94;
            case 3 -> 114;
            case 4 -> 6;
            case 5 -> 47;
            default -> 116;
         });
      }

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