package org.jivesoftware.smack.packet;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import org.jivesoftware.smack.util.PacketUtil;
import org.jivesoftware.smack.util.XmlStringBuilder;

public class AbstractError {
   private final String textNamespace;
   protected final Map<String, String> descriptiveTexts;
   private final List<ExtensionElement> extensions;

   protected AbstractError(Map<String, String> descriptiveTexts) {
      this(descriptiveTexts, (List)null);
   }

   protected AbstractError(Map<String, String> descriptiveTexts, List<ExtensionElement> extensions) {
      this(descriptiveTexts, (String)null, extensions);
   }

   protected AbstractError(Map<String, String> descriptiveTexts, String textNamespace, List<ExtensionElement> extensions) {
      if (descriptiveTexts != null) {
         this.descriptiveTexts = descriptiveTexts;
      } else {
         this.descriptiveTexts = Collections.emptyMap();
      }

      this.textNamespace = textNamespace;
      if (extensions != null) {
         this.extensions = extensions;
      } else {
         this.extensions = Collections.emptyList();
      }

   }

   public String getDescriptiveText() {
      String defaultLocale = Locale.getDefault().getLanguage();
      String descriptiveText = this.getDescriptiveText(defaultLocale);
      if (descriptiveText == null) {
         descriptiveText = this.getDescriptiveText("");
      }

      return descriptiveText;
   }

   public String getDescriptiveText(String xmllang) {
      return (String)this.descriptiveTexts.get(xmllang);
   }

   public <PE extends ExtensionElement> PE getExtension(String elementName, String namespace) {
      return PacketUtil.extensionElementFrom(this.extensions, elementName, namespace);
   }

   protected void addDescriptiveTextsAndExtensions(XmlStringBuilder xml) {
      Iterator i$ = this.descriptiveTexts.entrySet().iterator();

      while(i$.hasNext()) {
         Entry<String, String> entry = (Entry)i$.next();
         String xmllang = (String)entry.getKey();
         String text = (String)entry.getValue();
         xml.halfOpenElement("text").xmlnsAttribute(this.textNamespace).xmllangAttribute(xmllang).rightAngleBracket();
         xml.escape(text);
         xml.closeElement("text");
      }

      i$ = this.extensions.iterator();

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

   }
}
