package org.jivesoftware.smack.provider;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.ParserUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

public class IntrospectionProvider {
   public static Object parseWithIntrospection(Class<?> objectClass, XmlPullParser parser, int initialDepth) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, XmlPullParserException, IOException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException {
      ParserUtils.assertAtStartTag(parser);
      Object object = objectClass.newInstance();

      label20:
      do {
         while(true) {
            int eventType = parser.next();
            switch(eventType) {
            case 2:
               String name = parser.getName();
               String stringValue = parser.nextText();
               Class<?> propertyType = object.getClass().getMethod("get" + Character.toUpperCase(name.charAt(0)) + name.substring(1)).getReturnType();
               Object value = decode(propertyType, stringValue);
               object.getClass().getMethod("set" + Character.toUpperCase(name.charAt(0)) + name.substring(1), propertyType).invoke(object, value);
               break;
            case 3:
               continue label20;
            }
         }
      } while(parser.getDepth() != initialDepth);

      ParserUtils.assertAtEndTag(parser);
      return object;
   }

   private static Object decode(Class<?> type, String value) throws ClassNotFoundException {
      String name = type.getName();
      byte var4 = -1;
      switch(name.hashCode()) {
      case -1325958191:
         if (name.equals("double")) {
            var4 = 5;
         }
         break;
      case -530663260:
         if (name.equals("java.lang.Class")) {
            var4 = 8;
         }
         break;
      case 104431:
         if (name.equals("int")) {
            var4 = 2;
         }
         break;
      case 3039496:
         if (name.equals("byte")) {
            var4 = 7;
         }
         break;
      case 3327612:
         if (name.equals("long")) {
            var4 = 3;
         }
         break;
      case 64711720:
         if (name.equals("boolean")) {
            var4 = 1;
         }
         break;
      case 97526364:
         if (name.equals("float")) {
            var4 = 4;
         }
         break;
      case 109413500:
         if (name.equals("short")) {
            var4 = 6;
         }
         break;
      case 1195259493:
         if (name.equals("java.lang.String")) {
            var4 = 0;
         }
      }

      switch(var4) {
      case 0:
         return value;
      case 1:
         return Boolean.valueOf(value);
      case 2:
         return Integer.valueOf(value);
      case 3:
         return Long.valueOf(value);
      case 4:
         return Float.valueOf(value);
      case 5:
         return Double.valueOf(value);
      case 6:
         return Short.valueOf(value);
      case 7:
         return Byte.valueOf(value);
      case 8:
         return Class.forName(value);
      default:
         return null;
      }
   }

   public abstract static class PacketExtensionIntrospectionProvider<PE extends ExtensionElement> extends ExtensionElementProvider<PE> {
      private final Class<PE> elementClass;

      protected PacketExtensionIntrospectionProvider(Class<PE> elementClass) {
         this.elementClass = elementClass;
      }

      public PE parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackException {
         try {
            return (ExtensionElement)IntrospectionProvider.parseWithIntrospection(this.elementClass, parser, initialDepth);
         } catch (SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ClassNotFoundException | NoSuchMethodException var4) {
            throw new SmackException(var4);
         }
      }
   }

   public abstract static class IQIntrospectionProvider<I extends IQ> extends IQProvider<I> {
      private final Class<I> elementClass;

      protected IQIntrospectionProvider(Class<I> elementClass) {
         this.elementClass = elementClass;
      }

      public I parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackException {
         try {
            return (IQ)IntrospectionProvider.parseWithIntrospection(this.elementClass, parser, initialDepth);
         } catch (SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ClassNotFoundException | NoSuchMethodException var4) {
            throw new SmackException(var4);
         }
      }
   }
}
