/*
 * Copyright 2013 the original author or authors.
 *
 * Licensed under the MIT License (the "License"); you may not use this
 * file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 *     http://opensource.org/licenses/MIT
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.blankstyle.vertx.php.net;

import org.vertx.java.core.AsyncResult;

import com.blankstyle.vertx.php.AsyncResultHandler;
import com.blankstyle.vertx.php.AsyncResultWrapper;
import com.blankstyle.vertx.php.Handler;
import com.blankstyle.vertx.php.TCPClient;
import com.blankstyle.vertx.php.util.PhpTypes;
import com.caucho.quercus.annotation.Optional;
import com.caucho.quercus.env.Env;
import com.caucho.quercus.env.LongValue;
import com.caucho.quercus.env.NumberValue;
import com.caucho.quercus.env.StringValue;
import com.caucho.quercus.env.Value;

/**
 * A PHP compatible implementation of the Vert.x NetClient.
 *
 * @author Jordan Halterman
 */
public class NetClient extends TCPClient<org.vertx.java.core.net.NetClient> {

  public NetClient(org.vertx.java.core.net.NetClient client) {
    super(client);
  }

  public NetClient(Env env, org.vertx.java.core.net.NetClient client) {
    super(client);
  }

  /**
   * Connects to a server.
   */
  public NetClient connect(Env env, NumberValue port, @Optional StringValue host, @Optional Value handler) {
    if (PhpTypes.notNull(handler)) {
      PhpTypes.assertCallable(env, handler, "Handler argument to Vertx\\Net\\NetClient::connect() must be callable.");
    }

    if (PhpTypes.notNull(host)) {
      client.connect(port.toInt(), host.toString(), new AsyncResultHandler<org.vertx.java.core.net.NetSocket>(env, PhpTypes.toCallable(handler), new AsyncResultWrapper<org.vertx.java.core.net.NetSocket, NetSocket>() {
        @Override
        public NetSocket wrap(org.vertx.java.core.net.NetSocket socket) {
          return new NetSocket(socket);
        }
      }));
    }
    else {
      client.connect(port.toInt(), new Handler<AsyncResult<org.vertx.java.core.net.NetSocket>>(env, PhpTypes.toCallable(handler), new AsyncResultWrapper<org.vertx.java.core.net.NetSocket, NetSocket>() {
        @Override
        public NetSocket wrap(org.vertx.java.core.net.NetSocket socket) {
          return new NetSocket(socket);
        }
      }));
    }
    return this;
  }

  /**
   * Returns the connection timeout.
   */
  public int getConnectTimeout(Env env) {
    return client.getConnectTimeout();
  }

  /**
   * Sets the connect timeout.
   */
  public NetClient setConnectTimeout(Env env, NumberValue timeout) {
    client.setConnectTimeout(timeout.toInt());
    return this;
  }

  /**
   * Returns the reconnect attempts.
   */
  public int getReconnectAttempts(Env env) {
    return client.getReconnectAttempts();
  }

  /**
   * Sets the reconnect attempts.
   */
  public NetClient setReconnectAttempts(Env env, NumberValue attempts) {
    client.setReconnectAttempts(attempts.toInt());
    return this;
  }

  /**
   * Returns the reconnect interval.
   */
  public long getReconnectInterval(Env env) {
    return client.getReconnectInterval();
  }

  /**
   * Sets the reconnect interval.
   */
  public NetClient setReconnectInterval(Env env, LongValue interval) {
    client.setReconnectInterval(interval.toLong());
    return this;
  }

  /**
   * Closes the client.
   */
  public void close() {
    client.close();
  }

  public String toString() {
    return "php:Vertx\\Net\\NetClient";
  }

}

@@ -2631,30 +2631,27 @@
 %0A  /**%0A   * 
-R
+S
 et
-urn
 s the connec
@@ -2651,19 +2651,16 @@
  connect
-ion
  timeout
@@ -2680,16 +2680,15 @@
 lic 
-int getC
+Value c
 onne
@@ -2708,165 +2708,76 @@
  env
-) %7B%0A    return client.getConnectTimeout();%0A  %7D%0A%0A  /**%0A   * Sets the connect timeout.%0A   */%0A  public NetClient setConnectTimeout(Env env, NumberValue 
+, @Optional NumberValue timeout) %7B%0A    if (PhpTypes.notNull(
 timeout)
  %7B%0A 
@@ -2768,27 +2768,30 @@
 ull(timeout)
+)
  %7B%0A
+  
     client.s
@@ -2833,131 +2833,80 @@
     
+  
 return 
+env.wrapJava(
 this
+)
 ;%0A  
-%7D%0A%0A  /**%0A   * Returns the reconnect attempts.%0A   */%0A  public int getReconnectAttempts(Env env) %7B%0A    return 
+  %7D%0A    else %7B%0A      return env.wrapJava(
 clie
@@ -2915,28 +2915,32 @@
 .get
-Rec
+C
 onnect
-Attempts();
+Timeout());%0A    %7D
 %0A  %7D
@@ -2992,38 +2992,31 @@
 */%0A  public 
-NetClient setR
+Value r
 econnectAtte
@@ -3020,32 +3020,42 @@
 ttempts(Env env,
+ @Optional
  NumberValue att
@@ -3059,24 +3059,64 @@
 attempts) %7B%0A
+    if (PhpTypes.notNull(attempts)) %7B%0A  
     client.s
@@ -3162,162 +3162,117 @@
     
+  
 return 
+env.wrapJava(
 this
+)
 ;%0A  
-%7D%0A%0A  /**%0A   * Returns the reconnect interval.%0A   */%0A  public long getReconnectInterval(Env env) %7B%0A    return client.getReconnectInterval();
+  %7D%0A    else %7B%0A      return env.wrapJava(client.getReconnectAttempts());%0A    %7D
 %0A  %7D
@@ -3332,22 +3332,15 @@
 lic 
-NetClient setR
+Value r
 econ
@@ -3360,16 +3360,26 @@
 Env env,
+ @Optional
  LongVal
@@ -3389,24 +3389,64 @@
 interval) %7B%0A
+    if (PhpTypes.notNull(interval)) %7B%0A  
     client.s
@@ -3485,36 +3485,133 @@
 ong());%0A    
+  
 return 
-this;
+env.wrapJava(this);%0A    %7D%0A    else %7B%0A      return env.wrapJava(client.getReconnectInterval());%0A    %7D
 %0A  %7D%0A%0A  /**%0A

Replace get* and set* methods on NetClient with single getter/setter method to support public attribute access.