package com.airlock.iam.login.domain.model.location.interpret;

import com.airlock.iam.base.api.domain.model.plugin.LogicForConfig;
import com.airlock.iam.login.api.domain.model.location.interpret.LocationInterpretation;
import com.airlock.iam.login.api.domain.model.location.interpret.LocationInterpreters;
import com.airlock.iam.login.application.configuration.location.interpret.LocationInterpretersConfigImpl;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.inject.Inject;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

@LogicForConfig(LocationInterpretersConfigImpl.class)
public class LocationInterpretersImpl implements LocationInterpreters {
   private final Pattern selectionPattern;
   private final Map<String, LocationInterpreter> locationInterpreters;

   @Inject
   public LocationInterpretersImpl(Pattern selectionPattern, Map<String, LocationInterpreter> locationInterpreters) {
      this.selectionPattern = selectionPattern;
      this.locationInterpreters = locationInterpreters;
   }

   @Override
   public boolean canInterpret(URI location) {
      return this.selectionPattern.matcher(location.toString()).matches();
   }

   @Override
   public LocationInterpretation interpret(URI uri) {
      Map<String, String> result = new HashMap<>();

      for (Entry<String, LocationInterpreter> entry : this.locationInterpreters.entrySet()) {
         entry.getValue().interpret(uri).ifPresent(val -> result.put(entry.getKey(), val));
      }

      return new LocationInterpretation(result);
   }

   @SuppressFBWarnings(justification = "generated code")
   public static LocationInterpretersImpl.LocationInterpretersImplBuilder builder() {
      return new LocationInterpretersImpl.LocationInterpretersImplBuilder();
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class LocationInterpretersImplBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private Pattern selectionPattern;
      @SuppressFBWarnings(justification = "generated code")
      private Map<String, LocationInterpreter> locationInterpreters;

      @SuppressFBWarnings(justification = "generated code")
      LocationInterpretersImplBuilder() {
      }

      @SuppressFBWarnings(justification = "generated code")
      public LocationInterpretersImpl.LocationInterpretersImplBuilder selectionPattern(Pattern selectionPattern) {
         this.selectionPattern = selectionPattern;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public LocationInterpretersImpl.LocationInterpretersImplBuilder locationInterpreters(Map<String, LocationInterpreter> locationInterpreters) {
         this.locationInterpreters = locationInterpreters;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public LocationInterpretersImpl build() {
         return new LocationInterpretersImpl(this.selectionPattern, this.locationInterpreters);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "LocationInterpretersImpl.LocationInterpretersImplBuilder(selectionPattern="
            + this.selectionPattern
            + ", locationInterpreters="
            + this.locationInterpreters
            + ")";
      }
   }
}
