package org.jivesoftware.smack.debugger;

import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Constructor;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;

public class ReflectionDebuggerFactory implements SmackDebuggerFactory {
   private static final Logger LOGGER = Logger.getLogger(ReflectionDebuggerFactory.class.getName());
   private static final String DEBUGGER_CLASS_PROPERTY_NAME = "smack.debuggerClass";
   private static final String[] DEFAULT_DEBUGGERS = new String[]{"org.jivesoftware.smackx.debugger.EnhancedDebugger", "org.jivesoftware.smackx.debugger.android.AndroidDebugger", "org.jivesoftware.smack.debugger.ConsoleDebugger", "org.jivesoftware.smack.debugger.LiteDebugger", "org.jivesoftware.smack.debugger.JulDebugger"};

   public static void setDebuggerClass(Class<? extends SmackDebugger> debuggerClass) {
      if (debuggerClass == null) {
         System.clearProperty("smack.debuggerClass");
      } else {
         System.setProperty("smack.debuggerClass", debuggerClass.getCanonicalName());
      }

   }

   public static Class<SmackDebugger> getDebuggerClass() {
      String customDebuggerClassName = getCustomDebuggerClassName();
      if (customDebuggerClassName == null) {
         return getOneOfDefaultDebuggerClasses();
      } else {
         try {
            return Class.forName(customDebuggerClassName);
         } catch (Exception var2) {
            LOGGER.log(Level.WARNING, "Unable to instantiate debugger class " + customDebuggerClassName, var2);
            return null;
         }
      }
   }

   public SmackDebugger create(XMPPConnection connection, Writer writer, Reader reader) throws IllegalArgumentException {
      Class<SmackDebugger> debuggerClass = getDebuggerClass();
      if (debuggerClass != null) {
         try {
            Constructor<SmackDebugger> constructor = debuggerClass.getConstructor(XMPPConnection.class, Writer.class, Reader.class);
            return (SmackDebugger)constructor.newInstance(connection, writer, reader);
         } catch (Exception var6) {
            throw new IllegalArgumentException("Can't initialize the configured debugger!", var6);
         }
      } else {
         return null;
      }
   }

   private static String getCustomDebuggerClassName() {
      try {
         return System.getProperty("smack.debuggerClass");
      } catch (Throwable var1) {
         return null;
      }
   }

   private static Class<SmackDebugger> getOneOfDefaultDebuggerClasses() {
      String[] arr$ = DEFAULT_DEBUGGERS;
      int len$ = arr$.length;

      for(int i$ = 0; i$ < len$; ++i$) {
         String debugger = arr$[i$];
         if (!SmackConfiguration.isDisabledSmackClass(debugger)) {
            try {
               return Class.forName(debugger);
            } catch (ClassNotFoundException var5) {
               LOGGER.fine("Did not find debugger class '" + debugger + "'");
            } catch (ClassCastException var6) {
               LOGGER.warning("Found debugger class that does not appears to implement SmackDebugger interface");
            } catch (Exception var7) {
               LOGGER.warning("Unable to instantiate either Smack debugger class");
            }
         }
      }

      return null;
   }
}
