package com.sun.identity.saml2.assertion.impl;

import com.sun.identity.saml2.assertion.Issuer;
import com.sun.identity.saml2.common.SAML2Exception;
import com.sun.identity.saml2.common.SAML2SDKUtils;
import com.sun.identity.shared.xml.XMLUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class IssuerImpl extends NameIDTypeImpl implements Issuer {
   private static final String ISSUER_ELEMENT = "Issuer";

   public IssuerImpl() {
   }

   public IssuerImpl(String xml) throws SAML2Exception {
      Document document = XMLUtils.toDOMDocument(xml, SAML2SDKUtils.DEBUG);
      if (document != null) {
         Element rootElement = document.getDocumentElement();
         this.processElement(rootElement);
         this.makeImmutable();
      } else {
         SAML2SDKUtils.DEBUG.error("IssuerImpl.processElement(): invalid XML input");
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("errorObtainingElement"));
      }
   }

   public IssuerImpl(Element element) throws SAML2Exception {
      this.processElement(element);
      this.makeImmutable();
   }

   private void processElement(Element element) throws SAML2Exception {
      if (element == null) {
         SAML2SDKUtils.DEBUG.error("IssuerImpl.processElement(): invalid root element");
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("invalid_element"));
      }

      String elemName = element.getLocalName();
      if (elemName == null) {
         SAML2SDKUtils.DEBUG.error("IssuerImpl.processElement(): local name missing");
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("missing_local_name"));
      }

      if (!elemName.equals("Issuer")) {
         SAML2SDKUtils.DEBUG.error("IssuerImpl.processElement(): invalid local name " + elemName);
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("invalid_local_name"));
      }

      this.getValueAndAttributes(element);
   }

   @Override
   public String toXMLString(boolean includeNSPrefix, boolean declareNS) throws SAML2Exception {
      StringBuilder sb = new StringBuilder(2000);
      String ns = "";
      String appendNS = "";
      if (declareNS) {
         ns = " xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\"";
      }

      if (includeNSPrefix) {
         appendNS = "saml:";
      }

      sb.append("<").append(appendNS).append("Issuer").append(ns);
      String nameQualifier = this.getNameQualifier();
      if (nameQualifier != null && !nameQualifier.trim().isEmpty()) {
         sb.append(" ").append("NameQualifier").append("=\"").append(nameQualifier).append("\"");
      }

      String spNameQualifier = this.getSPNameQualifier();
      if (spNameQualifier != null && !spNameQualifier.trim().isEmpty()) {
         sb.append(" ").append("SPNameQualifier").append("=\"").append(spNameQualifier).append("\"");
      }

      String format = this.getFormat();
      if (format != null && !format.trim().isEmpty()) {
         sb.append(" ").append("Format").append("=\"").append(format).append("\"");
      }

      String spProvidedID = this.getSPProvidedID();
      if (spProvidedID != null && !spProvidedID.trim().isEmpty()) {
         sb.append(" ").append("SPProvidedID").append("=\"").append(spProvidedID);
      }

      sb.append(">");
      String value = this.getValue();
      if (value != null && !value.trim().isEmpty()) {
         sb.append(value);
         sb.append("</").append(appendNS).append("Issuer").append(">");
         return sb.toString();
      } else {
         SAML2SDKUtils.DEBUG.error("IssuerImpl.processElement(): name identifier is missing");
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("missing_name_identifier"));
      }
   }

   @Override
   public String toXMLString() throws SAML2Exception {
      return this.toXMLString(true, false);
   }
}
