package com.airlock.iam.application.parameters.domain.model;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public final class WebServerParameters {
   private final WebServerParameters.ConnectorParameters httpConnector;
   private final WebServerParameters.ConnectorParameters httpsConnector;
   private final Path workDir;
   private final WebServerLogParameters webServerLog;
   private final WebServerLogParameters accessLog;

   public static SecureStore secureStore(String type, Path file, String password) {
      return new SecureStore(type, Objects.requireNonNull(file), Objects.requireNonNull(password));
   }

   public Optional<WebServerParameters.ConnectorParameters> httpConnector() {
      return Optional.ofNullable(this.httpConnector);
   }

   public Optional<WebServerParameters.ConnectorParameters> httpsConnector() {
      return Optional.ofNullable(this.httpsConnector);
   }

   public Collection<WebServerParameters.ConnectorParameters> connectors() {
      return Stream.of(this.httpConnector, this.httpsConnector).filter(Objects::nonNull).collect(Collectors.toList());
   }

   @SuppressFBWarnings(justification = "generated code")
   public WebServerParameters(
      WebServerParameters.ConnectorParameters httpConnector,
      WebServerParameters.ConnectorParameters httpsConnector,
      Path workDir,
      WebServerLogParameters webServerLog,
      WebServerLogParameters accessLog
   ) {
      this.httpConnector = httpConnector;
      this.httpsConnector = httpsConnector;
      this.workDir = workDir;
      this.webServerLog = webServerLog;
      this.accessLog = accessLog;
   }

   @SuppressFBWarnings(justification = "generated code")
   public Path workDir() {
      return this.workDir;
   }

   @SuppressFBWarnings(justification = "generated code")
   public WebServerLogParameters webServerLog() {
      return this.webServerLog;
   }

   @SuppressFBWarnings(justification = "generated code")
   public WebServerLogParameters accessLog() {
      return this.accessLog;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof WebServerParameters other)) {
         return false;
      } else {
         Object this$httpConnector = this.httpConnector;
         Object other$httpConnector = other.httpConnector;
         if (this$httpConnector == null ? other$httpConnector == null : this$httpConnector.equals(other$httpConnector)) {
            Object this$httpsConnector = this.httpsConnector;
            Object other$httpsConnector = other.httpsConnector;
            if (this$httpsConnector == null ? other$httpsConnector == null : this$httpsConnector.equals(other$httpsConnector)) {
               Object this$workDir = this.workDir;
               Object other$workDir = other.workDir;
               if (this$workDir == null ? other$workDir == null : this$workDir.equals(other$workDir)) {
                  Object this$webServerLog = this.webServerLog;
                  Object other$webServerLog = other.webServerLog;
                  if (this$webServerLog == null ? other$webServerLog == null : this$webServerLog.equals(other$webServerLog)) {
                     Object this$accessLog = this.accessLog;
                     Object other$accessLog = other.accessLog;
                     return this$accessLog == null ? other$accessLog == null : this$accessLog.equals(other$accessLog);
                  } else {
                     return false;
                  }
               } else {
                  return false;
               }
            } else {
               return false;
            }
         } else {
            return false;
         }
      }
   }

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "WebServerParameters(httpConnector="
         + this.httpConnector()
         + ", httpsConnector="
         + this.httpsConnector()
         + ", workDir="
         + this.workDir()
         + ", webServerLog="
         + this.webServerLog()
         + ", accessLog="
         + this.accessLog()
         + ")";
   }

   public static final class ConnectorParameters {
      private final int port;
      private final String address;
      private final int maxThreads;
      private final int maxHttpHeaderSize;
      private final boolean compression;
      private final WebServerParameters.SecureConnectorParameters secureParameters;

      public Optional<WebServerParameters.SecureConnectorParameters> secureParameters() {
         return Optional.ofNullable(this.secureParameters);
      }

      public String scheme() {
         return this.secureParameters == null ? "http" : "https";
      }

      public String toShortString() {
         return String.format("%s://%s:%s", this.scheme(), this.address, this.port);
      }

      @SuppressFBWarnings(justification = "generated code")
      ConnectorParameters(
         int port, String address, int maxThreads, int maxHttpHeaderSize, boolean compression, WebServerParameters.SecureConnectorParameters secureParameters
      ) {
         this.port = port;
         this.address = address;
         this.maxThreads = maxThreads;
         this.maxHttpHeaderSize = maxHttpHeaderSize;
         this.compression = compression;
         this.secureParameters = secureParameters;
      }

      @SuppressFBWarnings(justification = "generated code")
      public static WebServerParameters.ConnectorParameters.ConnectorParametersBuilder builder() {
         return new WebServerParameters.ConnectorParameters.ConnectorParametersBuilder();
      }

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

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

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

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

      @SuppressFBWarnings(justification = "generated code")
      public boolean compression() {
         return this.compression;
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public boolean equals(Object o) {
         if (o == this) {
            return true;
         } else if (!(o instanceof WebServerParameters.ConnectorParameters other)) {
            return false;
         } else if (this.port != other.port) {
            return false;
         } else if (this.maxThreads != other.maxThreads) {
            return false;
         } else if (this.maxHttpHeaderSize != other.maxHttpHeaderSize) {
            return false;
         } else if (this.compression != other.compression) {
            return false;
         } else {
            Object this$address = this.address;
            Object other$address = other.address;
            if (this$address == null ? other$address == null : this$address.equals(other$address)) {
               Object this$secureParameters = this.secureParameters;
               Object other$secureParameters = other.secureParameters;
               return this$secureParameters == null ? other$secureParameters == null : this$secureParameters.equals(other$secureParameters);
            } else {
               return false;
            }
         }
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public int hashCode() {
         int PRIME = 59;
         int result = 1;
         result = result * 59 + this.port;
         result = result * 59 + this.maxThreads;
         result = result * 59 + this.maxHttpHeaderSize;
         result = result * 59 + (this.compression ? 79 : 97);
         Object $address = this.address;
         result = result * 59 + ($address == null ? 43 : $address.hashCode());
         Object $secureParameters = this.secureParameters;
         return result * 59 + ($secureParameters == null ? 43 : $secureParameters.hashCode());
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "WebServerParameters.ConnectorParameters(port="
            + this.port()
            + ", address="
            + this.address()
            + ", maxThreads="
            + this.maxThreads()
            + ", maxHttpHeaderSize="
            + this.maxHttpHeaderSize()
            + ", compression="
            + this.compression()
            + ", secureParameters="
            + this.secureParameters()
            + ")";
      }

      @SuppressFBWarnings(justification = "generated code")
      public static class ConnectorParametersBuilder {
         @SuppressFBWarnings(justification = "generated code")
         private int port;
         @SuppressFBWarnings(justification = "generated code")
         private String address;
         @SuppressFBWarnings(justification = "generated code")
         private int maxThreads;
         @SuppressFBWarnings(justification = "generated code")
         private int maxHttpHeaderSize;
         @SuppressFBWarnings(justification = "generated code")
         private boolean compression;
         @SuppressFBWarnings(justification = "generated code")
         private WebServerParameters.SecureConnectorParameters secureParameters;

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

         @SuppressFBWarnings(justification = "generated code")
         public WebServerParameters.ConnectorParameters.ConnectorParametersBuilder port(int port) {
            this.port = port;
            return this;
         }

         @SuppressFBWarnings(justification = "generated code")
         public WebServerParameters.ConnectorParameters.ConnectorParametersBuilder address(String address) {
            this.address = address;
            return this;
         }

         @SuppressFBWarnings(justification = "generated code")
         public WebServerParameters.ConnectorParameters.ConnectorParametersBuilder maxThreads(int maxThreads) {
            this.maxThreads = maxThreads;
            return this;
         }

         @SuppressFBWarnings(justification = "generated code")
         public WebServerParameters.ConnectorParameters.ConnectorParametersBuilder maxHttpHeaderSize(int maxHttpHeaderSize) {
            this.maxHttpHeaderSize = maxHttpHeaderSize;
            return this;
         }

         @SuppressFBWarnings(justification = "generated code")
         public WebServerParameters.ConnectorParameters.ConnectorParametersBuilder compression(boolean compression) {
            this.compression = compression;
            return this;
         }

         @SuppressFBWarnings(justification = "generated code")
         public WebServerParameters.ConnectorParameters.ConnectorParametersBuilder secureParameters(
            WebServerParameters.SecureConnectorParameters secureParameters
         ) {
            this.secureParameters = secureParameters;
            return this;
         }

         @SuppressFBWarnings(justification = "generated code")
         public WebServerParameters.ConnectorParameters build() {
            return new WebServerParameters.ConnectorParameters(
               this.port, this.address, this.maxThreads, this.maxHttpHeaderSize, this.compression, this.secureParameters
            );
         }

         @SuppressFBWarnings(justification = "generated code")
         @Override
         public String toString() {
            return "WebServerParameters.ConnectorParameters.ConnectorParametersBuilder(port="
               + this.port
               + ", address="
               + this.address
               + ", maxThreads="
               + this.maxThreads
               + ", maxHttpHeaderSize="
               + this.maxHttpHeaderSize
               + ", compression="
               + this.compression
               + ", secureParameters="
               + this.secureParameters
               + ")";
         }
      }
   }

   public static final class SecureConnectorParameters {
      private final List<String> tlsProtocols;
      private final String cipherSuites;
      private final SecureStore keyStore;
      private final SecureStore trustStore;
      private final String clientAuth;

      public Optional<SecureStore> trustStore() {
         return Optional.ofNullable(this.trustStore);
      }

      @SuppressFBWarnings(justification = "generated code")
      SecureConnectorParameters(List<String> tlsProtocols, String cipherSuites, SecureStore keyStore, SecureStore trustStore, String clientAuth) {
         this.tlsProtocols = tlsProtocols;
         this.cipherSuites = cipherSuites;
         this.keyStore = keyStore;
         this.trustStore = trustStore;
         this.clientAuth = clientAuth;
      }

      @SuppressFBWarnings(justification = "generated code")
      public static WebServerParameters.SecureConnectorParameters.SecureConnectorParametersBuilder builder() {
         return new WebServerParameters.SecureConnectorParameters.SecureConnectorParametersBuilder();
      }

      @SuppressFBWarnings(justification = "generated code")
      public List<String> tlsProtocols() {
         return this.tlsProtocols;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      public SecureStore keyStore() {
         return this.keyStore;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public boolean equals(Object o) {
         if (o == this) {
            return true;
         } else if (!(o instanceof WebServerParameters.SecureConnectorParameters other)) {
            return false;
         } else {
            Object this$tlsProtocols = this.tlsProtocols;
            Object other$tlsProtocols = other.tlsProtocols;
            if (this$tlsProtocols == null ? other$tlsProtocols == null : this$tlsProtocols.equals(other$tlsProtocols)) {
               Object this$cipherSuites = this.cipherSuites;
               Object other$cipherSuites = other.cipherSuites;
               if (this$cipherSuites == null ? other$cipherSuites == null : this$cipherSuites.equals(other$cipherSuites)) {
                  Object this$keyStore = this.keyStore;
                  Object other$keyStore = other.keyStore;
                  if (this$keyStore == null ? other$keyStore == null : this$keyStore.equals(other$keyStore)) {
                     Object this$trustStore = this.trustStore;
                     Object other$trustStore = other.trustStore;
                     if (this$trustStore == null ? other$trustStore == null : this$trustStore.equals(other$trustStore)) {
                        Object this$clientAuth = this.clientAuth;
                        Object other$clientAuth = other.clientAuth;
                        return this$clientAuth == null ? other$clientAuth == null : this$clientAuth.equals(other$clientAuth);
                     } else {
                        return false;
                     }
                  } else {
                     return false;
                  }
               } else {
                  return false;
               }
            } else {
               return false;
            }
         }
      }

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

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "WebServerParameters.SecureConnectorParameters(tlsProtocols="
            + this.tlsProtocols()
            + ", cipherSuites="
            + this.cipherSuites()
            + ", keyStore="
            + this.keyStore()
            + ", trustStore="
            + this.trustStore()
            + ", clientAuth="
            + this.clientAuth()
            + ")";
      }

      @SuppressFBWarnings(justification = "generated code")
      public static class SecureConnectorParametersBuilder {
         @SuppressFBWarnings(justification = "generated code")
         private List<String> tlsProtocols;
         @SuppressFBWarnings(justification = "generated code")
         private String cipherSuites;
         @SuppressFBWarnings(justification = "generated code")
         private SecureStore keyStore;
         @SuppressFBWarnings(justification = "generated code")
         private SecureStore trustStore;
         @SuppressFBWarnings(justification = "generated code")
         private String clientAuth;

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

         @SuppressFBWarnings(justification = "generated code")
         public WebServerParameters.SecureConnectorParameters.SecureConnectorParametersBuilder tlsProtocols(List<String> tlsProtocols) {
            this.tlsProtocols = tlsProtocols;
            return this;
         }

         @SuppressFBWarnings(justification = "generated code")
         public WebServerParameters.SecureConnectorParameters.SecureConnectorParametersBuilder cipherSuites(String cipherSuites) {
            this.cipherSuites = cipherSuites;
            return this;
         }

         @SuppressFBWarnings(justification = "generated code")
         public WebServerParameters.SecureConnectorParameters.SecureConnectorParametersBuilder keyStore(SecureStore keyStore) {
            this.keyStore = keyStore;
            return this;
         }

         @SuppressFBWarnings(justification = "generated code")
         public WebServerParameters.SecureConnectorParameters.SecureConnectorParametersBuilder trustStore(SecureStore trustStore) {
            this.trustStore = trustStore;
            return this;
         }

         @SuppressFBWarnings(justification = "generated code")
         public WebServerParameters.SecureConnectorParameters.SecureConnectorParametersBuilder clientAuth(String clientAuth) {
            this.clientAuth = clientAuth;
            return this;
         }

         @SuppressFBWarnings(justification = "generated code")
         public WebServerParameters.SecureConnectorParameters build() {
            return new WebServerParameters.SecureConnectorParameters(this.tlsProtocols, this.cipherSuites, this.keyStore, this.trustStore, this.clientAuth);
         }

         @SuppressFBWarnings(justification = "generated code")
         @Override
         public String toString() {
            return "WebServerParameters.SecureConnectorParameters.SecureConnectorParametersBuilder(tlsProtocols="
               + this.tlsProtocols
               + ", cipherSuites="
               + this.cipherSuites
               + ", keyStore="
               + this.keyStore
               + ", trustStore="
               + this.trustStore
               + ", clientAuth="
               + this.clientAuth
               + ")";
         }
      }
   }
}
