package be.iminds.ilabt.jfed.lowlevel.connection;

import be.iminds.ilabt.jfed.util.JFedPasswordManager;
import be.iminds.ilabt.jfed.util.JFedTrustStore;

import javax.annotation.Nullable;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.List;

/**
 * ConnectionConfig
 */
public class ConnectionConfig {
    public enum Authentication { NONE, HTTP_BASIC, SSL_CLIENT_AUTH }
    public enum Protocol { HTTP, HTTPS }
    public enum Type { HTTP, SFA }

    private final String serverUrlString;
    private final URL serverUrl;
    @Nullable
    private final JFedConnection.DebugInfo debugInfo;
    private final JFedConnection.ProxyInfo proxyInfo;
    private final ConnectionConfig.Authentication authentication;
    private final JFedPasswordManager.LoginInfo login;
    private final List<X509Certificate> clientCertChain;
    private final PrivateKey privateKey;
    private final ConnectionConfig.Protocol protocol;
    private final JFedTrustStore conTrustStore;
    private final HandleUntrustedCallback handleUntrustedCallback;
    private final Type type;

    /**
     * @param serverUrlString url to connect to (may not be null, unless this is a fake connection used for debugging)
     * @param conTrustStore The trust store to use to verify the server's identity. (may be null for connection that do not need it)
     * @param proxyInfo the proxy to use (may be null if no proxy)
     * @param debugInfo debug information, such as authority and user, and whether debug mode is enabled. (may be null, but even then, the debugInfo field of JFedConnection will never be null)
     */
    public ConnectionConfig(String serverUrlString,
                            @Nullable JFedConnection.DebugInfo debugInfo,
                            JFedConnection.ProxyInfo proxyInfo,
                            Authentication authentication,
                            JFedPasswordManager.LoginInfo login,
                            List<X509Certificate> clientCertChain,
                            PrivateKey privateKey,
                            Protocol protocol,
                            JFedTrustStore conTrustStore,
                            HandleUntrustedCallback handleUntrustedCallback,
                            Type type) throws MalformedURLException {
        this.serverUrlString = serverUrlString;
        this.serverUrl = serverUrlString == null ? null : new URL(serverUrlString);
        this.debugInfo = debugInfo;
        this.proxyInfo = proxyInfo;
        this.authentication = authentication;
        this.login = login;
        this.clientCertChain = clientCertChain;
        this.privateKey = privateKey;
        this.protocol = protocol;
        this.conTrustStore = conTrustStore;
        this.handleUntrustedCallback = handleUntrustedCallback;
        this.type = type;
    }
    /**
     * @param serverUrl url to connect to (may not be null, unless this is a fake connection used for debugging)
     * @param conTrustStore The trust store to use to verify the server's identity. (may be null for connection that do not need it)
     * @param proxyInfo the proxy to use (may be null if no proxy)
     * @param debugInfo debug information, such as authority and user, and whether debug mode is enabled. (may be null, but even then, the debugInfo field of JFedConnection will never be null)
     */
    public ConnectionConfig(URL serverUrl,
                            @Nullable JFedConnection.DebugInfo debugInfo,
                            JFedConnection.ProxyInfo proxyInfo,
                            Authentication authentication,
                            JFedPasswordManager.LoginInfo login,
                            List<X509Certificate> clientCertChain,
                            PrivateKey privateKey,
                            Protocol protocol,
                            JFedTrustStore conTrustStore,
                            HandleUntrustedCallback handleUntrustedCallback,
                            Type type) {
        this.serverUrl = serverUrl;
        this.serverUrlString = serverUrl == null ? null : serverUrl.toExternalForm();
        this.debugInfo = debugInfo;
        this.proxyInfo = proxyInfo;
        this.authentication = authentication;
        this.login = login;
        this.clientCertChain = clientCertChain;
        this.privateKey = privateKey;
        this.protocol = protocol;
        this.conTrustStore = conTrustStore;
        this.handleUntrustedCallback = handleUntrustedCallback;
        this.type = type;
    }

    public String getServerUrlString() {
        return serverUrlString;
    }

    public URL getServerUrl() {
        return serverUrl;
    }

    @Nullable
    public JFedConnection.DebugInfo getDebugInfo() {
        return debugInfo;
    }

    public JFedConnection.ProxyInfo getProxyInfo() {
        return proxyInfo;
    }

    public Authentication getAuthentication() {
        return authentication;
    }

    public JFedPasswordManager.LoginInfo getLogin() {
        return login;
    }

    public List<X509Certificate> getClientCertChain() {
        return clientCertChain;
    }

    public PrivateKey getPrivateKey() {
        return privateKey;
    }

    public Protocol getProtocol() {
        return protocol;
    }

    public JFedTrustStore getConTrustStore() {
        return conTrustStore;
    }

    public HandleUntrustedCallback getHandleUntrustedCallback() {
        return handleUntrustedCallback;
    }

    public Type getType() {
        return type;
    }

}
