package ahlers.phantom.embedded.proxies;

import com.google.common.base.Optional;
import de.flapdoodle.embed.process.builder.AbstractBuilder;
import de.flapdoodle.embed.process.builder.IProperty;
import de.flapdoodle.embed.process.builder.TypedProperty;

/**
 * @author [[mailto:michael@ahlers.consulting Michael Ahlers]]
 */
public class ProxyBuilder
        extends AbstractBuilder<IProxy> {

    protected static final TypedProperty<String> HOST = TypedProperty.with("host", String.class);
    protected static final TypedProperty<Integer> PORT = TypedProperty.with("port", Integer.class);

    protected static final TypedProperty<IProxyType> TYPE = TypedProperty.with("type", IProxyType.class);
    protected static final TypedProperty<IProxyCredential> CREDENTIAL = TypedProperty.with("credential", IProxyCredential.class);

    public ProxyBuilder defaults() {
        setDefault(TYPE, ProxyType.NONE);
        return this;
    }

    protected IProperty<String> host() {
        return property(HOST);
    }

    public ProxyBuilder host(final String value) {
        host().set(value);
        return this;
    }

    protected IProperty<Integer> port() {
        return property(PORT);
    }

    public ProxyBuilder port(final Integer value) {
        port().set(value);
        return this;
    }

    protected IProperty<IProxyType> type() {
        return property(TYPE);
    }

    public ProxyBuilder type(final IProxyType value) {
        type().set(value);
        return this;
    }

    protected IProperty<IProxyCredential> credential() {
        return property(CREDENTIAL);
    }

    public ProxyBuilder credential(final IProxyCredential value) {
        credential().set(value);
        return this;
    }

    @Override
    public IProxy build() {
        final String host = get(HOST);
        final Optional<Integer> port = Optional.fromNullable(get(PORT, null));
        final IProxyType type = get(TYPE);
        final Optional<IProxyCredential> credential = Optional.fromNullable(get(CREDENTIAL, null));

        return new ImmutableProxy(
                host,
                port,
                type,
                credential
        );
    }

    static class ImmutableProxy
            implements IProxy {

        private final String host;

        private final Optional<Integer> port;

        private final IProxyType type;

        private final Optional<IProxyCredential> credential;

        public ImmutableProxy(
                final String host,
                final Optional<Integer> port,
                final IProxyType type,
                final Optional<IProxyCredential> credential
        ) {
            this.host = host;
            this.port = port;
            this.type = type;
            this.credential = credential;
        }

        @Override
        public String host() {
            return host;
        }

        @Override
        public Optional<Integer> port() {
            return port;
        }

        @Override
        public IProxyType type() {
            return type;
        }

        @Override
        public Optional<IProxyCredential> credential() {
            return credential;
        }

    }

}

@@ -246,16 +246,128 @@
 roperty;
+%0Aimport org.apache.commons.lang3.builder.ToStringBuilder;%0Aimport org.apache.commons.lang3.builder.ToStringStyle;
 %0A%0A/**%0A *
@@ -977,50 +977,8 @@
 ) %7B%0A
-        setDefault(TYPE, ProxyType.NONE);%0A
     
@@ -1961,32 +1961,41 @@
 ;%0A        final 
+Optional%3C
 IProxyType type 
@@ -1992,16 +1992,17 @@
 Type
+%3E
  type = 
 get(
@@ -2001,16 +2001,38 @@
 e = 
+Optional.fromNullable(
 get(TYPE
 );%0A 
@@ -2023,24 +2023,31 @@
 ble(get(TYPE
+, null)
 );%0A        f
@@ -2450,24 +2450,33 @@
 ivate final 
+Optional%3C
 IProxyType t
@@ -2469,24 +2469,25 @@
 l%3CIProxyType
+%3E
  type;%0A%0A    
@@ -2674,24 +2674,33 @@
       final 
+Optional%3C
 IProxyType t
@@ -2693,24 +2693,25 @@
 l%3CIProxyType
+%3E
  type,%0A     
@@ -3130,24 +3130,33 @@
      public 
+Optional%3C
 IProxyType t
@@ -3153,16 +3153,17 @@
 roxyType
+%3E
  type() 
@@ -3267,32 +3267,32 @@
  credential() %7B%0A
-
             retu
@@ -3313,21 +3313,180 @@
 %0A        %7D%0A%0A
+        @Override%0A        public String toString() %7B%0A            return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);%0A        %7D%0A%0A
     %7D%0A%0A%7D%0A

Improve debug output