package com.sun.identity.saml2.common;

import com.sun.identity.saml2.assertion.AssertionFactory;
import com.sun.identity.saml2.assertion.NameID;
import java.util.StringTokenizer;

public class NameIDInfo {
   private static final String DELIM = "|";
   private static final String NULL = "null";
   private final String hostEntityId;
   private final String remoteEntityId;
   private final String nameIdValue;
   private final String nameQualifier;
   private final String format;
   private final String spNameIdValue;
   private final String spNameQualifier;
   private final String role;
   private final boolean isAffiliation;
   private final NameID nameId;

   public NameIDInfo(String hostEntityID, String remoteEntityID, NameID nameID, String hostEntityRole, boolean isAffiliation) throws SAML2Exception {
      if (nameID == null) {
         throw new SAML2Exception(SAML2Utils.BUNDLE.getString("nullNameID"));
      }

      if (hostEntityID == null) {
         throw new SAML2Exception(SAML2Utils.BUNDLE.getString("nullHostEntityID"));
      }

      if (remoteEntityID == null) {
         throw new SAML2Exception(SAML2Utils.BUNDLE.getString("nullRemoteEntityID"));
      }

      if (hostEntityRole == null) {
         throw new SAML2Exception(SAML2Utils.BUNDLE.getString("nullEntityRole"));
      }

      this.hostEntityId = hostEntityID;
      this.remoteEntityId = remoteEntityID;
      this.nameIdValue = nameID.getValue();
      this.nameQualifier = nameID.getNameQualifier();
      this.format = nameID.getFormat();
      this.spNameIdValue = nameID.getSPProvidedID();
      this.spNameQualifier = nameID.getSPNameQualifier();
      this.role = hostEntityRole;
      this.isAffiliation = isAffiliation;
      this.nameId = nameID;
   }

   public NameID getNameID() {
      return this.nameId;
   }

   public NameIDInfoKey getNameIDInfoKey() throws SAML2Exception {
      return new NameIDInfoKey(this.nameIdValue, this.hostEntityId, this.remoteEntityId);
   }

   public String getNameIDValue() {
      return this.nameIdValue;
   }

   public String getNameQualifier() {
      return this.nameQualifier;
   }

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

   public String getSPNameIDValue() {
      return this.spNameIdValue;
   }

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

   public String getHostEntityID() {
      return this.hostEntityId;
   }

   public String getRemoteEntityID() {
      return this.remoteEntityId;
   }

   public String getHostEntityRole() {
      return this.role;
   }

   public boolean isAffiliation() {
      return this.isAffiliation;
   }

   public String toValueString() {
      StringBuilder sb = new StringBuilder();
      sb.append(this.hostEntityId).append("|").append(this.remoteEntityId).append("|").append(this.nameIdValue).append("|");
      if (this.nameQualifier != null && !this.nameQualifier.isEmpty()) {
         sb.append(this.nameQualifier);
      } else {
         sb.append("null");
      }

      sb.append("|");
      if (this.format != null && !this.format.isEmpty()) {
         sb.append(this.format);
      } else {
         sb.append("null");
      }

      sb.append("|");
      if (this.spNameIdValue != null && !this.spNameIdValue.isEmpty()) {
         sb.append(this.spNameIdValue);
      } else {
         sb.append("null");
      }

      sb.append("|");
      if (this.spNameQualifier != null && !this.spNameQualifier.isEmpty()) {
         sb.append(this.spNameQualifier);
      } else {
         sb.append("null");
      }

      sb.append("|");
      if (this.role != null && !this.role.isEmpty()) {
         sb.append(this.role);
      } else {
         sb.append("null");
      }

      sb.append("|").append(this.isAffiliation);
      return sb.toString();
   }

   public static NameIDInfo parse(String info) throws SAML2Exception {
      if (info == null) {
         throw new SAML2Exception(SAML2Utils.BUNDLE.getString("nullNameIDInfo"));
      }

      StringTokenizer st = new StringTokenizer(info, "|");
      if (st.countTokens() != 9) {
         throw new SAML2Exception(SAML2Utils.BUNDLE.getString("inValidNameIDInfo"));
      }

      String hostEntityID = st.nextToken();
      String remoteEntityID = st.nextToken();
      String nameIDValue = st.nextToken();
      String nameQualifier = st.nextToken();
      String format = st.nextToken();
      String spNameIDValue = st.nextToken();
      String spNameQualifier = st.nextToken();
      String role = st.nextToken();
      boolean isAffiliation = Boolean.parseBoolean(st.nextToken());
      NameID nameID = AssertionFactory.getInstance().createNameID();
      nameID.setValue(nameIDValue);
      if (nameQualifier != null && !"null".equals(nameQualifier)) {
         nameID.setNameQualifier(nameQualifier);
      }

      if (spNameIDValue != null && !"null".equals(spNameIDValue)) {
         nameID.setSPProvidedID(spNameIDValue);
      }

      if (spNameQualifier != null && !"null".equals(spNameQualifier)) {
         nameID.setSPNameQualifier(spNameQualifier);
      }

      if (format != null && !"null".equals(format)) {
         nameID.setFormat(format);
      }

      return new NameIDInfo(hostEntityID, remoteEntityID, nameID, role, isAffiliation);
   }
}
