package com.airlock.iam.flow.shared.domain.model.message;

import com.airlock.iam.base.api.domain.model.plugin.LogicForConfig;
import com.airlock.iam.flow.shared.api.domain.model.message.MtanMessageProvider;
import com.airlock.iam.flow.shared.api.domain.model.valueprovider.ValueMapProvider;
import com.airlock.iam.flow.shared.application.configuration.message.GenericMtanMessageProviderConfig;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.inject.Inject;
import java.util.HashMap;
import java.util.Map;

@LogicForConfig(GenericMtanMessageProviderConfig.class)
public class GenericMtanMessageProvider implements MtanMessageProvider {
   private final GenericMtanMessageProvider.GenericMtanMessageProviderSettings settings;
   private final ValueMapProvider valueProvider;
   private final MessageProviderHelper messageProvider;

   @Inject
   public GenericMtanMessageProvider(
      GenericMtanMessageProvider.GenericMtanMessageProviderSettings settings, ValueMapProvider valueProvider, MessageProviderHelper messageProvider
   ) {
      this.settings = settings;
      this.valueProvider = valueProvider;
      this.messageProvider = messageProvider;
   }

   @Override
   public String generateMtanMessage() {
      return this.messageProvider.translateGrowableMessage(this.settings.getResourceKey(), this.settings.getMaxLength(), this::mtanValues);
   }

   private Map<String, Object> mtanValues() {
      Map<String, Object> values = new HashMap<>(this.valueProvider.provideValueMap());
      values.put("TOKEN", "$TOKEN$");
      return values;
   }

   public static final class GenericMtanMessageProviderSettings {
      private final String resourceKey;
      private final int maxLength;

      @SuppressFBWarnings(justification = "generated code")
      public GenericMtanMessageProviderSettings(String resourceKey, int maxLength) {
         this.resourceKey = resourceKey;
         this.maxLength = maxLength;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      public int getMaxLength() {
         return this.maxLength;
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public boolean equals(Object o) {
         if (o == this) {
            return true;
         } else if (!(o instanceof GenericMtanMessageProvider.GenericMtanMessageProviderSettings other)) {
            return false;
         } else {
            if (this.maxLength != other.maxLength) {
               return false;
            }

            Object this$resourceKey = this.resourceKey;
            Object other$resourceKey = other.resourceKey;
            return this$resourceKey == null ? other$resourceKey == null : this$resourceKey.equals(other$resourceKey);
         }
      }

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

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "GenericMtanMessageProvider.GenericMtanMessageProviderSettings(resourceKey="
            + this.getResourceKey()
            + ", maxLength="
            + this.getMaxLength()
            + ")";
      }
   }
}
