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

import com.airlock.iam.common.infrastructure.ResponseBodyWriter;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

public class I18nResource {
   private String content;
   private String contentHash;
   private long lastModified;

   public boolean isMoreRecentThan(I18nRequest request) {
      if (request.getIfModifiedSince() == -1L) {
         return true;
      } else if (this.withSecondsPrecision(request.getIfModifiedSince()) != this.withSecondsPrecision(this.lastModified)) {
         return true;
      } else {
         return request.getEtag().isEmpty() ? true : !this.contentHash.equals(request.getEtag().get());
      }
   }

   private long withSecondsPrecision(long time) {
      return TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(time));
   }

   public void applyTo(I18nRequest i18nRequest) {
      HttpServletResponse resp = i18nRequest.getResponse();
      resp.setContentType("application/json");
      resp.setCharacterEncoding(StandardCharsets.UTF_8.name());
      resp.addDateHeader("Date", System.currentTimeMillis());
      resp.addDateHeader("Last-Modified", this.getLastModified());
      resp.addHeader("ETag", this.getContentHash());
      resp.addHeader("Cache-Control", "public, max-age=0");
      if (this.isMoreRecentThan(i18nRequest)) {
         resp.setStatus(200);
         ResponseBodyWriter.writeBodyWithoutCommittingResponse(this.getContent(), resp);
      } else {
         resp.setStatus(304);
      }
   }

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

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

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

   @SuppressFBWarnings(justification = "generated code")
   public long getLastModified() {
      return this.lastModified;
   }

   @SuppressFBWarnings(justification = "generated code")
   private I18nResource(String content, String contentHash, long lastModified) {
      this.content = content;
      this.contentHash = contentHash;
      this.lastModified = lastModified;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class I18nResourceBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private String content;
      @SuppressFBWarnings(justification = "generated code")
      private String contentHash;
      @SuppressFBWarnings(justification = "generated code")
      private long lastModified;

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

      @SuppressFBWarnings(justification = "generated code")
      public I18nResource.I18nResourceBuilder content(String content) {
         this.content = content;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public I18nResource.I18nResourceBuilder contentHash(String contentHash) {
         this.contentHash = contentHash;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public I18nResource.I18nResourceBuilder lastModified(long lastModified) {
         this.lastModified = lastModified;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public I18nResource build() {
         return new I18nResource(this.content, this.contentHash, this.lastModified);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "I18nResource.I18nResourceBuilder(content=" + this.content + ", contentHash=" + this.contentHash + ", lastModified=" + this.lastModified + ")";
      }
   }
}
