package org.jivesoftware.smack.packet;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.jivesoftware.smack.packet.id.StanzaIdUtil;
import org.jivesoftware.smack.util.MultiMap;
import org.jivesoftware.smack.util.PacketUtil;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
import org.jxmpp.util.XmppStringUtils;

public abstract class Stanza implements TopLevelStreamElement {
   public static final String TEXT = "text";
   public static final String ITEM = "item";
   protected static final String DEFAULT_LANGUAGE;
   private final MultiMap<String, ExtensionElement> packetExtensions;
   private String id;
   private Jid to;
   private Jid from;
   private XMPPError error;
   protected String language;

   protected Stanza() {
      this(StanzaIdUtil.newStanzaId());
   }

   protected Stanza(String stanzaId) {
      this.packetExtensions = new MultiMap();
      this.id = null;
      this.error = null;
      this.setStanzaId(stanzaId);
   }

   protected Stanza(Stanza p) {
      this.packetExtensions = new MultiMap();
      this.id = null;
      this.error = null;
      this.id = p.getStanzaId();
      this.to = p.getTo();
      this.from = p.getFrom();
      this.error = p.error;
      Iterator i$ = p.getExtensions().iterator();

      while(i$.hasNext()) {
         ExtensionElement pe = (ExtensionElement)i$.next();
         this.addExtension(pe);
      }

   }

   public String getStanzaId() {
      return this.id;
   }

   /** @deprecated */
   @Deprecated
   public String getPacketID() {
      return this.getStanzaId();
   }

   public void setStanzaId(String id) {
      if (id != null) {
         StringUtils.requireNotNullOrEmpty(id, "id must either be null or not the empty String");
      }

      this.id = id;
   }

   /** @deprecated */
   @Deprecated
   public void setPacketID(String packetID) {
      this.setStanzaId(packetID);
   }

   public boolean hasStanzaIdSet() {
      return this.id != null;
   }

   public Jid getTo() {
      return this.to;
   }

   /** @deprecated */
   @Deprecated
   public void setTo(String to) {
      Jid jid;
      try {
         jid = JidCreate.from(to);
      } catch (XmppStringprepException var4) {
         throw new IllegalArgumentException(var4);
      }

      this.setTo(jid);
   }

   public void setTo(Jid to) {
      this.to = to;
   }

   public Jid getFrom() {
      return this.from;
   }

   /** @deprecated */
   @Deprecated
   public void setFrom(String from) {
      Jid jid;
      try {
         jid = JidCreate.from(from);
      } catch (XmppStringprepException var4) {
         throw new IllegalArgumentException(var4);
      }

      this.setFrom(jid);
   }

   public void setFrom(Jid from) {
      this.from = from;
   }

   public XMPPError getError() {
      return this.error;
   }

   public void setError(XMPPError error) {
      this.error = error;
   }

   public String getLanguage() {
      return this.language;
   }

   public void setLanguage(String language) {
      this.language = language;
   }

   public List<ExtensionElement> getExtensions() {
      synchronized(this.packetExtensions) {
         return this.packetExtensions.values();
      }
   }

   public Set<ExtensionElement> getExtensions(String elementName, String namespace) {
      StringUtils.requireNotNullOrEmpty(elementName, "elementName must not be null or empty");
      StringUtils.requireNotNullOrEmpty(namespace, "namespace must not be null or empty");
      String key = XmppStringUtils.generateKey(elementName, namespace);
      return this.packetExtensions.getAll(key);
   }

   public ExtensionElement getExtension(String namespace) {
      return PacketUtil.extensionElementFrom(this.getExtensions(), (String)null, namespace);
   }

   public <PE extends ExtensionElement> PE getExtension(String elementName, String namespace) {
      if (namespace == null) {
         return null;
      } else {
         String key = XmppStringUtils.generateKey(elementName, namespace);
         ExtensionElement packetExtension;
         synchronized(this.packetExtensions) {
            packetExtension = (ExtensionElement)this.packetExtensions.getFirst(key);
         }

         return packetExtension == null ? null : packetExtension;
      }
   }

   public void addExtension(ExtensionElement extension) {
      if (extension != null) {
         String key = XmppStringUtils.generateKey(extension.getElementName(), extension.getNamespace());
         synchronized(this.packetExtensions) {
            this.packetExtensions.put(key, extension);
         }
      }
   }

   public void addExtensions(Collection<ExtensionElement> extensions) {
      if (extensions != null) {
         Iterator i$ = extensions.iterator();

         while(i$.hasNext()) {
            ExtensionElement packetExtension = (ExtensionElement)i$.next();
            this.addExtension(packetExtension);
         }

      }
   }

   public boolean hasExtension(String elementName, String namespace) {
      if (elementName == null) {
         return this.hasExtension(namespace);
      } else {
         String key = XmppStringUtils.generateKey(elementName, namespace);
         synchronized(this.packetExtensions) {
            return this.packetExtensions.containsKey(key);
         }
      }
   }

   public boolean hasExtension(String namespace) {
      synchronized(this.packetExtensions) {
         Iterator i$ = this.packetExtensions.values().iterator();

         ExtensionElement packetExtension;
         do {
            if (!i$.hasNext()) {
               return false;
            }

            packetExtension = (ExtensionElement)i$.next();
         } while(!packetExtension.getNamespace().equals(namespace));

         return true;
      }
   }

   public ExtensionElement removeExtension(String elementName, String namespace) {
      String key = XmppStringUtils.generateKey(elementName, namespace);
      synchronized(this.packetExtensions) {
         return (ExtensionElement)this.packetExtensions.remove(key);
      }
   }

   public ExtensionElement removeExtension(ExtensionElement extension) {
      return this.removeExtension(extension.getElementName(), extension.getNamespace());
   }

   public String toString() {
      return this.toXML().toString();
   }

   protected final XmlStringBuilder getExtensionsXML() {
      XmlStringBuilder xml = new XmlStringBuilder();
      Iterator i$ = this.getExtensions().iterator();

      while(i$.hasNext()) {
         ExtensionElement extension = (ExtensionElement)i$.next();
         xml.append(extension.toXML());
      }

      return xml;
   }

   public static String getDefaultLanguage() {
      return DEFAULT_LANGUAGE;
   }

   protected void addCommonAttributes(XmlStringBuilder xml) {
      xml.optAttribute("to", (CharSequence)this.getTo());
      xml.optAttribute("from", (CharSequence)this.getFrom());
      xml.optAttribute("id", this.getStanzaId());
      xml.xmllangAttribute(this.getLanguage());
   }

   protected void appendErrorIfExists(XmlStringBuilder xml) {
      XMPPError error = this.getError();
      if (error != null) {
         xml.append(error.toXML());
      }

   }

   static {
      DEFAULT_LANGUAGE = Locale.getDefault().getLanguage().toLowerCase(Locale.US);
   }
}
