package org.jivesoftware.smack.util;

import java.io.IOException;
import java.util.Locale;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.jid.parts.Resourcepart;
import org.jxmpp.stringprep.XmppStringprepException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

public class ParserUtils {
   public static void assertAtStartTag(XmlPullParser parser) throws XmlPullParserException {
      assert parser.getEventType() == 2;

   }

   public static void assertAtEndTag(XmlPullParser parser) throws XmlPullParserException {
      assert parser.getEventType() == 3;

   }

   public static void forwardToEndTagOfDepth(XmlPullParser parser, int depth) throws XmlPullParserException, IOException {
      for(int event = parser.getEventType(); event != 3 || parser.getDepth() != depth; event = parser.next()) {
      }

   }

   public static Jid getJidAttribute(XmlPullParser parser) throws XmppStringprepException {
      return getJidAttribute(parser, "jid");
   }

   public static Jid getJidAttribute(XmlPullParser parser, String name) throws XmppStringprepException {
      String jidString = parser.getAttributeValue("", name);
      return jidString == null ? null : JidCreate.from(jidString);
   }

   public static Resourcepart getResourcepartAttribute(XmlPullParser parser, String name) throws XmppStringprepException {
      String resourcepartString = parser.getAttributeValue("", name);
      return resourcepartString == null ? null : Resourcepart.from(resourcepartString);
   }

   public static Boolean getBooleanAttribute(XmlPullParser parser, String name) {
      String valueString = parser.getAttributeValue("", name);
      if (valueString == null) {
         return null;
      } else {
         valueString = valueString.toLowerCase(Locale.US);
         return !valueString.equals("true") && !valueString.equals("0") ? false : true;
      }
   }

   public static boolean getBooleanAttribute(XmlPullParser parser, String name, boolean defaultValue) {
      Boolean bool = getBooleanAttribute(parser, name);
      return bool == null ? defaultValue : bool;
   }

   public static Integer getIntegerAttribute(XmlPullParser parser, String name) {
      String valueString = parser.getAttributeValue("", name);
      return valueString == null ? null : Integer.valueOf(valueString);
   }

   public static int getIntegerAttribute(XmlPullParser parser, String name, int defaultValue) {
      Integer integer = getIntegerAttribute(parser, name);
      return integer == null ? defaultValue : integer;
   }

   public static int getIntegerFromNextText(XmlPullParser parser) throws XmlPullParserException, IOException {
      String intString = parser.nextText();
      return Integer.valueOf(intString);
   }

   public static Long getLongAttribute(XmlPullParser parser, String name) {
      String valueString = parser.getAttributeValue("", name);
      return valueString == null ? null : Long.valueOf(valueString);
   }

   public static long getLongAttribute(XmlPullParser parser, String name, long defaultValue) {
      Long l = getLongAttribute(parser, name);
      return l == null ? defaultValue : l;
   }
}
