package org.jivesoftware.smack.sasl.packet;

import java.util.Map;
import org.jivesoftware.smack.packet.AbstractError;
import org.jivesoftware.smack.packet.PlainStreamElement;
import org.jivesoftware.smack.sasl.SASLError;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;

public class SaslStreamElements {
   public static final String NAMESPACE = "urn:ietf:params:xml:ns:xmpp-sasl";

   public static class SASLFailure extends AbstractError implements PlainStreamElement {
      public static final String ELEMENT = "failure";
      private final SASLError saslError;
      private final String saslErrorString;

      public SASLFailure(String saslError) {
         this(saslError, (Map)null);
      }

      public SASLFailure(String saslError, Map<String, String> descriptiveTexts) {
         super(descriptiveTexts);
         SASLError error = SASLError.fromString(saslError);
         if (error == null) {
            this.saslError = SASLError.not_authorized;
         } else {
            this.saslError = error;
         }

         this.saslErrorString = saslError;
      }

      public SASLError getSASLError() {
         return this.saslError;
      }

      public String getSASLErrorString() {
         return this.saslErrorString;
      }

      public XmlStringBuilder toXML() {
         XmlStringBuilder xml = new XmlStringBuilder();
         xml.halfOpenElement("failure").xmlnsAttribute("urn:ietf:params:xml:ns:xmpp-sasl").rightAngleBracket();
         xml.emptyElement(this.saslErrorString);
         this.addDescriptiveTextsAndExtensions(xml);
         xml.closeElement("failure");
         return xml;
      }

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

   public static class Success implements PlainStreamElement {
      public static final String ELEMENT = "success";
      private final String data;

      public Success(String data) {
         this.data = StringUtils.returnIfNotEmptyTrimmed(data);
      }

      public String getData() {
         return this.data;
      }

      public XmlStringBuilder toXML() {
         XmlStringBuilder xml = new XmlStringBuilder();
         xml.halfOpenElement("success").xmlnsAttribute("urn:ietf:params:xml:ns:xmpp-sasl").rightAngleBracket();
         xml.optAppend((CharSequence)this.data);
         xml.closeElement("success");
         return xml;
      }
   }

   public static class Response implements PlainStreamElement {
      public static final String ELEMENT = "response";
      private final String authenticationText;

      public Response() {
         this.authenticationText = null;
      }

      public Response(String authenticationText) {
         this.authenticationText = StringUtils.returnIfNotEmptyTrimmed(authenticationText);
      }

      public XmlStringBuilder toXML() {
         XmlStringBuilder xml = new XmlStringBuilder();
         xml.halfOpenElement("response").xmlnsAttribute("urn:ietf:params:xml:ns:xmpp-sasl").rightAngleBracket();
         xml.optAppend((CharSequence)this.authenticationText);
         xml.closeElement("response");
         return xml;
      }

      public String getAuthenticationText() {
         return this.authenticationText;
      }
   }

   public static class Challenge implements PlainStreamElement {
      public static final String ELEMENT = "challenge";
      private final String data;

      public Challenge(String data) {
         this.data = StringUtils.returnIfNotEmptyTrimmed(data);
      }

      public XmlStringBuilder toXML() {
         XmlStringBuilder xml = (new XmlStringBuilder()).halfOpenElement("challenge").xmlnsAttribute("urn:ietf:params:xml:ns:xmpp-sasl").rightAngleBracket();
         xml.optAppend((CharSequence)this.data);
         xml.closeElement("challenge");
         return xml;
      }
   }

   public static class AuthMechanism implements PlainStreamElement {
      public static final String ELEMENT = "auth";
      private final String mechanism;
      private final String authenticationText;

      public AuthMechanism(String mechanism, String authenticationText) {
         this.mechanism = (String)Objects.requireNonNull(mechanism, "SASL mechanism shouldn't be null.");
         this.authenticationText = (String)StringUtils.requireNotNullOrEmpty(authenticationText, "SASL authenticationText must not be null or empty (RFC6120 6.4.2)");
      }

      public XmlStringBuilder toXML() {
         XmlStringBuilder xml = new XmlStringBuilder();
         xml.halfOpenElement("auth").xmlnsAttribute("urn:ietf:params:xml:ns:xmpp-sasl").attribute("mechanism", this.mechanism).rightAngleBracket();
         xml.optAppend((CharSequence)this.authenticationText);
         xml.closeElement("auth");
         return xml;
      }

      public String getMechanism() {
         return this.mechanism;
      }

      public String getAuthenticationText() {
         return this.authenticationText;
      }
   }
}
