package com.airlock.iam.common.domain.model.sms;

import com.airlock.iam.base.domain.service.uri.IamUriBuilder;
import com.cloudhopper.commons.charset.Charset;
import com.cloudhopper.commons.charset.CharsetUtil;
import com.cloudhopper.smpp.SmppSessionConfiguration;
import com.cloudhopper.smpp.ssl.SslConfiguration;
import com.cloudhopper.smpp.type.LoggingOptions;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.net.URI;
import java.util.Optional;
import lombok.NonNull;

public class SmppSmsGatewayConfiguration {
   private final int windowSize;
   @NonNull
   private final String sessionName;
   @NonNull
   private final SmppBindType smppBindType;
   private final long connectionTimeout;
   @NonNull
   private final String serverIp;
   private final int serverPort;
   @NonNull
   private final String accountUsername;
   @NonNull
   private final String accountPassword;
   private final byte sourceTon;
   private final byte destTon;
   private final byte sourceNpi;
   private final byte destNpi;
   @NonNull
   private final SmppDataCodingScheme dataCodingScheme;
   @NonNull
   private final SmppFlashMode flashMode;
   private final int sendTimeout;
   private final int maxNumberOfSessions;
   @NonNull
   private final String messageEncodingCharset;
   private final boolean enableDebugging;
   @NonNull
   private final Optional<String> trustStorePath;
   @NonNull
   private final Optional<String> trustStorePassword;
   private final boolean useSsl;
   private SmppSessionConfiguration smppSessionConfiguration;

   private void createSmppSessionConfiguration() {
      this.smppSessionConfiguration = new SmppSessionConfiguration();
      this.smppSessionConfiguration.setWindowSize(this.windowSize);
      this.smppSessionConfiguration.setName(this.sessionName);
      this.smppSessionConfiguration.setType(this.smppBindType);
      this.smppSessionConfiguration.setConnectTimeout(this.connectionTimeout);
      this.smppSessionConfiguration.setHost(this.serverIp);
      this.smppSessionConfiguration.setPort(this.serverPort);
      this.smppSessionConfiguration.setSystemId(this.accountUsername);
      this.smppSessionConfiguration.setPassword(this.accountPassword);
      if (this.useSsl) {
         SslConfiguration sslConfig = new SslConfiguration();
         sslConfig.setKeyStorePath(this.trustStorePath.get());
         sslConfig.setKeyStorePassword(this.trustStorePassword.get());
         sslConfig.setTrustStorePath(this.trustStorePath.get());
         sslConfig.setTrustStorePassword(this.trustStorePassword.get());
         this.smppSessionConfiguration.setUseSsl(true);
         this.smppSessionConfiguration.setSslConfiguration(sslConfig);
      }

      this.smppSessionConfiguration.setInterfaceVersion((byte)52);
      this.smppSessionConfiguration.setLoggingOptions(this.getLoggingOptions());
   }

   private LoggingOptions getLoggingOptions() {
      LoggingOptions loggingOptions = new LoggingOptions();
      loggingOptions.setLogPdu(this.enableDebugging);
      loggingOptions.setLogBytes(this.enableDebugging);
      return loggingOptions;
   }

   public synchronized SmppSessionConfiguration getSmppSessionConfiguration() {
      if (this.smppSessionConfiguration == null) {
         this.createSmppSessionConfiguration();
      }

      return this.smppSessionConfiguration;
   }

   public byte getSourceTon() {
      return this.sourceTon;
   }

   public byte getDestTon() {
      return this.destTon;
   }

   public byte getSourceNpi() {
      return this.sourceNpi;
   }

   public byte getDestNpi() {
      return this.destNpi;
   }

   public int getSendTimeout() {
      return this.sendTimeout;
   }

   public int getMaxNumberOfSessions() {
      return this.maxNumberOfSessions;
   }

   public Charset getCharset() {
      return CharsetUtil.map(this.messageEncodingCharset);
   }

   public URI getServiceUri() {
      return IamUriBuilder.uriBuilder().scheme("smpp").host(this.serverIp).port(this.serverPort).build();
   }

