package io.appium.java_client.remote;

import javax.annotation.Nullable;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

import static io.appium.java_client.internal.CapabilityHelpers.APPIUM_PREFIX;

// TODO: simplify with lombok as another PR
public class DirectConnect {
    private static final String DIRECT_CONNECT_PROTOCOL = "directConnectProtocol";
    private static final String DIRECT_CONNECT_PATH = "directConnectPath";
    private static final String DIRECT_CONNECT_HOST = "directConnectHost";
    private static final String DIRECT_CONNECT_PORT = "directConnectPort";

    private final String protocol;

    private final String path;

    private final String host;

    private final String port;

    /**
     * Create a DirectConnect instance.
     * @param responseValue is the response body
     */
    public DirectConnect(Map<String, ?> responseValue) {
        this.protocol = this.getDirectConnectValue(responseValue, DIRECT_CONNECT_PROTOCOL);
        this.path = this.getDirectConnectValue(responseValue, DIRECT_CONNECT_PATH);
        this.host = this.getDirectConnectValue(responseValue, DIRECT_CONNECT_HOST);
        this.port = this.getDirectConnectValue(responseValue, DIRECT_CONNECT_PORT);
    }

    public String getProtocol() {
        return protocol;
    }

    @Nullable
    private String getDirectConnectValue(Map<String, ?> responseValue, String key) {
        Object directConnectPath = responseValue.get(APPIUM_PREFIX + key);
        if (directConnectPath != null) {
            return String.valueOf(directConnectPath);
        }
        directConnectPath = responseValue.get(key);
        return directConnectPath == null ? null : String.valueOf(directConnectPath);
    }

    /**
     * Returns true if the {@link DirectConnect} instance member has nonnull values.
     * @return true if each connection information has a nonnull value
     */
    public boolean isValid() {
        return Stream.of(this.protocol, this.path, this.host, this.port).noneMatch(Objects::isNull);
    }

    /**
     * Returns a URL instance built with members in the DirectConnect instance.
     * @return A URL object
     * @throws MalformedURLException if the built url was invalid
     */
    public URL getUrl() throws MalformedURLException {
        String newUrlCandidate = String.format("%s://%s:%s%s", this.protocol, this.host, this.port, this.path);

        try {
            return new URL(newUrlCandidate);
        } catch (MalformedURLException e) {
            throw new MalformedURLException(
                    String.format("The remote server returned an invalid value to build the direct connect URL: %s",
                            newUrlCandidate)
            );
        }
    }
}

@@ -32,16 +32,104 @@
 emote;%0A%0A
+import lombok.AccessLevel;%0Aimport lombok.Getter;%0Aimport lombok.experimental.Accessors;%0A%0A
 import j
@@ -378,51 +378,18 @@
 X;%0A%0A
-// TODO: simplify with lombok as another PR
+@Accessors
 %0Apub
@@ -722,24 +722,52 @@
 tPort%22;%0A%0A   
+ @Getter(AccessLevel.PUBLIC)
  private fin
@@ -786,24 +786,52 @@
 otocol;%0A%0A   
+ @Getter(AccessLevel.PUBLIC)
  private fin
@@ -846,24 +846,52 @@
 g path;%0A%0A   
+ @Getter(AccessLevel.PUBLIC)
  private fin
@@ -906,24 +906,52 @@
 g host;%0A%0A   
+ @Getter(AccessLevel.PUBLIC)
  private fin
@@ -1484,74 +1484,8 @@
  %7D%0A%0A
-    public String getProtocol() %7B%0A        return protocol;%0A    %7D%0A%0A
     

use Lombok (#1789)