package com.airlock.iam.transactionapproval.domain.model.flow;

import com.airlock.iam.base.api.domain.model.mapification.MapifiedValues;
import com.airlock.iam.base.domain.model.mapification.MapifiedValuesBuilder;
import com.airlock.iam.base.misc.collection.IamMaps;
import com.airlock.iam.elog.api.ELogger;
import com.airlock.iam.elog.api.ELoggerFactory;
import com.airlock.iam.flow.api.domain.model.session.SingleFlowScopedFlowAttribute;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Map;

public final class TransactionApprovalMessageAttributes implements SingleFlowScopedFlowAttribute<TransactionApprovalMessageAttributes> {
   public static final String ATTRIBUTES_KEY = "attr";
   private static final ELogger LOG = ELoggerFactory.getLogger(TransactionApprovalMessageAttributes.class);
   private final Map<String, String> attributes;

   public static TransactionApprovalMessageAttributes transactionApprovalMessageAttributes(Map<String, String> messageParameters) {
      return new TransactionApprovalMessageAttributes(messageParameters);
   }

   private TransactionApprovalMessageAttributes(Map<String, String> messageParameters) {
      this.attributes = Map.copyOf(messageParameters);
   }

   public TransactionApprovalMessageAttributes mergeWith(TransactionApprovalMessageAttributes newAttribute) {
      Map<String, String> result = IamMaps.mergeMaps(this.attributes, newAttribute.attributeAsRawKeyValues(), this::logWhenOverriding);
      return transactionApprovalMessageAttributes(result);
   }

   public Map<String, String> attributeAsRawKeyValues() {
      return Maps.filterEntries(this.attributes, e -> e != null && !Strings.isNullOrEmpty((String)e.getKey()) && !Strings.isNullOrEmpty((String)e.getValue()));
   }

   private void logWhenOverriding(String key) {
      LOG.debug("Replacing messageAttribute for key [" + key + "]");
   }

   public MapifiedValues mapify() {
      return MapifiedValuesBuilder.mapifiedValues().put("attr", this.attributes).build();
   }

   public static TransactionApprovalMessageAttributes demapify(MapifiedValues values) {
      return new TransactionApprovalMessageAttributes((Map<String, String>)values.get("attr"));
   }

   @SuppressFBWarnings(justification = "generated code")
   public Map<String, String> getAttributes() {
      return this.attributes;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "TransactionApprovalMessageAttributes(attributes=" + this.getAttributes() + ")";
   }
}