   @SuppressFBWarnings(justification = "generated code")
   SmppSmsGatewayConfiguration(
      int windowSize,
      @NonNull String sessionName,
      @NonNull SmppBindType smppBindType,
      long connectionTimeout,
      @NonNull String serverIp,
      int serverPort,
      @NonNull String accountUsername,
      @NonNull String accountPassword,
      byte sourceTon,
      byte destTon,
      byte sourceNpi,
      byte destNpi,
      @NonNull SmppDataCodingScheme dataCodingScheme,
      @NonNull SmppFlashMode flashMode,
      int sendTimeout,
      int maxNumberOfSessions,
      @NonNull String messageEncodingCharset,
      boolean enableDebugging,
      @NonNull Optional<String> trustStorePath,
      @NonNull Optional<String> trustStorePassword,
      boolean useSsl,
      SmppSessionConfiguration smppSessionConfiguration
   ) {
      if (sessionName == null) {
         throw new NullPointerException("sessionName is marked non-null but is null");
      }

      if (smppBindType == null) {
         throw new NullPointerException("smppBindType is marked non-null but is null");
      }

      if (serverIp == null) {
         throw new NullPointerException("serverIp is marked non-null but is null");
      }

      if (accountUsername == null) {
         throw new NullPointerException("accountUsername is marked non-null but is null");
      }

      if (accountPassword == null) {
         throw new NullPointerException("accountPassword is marked non-null but is null");
      }

      if (dataCodingScheme == null) {
         throw new NullPointerException("dataCodingScheme is marked non-null but is null");
      }

      if (flashMode == null) {
         throw new NullPointerException("flashMode is marked non-null but is null");
      }

      if (messageEncodingCharset == null) {
         throw new NullPointerException("messageEncodingCharset is marked non-null but is null");
      }

      if (trustStorePath == null) {
         throw new NullPointerException("trustStorePath is marked non-null but is null");
      }

      if (trustStorePassword == null) {
         throw new NullPointerException("trustStorePassword is marked non-null but is null");
      }

      this.windowSize = windowSize;
      this.sessionName = sessionName;
      this.smppBindType = smppBindType;
      this.connectionTimeout = connectionTimeout;
      this.serverIp = serverIp;
      this.serverPort = serverPort;
      this.accountUsername = accountUsername;
      this.accountPassword = accountPassword;
      this.sourceTon = sourceTon;
      this.destTon = destTon;
      this.sourceNpi = sourceNpi;
      this.destNpi = destNpi;
      this.dataCodingScheme = dataCodingScheme;
      this.flashMode = flashMode;
      this.sendTimeout = sendTimeout;
      this.maxNumberOfSessions = maxNumberOfSessions;
      this.messageEncodingCharset = messageEncodingCharset;
      this.enableDebugging = enableDebugging;
      this.trustStorePath = trustStorePath;
      this.trustStorePassword = trustStorePassword;
      this.useSsl = useSsl;
      this.smppSessionConfiguration = smppSessionConfiguration;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof SmppSmsGatewayConfiguration other)) {
         return false;
      } else {
         if (!other.canEqual(this)) {
            return false;
         }

         if (this.windowSize != other.windowSize) {
            return false;
         }

         if (this.connectionTimeout != other.connectionTimeout) {
            return false;
         }

         if (this.serverPort != other.serverPort) {
            return false;
         }

         if (this.sourceTon != other.sourceTon) {
            return false;
         }

         if (this.destTon != other.destTon) {
            return false;
         }

         if (this.sourceNpi != other.sourceNpi) {
            return false;
         }

         if (this.destNpi != other.destNpi) {
            return false;
         }

         if (this.sendTimeout != other.sendTimeout) {
            return false;
         }

         if (this.maxNumberOfSessions != other.maxNumberOfSessions) {
            return false;
         }

         if (this.enableDebugging != other.enableDebugging) {
            return false;
         }

         if (this.useSsl != other.useSsl) {
            return false;
         }

         Object this$sessionName = this.sessionName;
         Object other$sessionName = other.sessionName;
         if (this$sessionName == null ? other$sessionName == null : this$sessionName.equals(other$sessionName)) {
            Object this$smppBindType = this.smppBindType;
            Object other$smppBindType = other.smppBindType;
            if (this$smppBindType == null ? other$smppBindType == null : this$smppBindType.equals(other$smppBindType)) {
               Object this$serverIp = this.serverIp;
               Object other$serverIp = other.serverIp;
               if (this$serverIp == null ? other$serverIp == null : this$serverIp.equals(other$serverIp)) {
                  Object this$accountUsername = this.accountUsername;
                  Object other$accountUsername = other.accountUsername;
                  if (this$accountUsername == null ? other$accountUsername == null : this$accountUsername.equals(other$accountUsername)) {
                     Object this$accountPassword = this.accountPassword;
                     Object other$accountPassword = other.accountPassword;
                     if (this$accountPassword == null ? other$accountPassword == null : this$accountPassword.equals(other$accountPassword)) {
                        Object this$dataCodingScheme = this.dataCodingScheme;
                        Object other$dataCodingScheme = other.dataCodingScheme;
                        if (this$dataCodingScheme == null ? other$dataCodingScheme == null : this$dataCodingScheme.equals(other$dataCodingScheme)) {
                           Object this$flashMode = this.flashMode;
                           Object other$flashMode = other.flashMode;
                           if (this$flashMode == null ? other$flashMode == null : this$flashMode.equals(other$flashMode)) {
                              Object this$messageEncodingCharset = this.messageEncodingCharset;
                              Object other$messageEncodingCharset = other.messageEncodingCharset;
                              if (this$messageEncodingCharset == null
                                 ? other$messageEncodingCharset == null
                                 : this$messageEncodingCharset.equals(other$messageEncodingCharset)) {
                                 Object this$trustStorePath = this.trustStorePath;
                                 Object other$trustStorePath = other.trustStorePath;
                                 if (this$trustStorePath == null ? other$trustStorePath == null : this$trustStorePath.equals(other$trustStorePath)) {
                                    Object this$trustStorePassword = this.trustStorePassword;
                                    Object other$trustStorePassword = other.trustStorePassword;
                                    return this$trustStorePassword == null
                                       ? other$trustStorePassword == null
                                       : this$trustStorePassword.equals(other$trustStorePassword);
                                 } else {
                                    return false;
                                 }
                              } else {
                                 return false;
                              }
                           } else {
                              return false;
                           }
                        } else {
                           return false;
                        }
                     } else {
                        return false;
                     }
                  } else {
                     return false;
                  }
               } else {
                  return false;
               }
            } else {
               return false;
            }
         } else {
            return false;
         }
      }
   }

   @SuppressFBWarnings(justification = "generated code")
   protected boolean canEqual(Object other) {
      return other instanceof SmppSmsGatewayConfiguration;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public int hashCode() {
      int PRIME = 59;
      int result = 1;
      result = result * 59 + this.windowSize;
      long $connectionTimeout = this.connectionTimeout;
      result = result * 59 + (int)($connectionTimeout >>> 32 ^ $connectionTimeout);
      result = result * 59 + this.serverPort;
      result = result * 59 + this.sourceTon;
      result = result * 59 + this.destTon;
      result = result * 59 + this.sourceNpi;
      result = result * 59 + this.destNpi;
      result = result * 59 + this.sendTimeout;
      result = result * 59 + this.maxNumberOfSessions;
      result = result * 59 + (this.enableDebugging ? 79 : 97);
      result = result * 59 + (this.useSsl ? 79 : 97);
      Object $sessionName = this.sessionName;
      result = result * 59 + ($sessionName == null ? 43 : $sessionName.hashCode());
      Object $smppBindType = this.smppBindType;
      result = result * 59 + ($smppBindType == null ? 43 : $smppBindType.hashCode());
      Object $serverIp = this.serverIp;
      result = result * 59 + ($serverIp == null ? 43 : $serverIp.hashCode());
      Object $accountUsername = this.accountUsername;
      result = result * 59 + ($accountUsername == null ? 43 : $accountUsername.hashCode());
      Object $accountPassword = this.accountPassword;
      result = result * 59 + ($accountPassword == null ? 43 : $accountPassword.hashCode());
      Object $dataCodingScheme = this.dataCodingScheme;
      result = result * 59 + ($dataCodingScheme == null ? 43 : $dataCodingScheme.hashCode());
      Object $flashMode = this.flashMode;
      result = result * 59 + ($flashMode == null ? 43 : $flashMode.hashCode());
      Object $messageEncodingCharset = this.messageEncodingCharset;
      result = result * 59 + ($messageEncodingCharset == null ? 43 : $messageEncodingCharset.hashCode());
      Object $trustStorePath = this.trustStorePath;
      result = result * 59 + ($trustStorePath == null ? 43 : $trustStorePath.hashCode());
      Object $trustStorePassword = this.trustStorePassword;
      return result * 59 + ($trustStorePassword == null ? 43 : $trustStorePassword.hashCode());
   }

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

   @NonNull
   @SuppressFBWarnings(justification = "generated code")
   public SmppDataCodingScheme getDataCodingScheme() {
      return this.dataCodingScheme;
   }

   @NonNull
   @SuppressFBWarnings(justification = "generated code")
   public SmppFlashMode getFlashMode() {
      return this.flashMode;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class SmppSmsGatewayConfigurationBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private int windowSize;
      @SuppressFBWarnings(justification = "generated code")
      private String sessionName;
      @SuppressFBWarnings(justification = "generated code")
      private com.cloudhopper.smpp.SmppBindType smppBindType;
      @SuppressFBWarnings(justification = "generated code")
      private long connectionTimeout;
      @SuppressFBWarnings(justification = "generated code")
      private String serverIp;
      @SuppressFBWarnings(justification = "generated code")
      private int serverPort;
      @SuppressFBWarnings(justification = "generated code")
      private String accountUsername;
      @SuppressFBWarnings(justification = "generated code")
      private String accountPassword;
      @SuppressFBWarnings(justification = "generated code")
      private byte sourceTon;
      @SuppressFBWarnings(justification = "generated code")
      private byte destTon;
      @SuppressFBWarnings(justification = "generated code")
      private byte sourceNpi;
      @SuppressFBWarnings(justification = "generated code")
      private byte destNpi;
      @SuppressFBWarnings(justification = "generated code")
      private SmppDataCodingScheme dataCodingScheme;
      @SuppressFBWarnings(justification = "generated code")
      private SmppFlashMode flashMode;
      @SuppressFBWarnings(justification = "generated code")
      private int sendTimeout;
      @SuppressFBWarnings(justification = "generated code")
      private int maxNumberOfSessions;
      @SuppressFBWarnings(justification = "generated code")
      private String messageEncodingCharset;
      @SuppressFBWarnings(justification = "generated code")
      private boolean enableDebugging;
      @SuppressFBWarnings(justification = "generated code")
      private Optional<String> trustStorePath;
      @SuppressFBWarnings(justification = "generated code")
      private Optional<String> trustStorePassword;
      @SuppressFBWarnings(justification = "generated code")
      private boolean useSsl;
      @SuppressFBWarnings(justification = "generated code")
      private SmppSessionConfiguration smppSessionConfiguration;

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

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

      @SuppressFBWarnings(justification = "generated code")
      public SmppSmsGatewayConfiguration.SmppSmsGatewayConfigurationBuilder sessionName(@NonNull String sessionName) {
         if (sessionName == null) {
            throw new NullPointerException("sessionName is marked non-null but is null");
         }

         this.sessionName = sessionName;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public SmppSmsGatewayConfiguration.SmppSmsGatewayConfigurationBuilder smppBindType(@NonNull SmppBindType smppBindType) {
         if (smppBindType == null) {
            throw new NullPointerException("smppBindType is marked non-null but is null");
         }

         this.smppBindType = smppBindType;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public SmppSmsGatewayConfiguration.SmppSmsGatewayConfigurationBuilder connectionTimeout(long connectionTimeout) {
         this.connectionTimeout = connectionTimeout;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public SmppSmsGatewayConfiguration.SmppSmsGatewayConfigurationBuilder serverIp(@NonNull String serverIp) {
         if (serverIp == null) {
            throw new NullPointerException("serverIp is marked non-null but is null");
         }

         this.serverIp = serverIp;
         return this;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      public SmppSmsGatewayConfiguration.SmppSmsGatewayConfigurationBuilder accountUsername(@NonNull String accountUsername) {
         if (accountUsername == null) {
            throw new NullPointerException("accountUsername is marked non-null but is null");
         }

         this.accountUsername = accountUsername;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public SmppSmsGatewayConfiguration.SmppSmsGatewayConfigurationBuilder accountPassword(@NonNull String accountPassword) {
         if (accountPassword == null) {
            throw new NullPointerException("accountPassword is marked non-null but is null");
         }

         this.accountPassword = accountPassword;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public SmppSmsGatewayConfiguration.SmppSmsGatewayConfigurationBuilder sourceTon(byte sourceTon) {
         this.sourceTon = sourceTon;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public SmppSmsGatewayConfiguration.SmppSmsGatewayConfigurationBuilder destTon(byte destTon) {
         this.destTon = destTon;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public SmppSmsGatewayConfiguration.SmppSmsGatewayConfigurationBuilder sourceNpi(byte sourceNpi) {
         this.sourceNpi = sourceNpi;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public SmppSmsGatewayConfiguration.SmppSmsGatewayConfigurationBuilder destNpi(byte destNpi) {
         this.destNpi = destNpi;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public SmppSmsGatewayConfiguration.SmppSmsGatewayConfigurationBuilder dataCodingScheme(@NonNull SmppDataCodingScheme dataCodingScheme) {
         if (dataCodingScheme == null) {
            throw new NullPointerException("dataCodingScheme is marked non-null but is null");
         }

         this.dataCodingScheme = dataCodingScheme;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public SmppSmsGatewayConfiguration.SmppSmsGatewayConfigurationBuilder flashMode(@NonNull SmppFlashMode flashMode) {
         if (flashMode == null) {
            throw new NullPointerException("flashMode is marked non-null but is null");
         }

         this.flashMode = flashMode;
         return this;
      }

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

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

      @SuppressFBWarnings(justification = "generated code")
      public SmppSmsGatewayConfiguration.SmppSmsGatewayConfigurationBuilder messageEncodingCharset(@NonNull String messageEncodingCharset) {
         if (messageEncodingCharset == null) {
            throw new NullPointerException("messageEncodingCharset is marked non-null but is null");
         }

         this.messageEncodingCharset = messageEncodingCharset;
         return this;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      public SmppSmsGatewayConfiguration.SmppSmsGatewayConfigurationBuilder trustStorePath(@NonNull Optional<String> trustStorePath) {
         if (trustStorePath == null) {
            throw new NullPointerException("trustStorePath is marked non-null but is null");
         }

         this.trustStorePath = trustStorePath;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public SmppSmsGatewayConfiguration.SmppSmsGatewayConfigurationBuilder trustStorePassword(@NonNull Optional<String> trustStorePassword) {
         if (trustStorePassword == null) {
            throw new NullPointerException("trustStorePassword is marked non-null but is null");
         }

         this.trustStorePassword = trustStorePassword;
         return this;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      public SmppSmsGatewayConfiguration.SmppSmsGatewayConfigurationBuilder smppSessionConfiguration(SmppSessionConfiguration smppSessionConfiguration) {
         this.smppSessionConfiguration = smppSessionConfiguration;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public SmppSmsGatewayConfiguration build() {
         return new SmppSmsGatewayConfiguration(
            this.windowSize,
            this.sessionName,
            this.smppBindType,
            this.connectionTimeout,
            this.serverIp,
            this.serverPort,
            this.accountUsername,
            this.accountPassword,
            this.sourceTon,
            this.destTon,
            this.sourceNpi,
            this.destNpi,
            this.dataCodingScheme,
            this.flashMode,
            this.sendTimeout,
            this.maxNumberOfSessions,
            this.messageEncodingCharset,
            this.enableDebugging,
            this.trustStorePath,
            this.trustStorePassword,
            this.useSsl,
            this.smppSessionConfiguration
         );
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "SmppSmsGatewayConfiguration.SmppSmsGatewayConfigurationBuilder(windowSize="
            + this.windowSize
            + ", sessionName="
            + this.sessionName
            + ", smppBindType="
            + this.smppBindType
            + ", connectionTimeout="
            + this.connectionTimeout
            + ", serverIp="
            + this.serverIp
            + ", serverPort="
            + this.serverPort
            + ", accountUsername="
            + this.accountUsername
            + ", accountPassword="
            + this.accountPassword
            + ", sourceTon="
            + this.sourceTon
            + ", destTon="
            + this.destTon
            + ", sourceNpi="
            + this.sourceNpi
            + ", destNpi="
            + this.destNpi
            + ", dataCodingScheme="
            + this.dataCodingScheme
            + ", flashMode="
            + this.flashMode
            + ", sendTimeout="
            + this.sendTimeout
            + ", maxNumberOfSessions="
            + this.maxNumberOfSessions
            + ", messageEncodingCharset="
            + this.messageEncodingCharset
            + ", enableDebugging="
            + this.enableDebugging
            + ", trustStorePath="
            + this.trustStorePath
            + ", trustStorePassword="
            + this.trustStorePassword
            + ", useSsl="
            + this.useSsl
            + ", smppSessionConfiguration="
            + this.smppSessionConfiguration
            + ")";
      }
   }
}
