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

import com.airlock.iam.base.api.domain.model.exception.ViolationException;
import com.airlock.iam.base.api.domain.model.mapification.Mapifiable;
import com.airlock.iam.base.api.domain.model.mapification.MapifiedValues;
import com.airlock.iam.base.domain.model.mapification.MapifiedValuesBuilder;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.List;

public final class FlowSaml2Attribute implements Mapifiable {
   private static final String NAME_KEY = "name";
   private static final String VALUES_KEY = "values";
   private static final String FORMAT_KEY = "format";
   private final String attributeName;
   private final List<String> attributeValues;
   private final String nameFormat;

   public FlowSaml2Attribute(String attributeName, String attributeValue, String nameFormat) {
      this(attributeName, List.of(ViolationException.checkNotNull(attributeValue, "attributeValue must not be null")), nameFormat);
   }

   public FlowSaml2Attribute(String attributeName, List<String> attributeValues, String nameFormat) {
      ViolationException.checkNotEmpty(attributeValues, "SAML2 Attribute must contain at least one value");
      this.attributeName = attributeName;
      this.attributeValues = List.copyOf(attributeValues);
      this.nameFormat = nameFormat;
   }

   public List<String> getAttributeValues() {
      return this.attributeValues;
   }

   @Override
   public MapifiedValues mapify() {
      return MapifiedValuesBuilder.mapifiedValues()
         .put("name", this.attributeName)
         .putIfNotEmpty("values", this.attributeValues)
         .put("format", this.nameFormat)
         .build();
   }

   public static FlowSaml2Attribute demapify(MapifiedValues values) {
      return new FlowSaml2Attribute(values.get("name"), values.getList("values"), values.get("format"));
   }

   @SuppressFBWarnings(justification = "generated code")
   public String getAttributeName() {
      return this.attributeName;
   }

   @SuppressFBWarnings(justification = "generated code")
   public String getNameFormat() {
      return this.nameFormat;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof FlowSaml2Attribute other)) {
         return false;
      } else {
         Object this$attributeName = this.attributeName;
         Object other$attributeName = other.attributeName;
         if (this$attributeName == null ? other$attributeName == null : this$attributeName.equals(other$attributeName)) {
            Object this$attributeValues = this.attributeValues;
            Object other$attributeValues = other.attributeValues;
            if (this$attributeValues == null ? other$attributeValues == null : this$attributeValues.equals(other$attributeValues)) {
               Object this$nameFormat = this.nameFormat;
               Object other$nameFormat = other.nameFormat;
               return this$nameFormat == null ? other$nameFormat == null : this$nameFormat.equals(other$nameFormat);
            } else {
               return false;
            }
         } else {
            return false;
         }
      }
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public int hashCode() {
      int PRIME = 59;
      int result = 1;
      Object $attributeName = this.attributeName;
      result = result * 59 + ($attributeName == null ? 43 : $attributeName.hashCode());
      Object $attributeValues = this.attributeValues;
      result = result * 59 + ($attributeValues == null ? 43 : $attributeValues.hashCode());
      Object $nameFormat = this.nameFormat;
      return result * 59 + ($nameFormat == null ? 43 : $nameFormat.hashCode());
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "FlowSaml2Attribute(attributeName="
         + this.getAttributeName()
         + ", attributeValues="
         + this.getAttributeValues()
         + ", nameFormat="
         + this.getNameFormat()
         + ")";
   }
}
