package be.iminds.ilabt.jfed.lowlevel;

import be.iminds.ilabt.jfed.lowlevel.connection.ConnectionConfig;

import java.util.Objects;

/**
 * AuthorityType represents the type of server we are connecting to.
 *
 * This should not be used anymore. It has been replaced by fedmon json and ApiInfo
 * This is only left for use in backward compatibility code related to SfaAuthority and testbeds.xml
 */
@Deprecated
public class ServerType {
    public enum GeniServerRole {
        PlanetLabSliceRegistry,
        PROTOGENI_SA,
        AM,
        PROTOGENI_CH,
        GENI_CH,
        GENI_CH_SA,
        GENI_CH_MA,
        SCS,
        GTS_REST(ConnectionConfig.Authentication.HTTP_BASIC, /*ConnectionBuilder.Protocol.HTTP,*/ ConnectionConfig.Type.HTTP),
        Fedmon(ConnectionConfig.Authentication.NONE, /*ConnectionBuilder.Protocol.HTTPS,*/ ConnectionConfig.Type.HTTP),
        ReservationCalendarPage(ConnectionConfig.Authentication.NONE, ConnectionConfig.Type.HTTP),
        SLA_COLLECTOR(ConnectionConfig.Authentication.HTTP_BASIC, ConnectionConfig.Type.HTTP),
        REPUTATION_SERVICE(ConnectionConfig.Authentication.NONE, ConnectionConfig.Type.HTTP),
        ;

        private final ConnectionConfig.Authentication authentication;
        //        private final ConnectionBuilder.Protocol protocol;
        private final ConnectionConfig.Type connectionType;


        /**
         * All parameters may be null. When not null, they only list the defaults.
         * But most time, for authentication and connectionType they will also be the only allowed value.
         **/
        GeniServerRole(ConnectionConfig.Authentication authentication, /*ConnectionBuilder.Protocol protocol,*/ ConnectionConfig.Type connectionType) {
            this.authentication = authentication;
//            this.protocol = protocol;
            this.connectionType = connectionType;
        }

        GeniServerRole() {
            this.authentication = ConnectionConfig.Authentication.SSL_CLIENT_AUTH;
//            this.protocol = ConnectionBuilder.Protocol.HTTPS;
            this.connectionType = ConnectionConfig.Type.SFA;
        }

        public ConnectionConfig.Authentication getAuthentication() {
            return authentication;
        }

        public ConnectionConfig.Type getConnectionType() {
            return connectionType;
        }

//        public ConnectionBuilder.Protocol getProtocol() {
//            return protocol;
//        }
    }

    private final GeniServerRole role;
    private final String version;

    public ServerType(GeniServerRole role, int version) {
        this.role = role;
        this.version = version + "";
    }

    public ServerType(GeniServerRole role, String version) {
        this.role = role;
        this.version = version;
    }

    public GeniServerRole getRole() {
        return role;
    }

    public String getVersion() {
        return version;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ServerType that = (ServerType) o;

        if (role != that.role) return false;
        if (version != null ? !Objects.equals(version, that.version) : that.version != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = role != null ? role.hashCode() : 0;
        result = 31 * result + (version != null ? version.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "ServerType{" +
                "\"" + role +
                "\" \"" + version + '\"' +
                '}';
    }

////    /* helpers */
//    public static void addSAToUrlMap(Map<AuthorityType, URL> map, int version, URL url) {
//        map.put(new AuthorityType(GeniServerRole.SA, version), url);
//    }
//    public static void addSAToUrlMap(Map<AuthorityType, URL> map, int version, String url) throws MalformedURLException {
//        map.put(new AuthorityType(GeniServerRole.SA, version), new URL(url));
//    }
//    public static void addAMToUrlMap(Map<AuthorityType, URL> map, int version, URL url) {
//        map.put(new AuthorityType(GeniServerRole.AM, version), url);
//    }
//    public static void addAMToUrlMap(Map<AuthorityType, URL> map, int version, String url) throws MalformedURLException {
//        map.put(new AuthorityType(GeniServerRole.AM, version), new URL(url));
//    }
}
