package org.jivesoftware.smack.packet;

import java.util.List;
import java.util.Map;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;

public class StreamError extends AbstractError implements PlainStreamElement {
   public static final String ELEMENT = "stream:error";
   public static final String NAMESPACE = "urn:ietf:params:xml:ns:xmpp-streams";
   private final StreamError.Condition condition;
   private final String conditionText;

   public StreamError(StreamError.Condition condition, String conditionText, Map<String, String> descriptiveTexts, List<ExtensionElement> extensions) {
      super(descriptiveTexts, extensions);
      if (StringUtils.isNullOrEmpty(conditionText)) {
         conditionText = null;
      }

      if (conditionText != null) {
         switch(condition) {
         case see_other_host:
            break;
         default:
            throw new IllegalArgumentException("The given condition '" + condition + "' can not contain a conditionText");
         }
      }

      this.condition = condition;
      this.conditionText = conditionText;
   }

   public StreamError.Condition getCondition() {
      return this.condition;
   }

   public String getConditionText() {
      return this.conditionText;
   }

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

   public XmlStringBuilder toXML() {
      XmlStringBuilder xml = new XmlStringBuilder();
      xml.openElement("stream:error");
      xml.halfOpenElement(this.condition.toString()).xmlnsAttribute("urn:ietf:params:xml:ns:xmpp-streams").closeEmptyElement();
      this.addDescriptiveTextsAndExtensions(xml);
      xml.closeElement("stream:error");
      return xml;
   }

   public static enum Condition {
      bad_format,
      bad_namespace_prefix,
      conflict,
      connection_timeout,
      host_gone,
      host_unknown,
      improper_addressing,
      internal_server_error,
      invalid_from,
      invalid_namespace,
      invalid_xml,
      not_authorized,
      not_well_formed,
      policy_violation,
      remote_connection_failed,
      reset,
      resource_constraint,
      restricted_xml,
      see_other_host,
      system_shutdown,
      undeficed_condition,
      unsupported_encoding,
      unsupported_feature,
      unsupported_stanza_type,
      unsupported_version;

      public String toString() {
         return this.name().replace('_', '-');
      }

      public static StreamError.Condition fromString(String string) {
         string = string.replace('-', '_');
         StreamError.Condition condition = null;

         try {
            condition = valueOf(string);
            return condition;
         } catch (Exception var3) {
            throw new IllegalStateException("Could not transform string '" + string + "' to XMPPErrorCondition", var3);
         }
      }
   }
}
