package io.jooby;

import com.typesafe.config.Config;

import javax.annotation.Nonnull;
import java.util.Optional;

public final class SSLOptions {
  public static final String X509 = "X509";

  public static final String PKCS12 = "PKCS12";

  private String password;

  private String type;

  private String cert;

  private String privateKey;

  private SSLOptions() {
  }

  public String getType() {
    return type;
  }

  public SSLOptions setType(String type) {
    this.type = type;
    return this;
  }

  public String getCert() {
    return cert;
  }

  public SSLOptions setCert(String cert) {
    this.cert = cert;
    return this;
  }

  public String getPrivateKey() {
    return privateKey;
  }

  public SSLOptions setPrivateKey(String privateKey) {
    this.privateKey = privateKey;
    return this;
  }

  public static SSLOptions x509(String crt, String key) {
    SSLOptions options = new SSLOptions();
    options.setType(X509);
    options.setPrivateKey(key);
    options.setCert(crt);
    return options;
  }

  public static SSLOptions x509() {
    return x509("io/jooby/ssl/localhost.crt", "io/jooby/ssl/localhost.key");
  }

  public static SSLOptions pkcs12(String cert, String password) {
    SSLOptions options = new SSLOptions();
    options.setType(PKCS12);
    options.setCert(cert);
    options.setPassword(password);
    return options;
  }

  public static SSLOptions pkcs12() {
    return pkcs12("io/jooby/ssl/localhost.p12", "changeit");
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public String getPassword() {
    return password;
  }

  public static @Nonnull Optional<SSLOptions> parse(@Nonnull Config conf) {
    if (conf.hasPath("server.ssl")) {
      SSLOptions options = new SSLOptions();
      if (conf.hasPath("sever.ssl.type")) {
        String type = conf.getString("server.ssl.type").toUpperCase();
        if (type.equals(PKCS12)) {
          options.setType(type);
        } else if (type.equals(X509)) {
          options.setType(type);
        } else {
          throw new IllegalArgumentException("Unsupported SSL type: " + type.toUpperCase());
        }
      } else {
        options.setType(PKCS12);
      }
      String type = options.getType();
      switch (type) {

      }

      return Optional.of(options);
    }
    return Optional.empty();
  }

  @Override public String toString() {
    return type;
  }
}

@@ -1606,32 +1606,34 @@
  password;%0A  %7D%0A%0A
+//
   public static 
@@ -1688,24 +1688,26 @@
 fig conf) %7B%0A
+//
     if (conf
@@ -1728,24 +1728,26 @@
 er.ssl%22)) %7B%0A
+//
       SSLOpt
@@ -1775,24 +1775,26 @@
 LOptions();%0A
+//
       if (co
@@ -1821,24 +1821,26 @@
 l.type%22)) %7B%0A
+//
         Stri
@@ -1894,24 +1894,26 @@
 pperCase();%0A
+//
         if (
@@ -1931,24 +1931,26 @@
 (PKCS12)) %7B%0A
+//
           op
@@ -1962,32 +1962,34 @@
 .setType(type);%0A
+//
         %7D else i
@@ -2008,24 +2008,26 @@
 ls(X509)) %7B%0A
+//
           op
@@ -2047,16 +2047,18 @@
 (type);%0A
+//
         
@@ -2058,32 +2058,34 @@
        %7D else %7B%0A
+//
           throw 
@@ -2161,16 +2161,18 @@
 ase());%0A
+//
         
@@ -2169,24 +2169,26 @@
 //        %7D%0A
+//
       %7D else
@@ -2190,16 +2190,18 @@
  else %7B%0A
+//
         
@@ -2225,24 +2225,26 @@
 KCS12);%0A
+//
       %7D%0A
       St
@@ -2231,24 +2231,26 @@
 ;%0A//      %7D%0A
+//
       String
@@ -2272,24 +2272,26 @@
 .getType();%0A
+//
       switch
@@ -2304,18 +2304,61 @@
 ) %7B%0A
-%0A      %7D%0A%0A
+//        case X509: %7B%0A//%0A//        %7D%0A//      %7D%0A//%0A//
     
@@ -2388,22 +2388,26 @@
 tions);%0A
+//
     %7D%0A
+//
     retu
@@ -2427,16 +2427,18 @@
 mpty();%0A
+//
   %7D%0A%0A  @

remove creation from config object (need redo it)