package com.airlock.iam.saml2.domain.model.idp;

import com.airlock.iam.base.api.domain.model.exception.FailureException;
import com.airlock.iam.base.api.domain.model.mapification.MapifiedValues;
import com.airlock.iam.base.domain.model.mapification.MapifiedValuesBuilder;
import com.airlock.iam.flow.api.domain.model.session.MultiFlowScopedFlowAttribute;
import com.airlock.iam.saml2.domain.model.sp.Saml2SpEntityId;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public final class Saml2SpInitiatedRequestAttribute implements MultiFlowScopedFlowAttribute<Saml2SpInitiatedRequestAttribute> {
   public static final String REQUEST_ID_KEY = "requestId";
   public static final String ENTITY_ID_KEY = "entityId";
   private final Saml2RequestId requestId;
   private final Saml2SpEntityId spEntityId;

   public Saml2SpInitiatedRequestAttribute(Saml2RequestId requestId, Saml2SpEntityId spEntityId) {
      this.requestId = requestId;
      this.spEntityId = spEntityId;
   }

   public Saml2SpInitiatedRequestAttribute mergeWith(Saml2SpInitiatedRequestAttribute newAttribute) {
      throw new FailureException(
         String.format("A request ID (%s) was already defined. Cannot be overridden with %s", this.requestId, newAttribute.getRequestId())
      );
   }

   @Override
   public MapifiedValues mapify() {
      return MapifiedValuesBuilder.mapifiedValues().put("requestId", this.requestId.value()).put("entityId", this.spEntityId.value()).build();
   }

   public static Saml2SpInitiatedRequestAttribute demapify(MapifiedValues values) {
      return new Saml2SpInitiatedRequestAttribute(Saml2RequestId.requestId(values.get("requestId")), Saml2SpEntityId.spEntityId(values.get("entityId")));
   }

   @SuppressFBWarnings(justification = "generated code")
   public Saml2RequestId getRequestId() {
      return this.requestId;
   }

   @SuppressFBWarnings(justification = "generated code")
   public Saml2SpEntityId getSpEntityId() {
      return this.spEntityId;
   }
}
