package com.airlock.iam.common.infrastructure.i18n;

import com.airlock.iam.base.domain.service.uri.IamUriBuilder;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.nio.file.Path;
import java.util.Optional;

public class I18nRequest {
   private HttpServletRequest request;
   private HttpServletResponse response;
   private String basePath;
   private String defaultI18nPath;
   private String customI18nPath;
   private UiResourceIdentifier uiResourceIdentifier;

   public static I18nRequest createI18nRequest(HttpServletRequest request, HttpServletResponse response) {
      Path resource = Path.of(request.getPathInfo());
      UiResourceIdentifier uiResourceIdentifier = UiResourceIdentifier.withoutResourceSet(resource);
      return i18nRequestBuilder(request, response).defaultI18nPath("").customI18nPath("").basePath("").uiResourceIdentifier(uiResourceIdentifier).build();
   }

   public static I18nRequest createI18nRequest(
      HttpServletRequest request, HttpServletResponse response, String basePath, String defaultI18nPath, String customI18nPath
   ) {
      Path resource = Path.of(request.getPathInfo());
      UiResourceIdentifier uiResourceIdentifier = UiResourceIdentifier.withoutResourceSet(resource);
      return i18nRequestBuilder(request, response)
         .defaultI18nPath(defaultI18nPath)
         .customI18nPath(customI18nPath)
         .basePath(basePath)
         .uiResourceIdentifier(uiResourceIdentifier)
         .build();
   }

   public static I18nRequest createI18nRequest(
      HttpServletRequest request, HttpServletResponse response, String basePath, String defaultI18nPath, String customI18nPath, String uiResourceSetName
   ) {
      Path resource = Path.of(request.getPathInfo());
      UiResourceIdentifier uiResourceIdentifier = UiResourceIdentifier.withResourceSet(resource, uiResourceSetName);
      return i18nRequestBuilder(request, response)
         .defaultI18nPath(defaultI18nPath)
         .customI18nPath(customI18nPath)
         .basePath(basePath)
         .uiResourceIdentifier(uiResourceIdentifier)
         .build();
   }

   private static I18nRequest.I18nRequestBuilder i18nRequestBuilder(HttpServletRequest request, HttpServletResponse response) {
      return builder().request(request).response(response);
   }

   public long getIfModifiedSince() {
      return this.request.getHeader("If-Modified-Since") != null ? this.request.getDateHeader("If-Modified-Since") : -1L;
   }

   public Optional<String> getEtag() {
      return Optional.ofNullable(this.request.getHeader("If-None-Match"));
   }

   public String getDefaultResourcePath() {
      return IamUriBuilder.fromPath(this.basePath).path(this.defaultI18nPath).path(this.uiResourceIdentifier.getResource()).build().getPath();
   }

   public String getCustomResourcePath() {
      IamUriBuilder customResourcePath = IamUriBuilder.fromPath(this.basePath);
      this.uiResourceIdentifier.getResourceSetName().ifPresent(customResourcePath::path);
      return customResourcePath.path(this.customI18nPath).path(this.uiResourceIdentifier.getResource()).build().getPath();
   }

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

   @SuppressFBWarnings(justification = "generated code")
   private I18nRequest(
      HttpServletRequest request,
      HttpServletResponse response,
      String basePath,
      String defaultI18nPath,
      String customI18nPath,
      UiResourceIdentifier uiResourceIdentifier
   ) {
      this.request = request;
      this.response = response;
      this.basePath = basePath;
      this.defaultI18nPath = defaultI18nPath;
      this.customI18nPath = customI18nPath;
      this.uiResourceIdentifier = uiResourceIdentifier;
   }

   @SuppressFBWarnings(justification = "generated code")
   public HttpServletRequest getRequest() {
      return this.request;
   }

   @SuppressFBWarnings(justification = "generated code")
   public HttpServletResponse getResponse() {
      return this.response;
   }

   @SuppressFBWarnings(justification = "generated code")
   public UiResourceIdentifier getUiResourceIdentifier() {
      return this.uiResourceIdentifier;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class I18nRequestBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private HttpServletRequest request;
      @SuppressFBWarnings(justification = "generated code")
      private HttpServletResponse response;
      @SuppressFBWarnings(justification = "generated code")
      private String basePath;
      @SuppressFBWarnings(justification = "generated code")
      private String defaultI18nPath;
      @SuppressFBWarnings(justification = "generated code")
      private String customI18nPath;
      @SuppressFBWarnings(justification = "generated code")
      private UiResourceIdentifier uiResourceIdentifier;

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

      @SuppressFBWarnings(justification = "generated code")
      public I18nRequest.I18nRequestBuilder request(HttpServletRequest request) {
         this.request = request;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public I18nRequest.I18nRequestBuilder response(HttpServletResponse response) {
         this.response = response;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public I18nRequest.I18nRequestBuilder basePath(String basePath) {
         this.basePath = basePath;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public I18nRequest.I18nRequestBuilder defaultI18nPath(String defaultI18nPath) {
         this.defaultI18nPath = defaultI18nPath;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public I18nRequest.I18nRequestBuilder customI18nPath(String customI18nPath) {
         this.customI18nPath = customI18nPath;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public I18nRequest.I18nRequestBuilder uiResourceIdentifier(UiResourceIdentifier uiResourceIdentifier) {
         this.uiResourceIdentifier = uiResourceIdentifier;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public I18nRequest build() {
         return new I18nRequest(this.request, this.response, this.basePath, this.defaultI18nPath, this.customI18nPath, this.uiResourceIdentifier);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "I18nRequest.I18nRequestBuilder(request="
            + this.request
            + ", response="
            + this.response
            + ", basePath="
            + this.basePath
            + ", defaultI18nPath="
            + this.defaultI18nPath
            + ", customI18nPath="
            + this.customI18nPath
            + ", uiResourceIdentifier="
            + this.uiResourceIdentifier
            + ")";
      }
   }
}
