package com.airlock.iam.base.infrastructure.mapper.jsonapi;

import com.airlock.iam.base.api.domain.model.exception.FailureException;
import com.airlock.iam.base.infrastructure.mapper.FieldMapper;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JsonApiResourceIdentifierArrayFieldMapper implements FieldMapper {
   private static final Pattern MODEL_FIELD_PATTERN = Pattern.compile("(.*)\\[(\\d+)]");
   private String idFieldName;
   private List<String> supportedMetaFields;

   @Override
   public String map(String modelField) {
      Matcher matcher = MODEL_FIELD_PATTERN.matcher(modelField);
      if (!matcher.matches()) {
         throw new FailureException("Unparsable model field value received: " + modelField);
      } else {
         String fieldName = matcher.group(1);
         String index = matcher.group(2);
         if (this.idFieldName.equals(fieldName)) {
            return "/data[" + index + "]/id";
         } else if (this.supportedMetaFields.contains(fieldName)) {
            return "/data[" + index + "]/meta/" + fieldName;
         } else {
            throw new FailureException("Unsupported model field value received: " + modelField);
         }
      }
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public JsonApiResourceIdentifierArrayFieldMapper(String idFieldName, List<String> supportedMetaFields) {
      this.idFieldName = idFieldName;
      this.supportedMetaFields = supportedMetaFields;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class JsonApiResourceIdentifierArrayFieldMapperBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private String idFieldName;
      @SuppressFBWarnings(justification = "generated code")
      private ArrayList<String> supportedMetaFields;

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

      @SuppressFBWarnings(justification = "generated code")
      public JsonApiResourceIdentifierArrayFieldMapper.JsonApiResourceIdentifierArrayFieldMapperBuilder idFieldName(String idFieldName) {
         this.idFieldName = idFieldName;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public JsonApiResourceIdentifierArrayFieldMapper.JsonApiResourceIdentifierArrayFieldMapperBuilder supportedMetaField(String supportedMetaField) {
         if (this.supportedMetaFields == null) {
            this.supportedMetaFields = new ArrayList<>();
         }

         this.supportedMetaFields.add(supportedMetaField);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public JsonApiResourceIdentifierArrayFieldMapper.JsonApiResourceIdentifierArrayFieldMapperBuilder supportedMetaFields(
         Collection<? extends String> supportedMetaFields
      ) {
         if (supportedMetaFields == null) {
            throw new NullPointerException("supportedMetaFields cannot be null");
         }

         if (this.supportedMetaFields == null) {
            this.supportedMetaFields = new ArrayList<>();
         }

         this.supportedMetaFields.addAll(supportedMetaFields);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public JsonApiResourceIdentifierArrayFieldMapper.JsonApiResourceIdentifierArrayFieldMapperBuilder clearSupportedMetaFields() {
         if (this.supportedMetaFields != null) {
            this.supportedMetaFields.clear();
         }

         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public JsonApiResourceIdentifierArrayFieldMapper build() {
         return new JsonApiResourceIdentifierArrayFieldMapper(
            this.idFieldName, switch (this.supportedMetaFields == null ? 0 : this.supportedMetaFields.size()) {
               case 0 -> Collections.emptyList();
               case 1 -> Collections.singletonList(this.supportedMetaFields.get(0));
               default -> Collections.unmodifiableList(new ArrayList<>(this.supportedMetaFields));
            }
         );
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "JsonApiResourceIdentifierArrayFieldMapper.JsonApiResourceIdentifierArrayFieldMapperBuilder(idFieldName="
            + this.idFieldName
            + ", supportedMetaFields="
            + this.supportedMetaFields
            + ")";
      }
   }
}
