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

import com.sun.identity.saml2.common.SAML2Exception;
import com.sun.identity.saml2.common.SAML2SDKUtils;
import com.sun.identity.saml2.protocol.NameIDPolicy;
import org.w3c.dom.Element;

public class NameIDPolicyImpl implements NameIDPolicy {
   private static final String FORMAT = "Format";
   private static final String SPNAMEQUALIFIER = "SPNameQualifier";
   private static final String ALLOWCREATE = "AllowCreate";
   private boolean isMutable;
   private String format;
   private String spNameQualifier;
   private Boolean allowCreate;

   public NameIDPolicyImpl() {
      this.isMutable = true;
   }

   public NameIDPolicyImpl(Element element) {
      this.parseElement(element);
   }

   @Override
   public String getFormat() {
      return this.format;
   }

   @Override
   public void setFormat(String uri) throws SAML2Exception {
      if (this.isMutable) {
         this.format = uri;
      } else {
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("objectImmutable"));
      }
   }

   @Override
   public String getSPNameQualifier() {
      return this.spNameQualifier;
   }

   @Override
   public void setSPNameQualifier(String spNameQualifier) throws SAML2Exception {
      if (this.isMutable) {
         this.spNameQualifier = spNameQualifier;
      } else {
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("objectImmutable"));
      }
   }

   @Override
   public void setAllowCreate(boolean value) throws SAML2Exception {
      if (this.isMutable) {
         this.allowCreate = value ? Boolean.TRUE : Boolean.FALSE;
      } else {
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("objectImmutable"));
      }
   }

   @Override
   public boolean isAllowCreate() {
      return this.allowCreate == null ? false : this.allowCreate;
   }

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

   @Override
   public String toXMLString(boolean includeNSPrefix, boolean declareNS) throws SAML2Exception {
      this.validateData();
      StringBuilder xmlString = new StringBuilder(150);
      xmlString.append("<");
      if (includeNSPrefix) {
         xmlString.append("samlp:");
      }

      xmlString.append("NameIDPolicy").append(" ");
      if (declareNS) {
         xmlString.append(" xmlns:samlp=\"urn:oasis:names:tc:SAML:2.0:protocol\"");
      }

      if (this.format != null && !this.format.isEmpty()) {
         xmlString.append(" ").append("Format").append("=").append("\"").append(this.format).append("\"");
      }

      if (this.spNameQualifier != null && !this.spNameQualifier.isEmpty()) {
         xmlString.append(" ").append("SPNameQualifier").append("=").append("\"").append(this.spNameQualifier).append("\"");
      }

      if (this.allowCreate != null) {
         xmlString.append(" ").append("AllowCreate").append("=").append("\"").append(this.allowCreate).append("\"");
      }

      xmlString.append(">").append("</samlp:").append("NameIDPolicy").append(">");
      return xmlString.toString();
   }

   @Override
   public void makeImmutable() {
      if (this.isMutable) {
         this.isMutable = false;
      }
   }

   @Override
   public boolean isMutable() {
      return this.isMutable;
   }

   private void parseElement(Element element) {
      this.format = element.getAttribute("Format");
      this.spNameQualifier = element.getAttribute("SPNameQualifier");
      String allowCreateStr = element.getAttribute("AllowCreate");
      if (allowCreateStr != null && !allowCreateStr.isEmpty()) {
         this.allowCreate = SAML2SDKUtils.booleanValueOf(allowCreateStr);
      }
   }

   private void validateData() throws SAML2Exception {
      if ((this.format == null || this.format.isEmpty()) && (this.spNameQualifier == null || this.spNameQualifier.isEmpty()) && this.allowCreate == null) {
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("noAttributes"));
      }
   }
}
