package com.github.pedrovgs.androidwifiadb.adb;

import com.android.ddmlib.AndroidDebugBridge;
import com.android.ddmlib.IDevice;
import com.github.pedrovgs.androidwifiadb.Device;
import com.intellij.openapi.projectRoots.ProjectJdkTable;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.SdkTypeId;
import java.util.LinkedList;
import java.util.List;

public class ADB {

  private static final String ADB_PATH = "$ANDROID_HOME/platform-tools/adb";

  private final CommandLine commandLine;
  private final ADBParser adbParser;
  private final AndroidDebugBridge androidDebugBridge;

  public ADB(CommandLine commandLine, ADBParser adbParser) {
    this.commandLine = commandLine;
    this.adbParser = adbParser;
    AndroidDebugBridge.init(true);
    AndroidDebugBridge.createBridge();
    this.androidDebugBridge =  AndroidDebugBridge.getBridge();

  }

  public boolean isInstalled() {
    return androidDebugBridge.isConnected();
  }

  public List<Device> getDevicesConnectedByUSB() {
    IDevice[] adbDevices = androidDebugBridge.getDevices();
    List<Device> devices = new LinkedList<Device>();
    for (IDevice adbDevice : adbDevices) {
      String name = adbDevice.getName();
      String id = adbDevice.getAvdName();
      Device device = new Device(name, id);
      devices.add(device);
    }
    return devices;
  }

  public List<Device> connectDevices(List<Device> devices) {
    for (Device device : devices) {
      boolean connected = connectDeviceByIp(device);
      device.setConnected(connected);
    }
    return devices;
  }

  private boolean connectDeviceByIp(Device device) {
    String deviceIp = getDeviceIp(device);
    return connectDevice(deviceIp);
  }

  private String getDeviceIp(Device device) {
    String ipInfoOutput = commandLine.executeCommand(
        ADB_PATH + " -s " + device.getId() + " shell ip -f inet addr show wlan0");
    return adbParser.parseGetDeviceIp(ipInfoOutput);
  }

  private boolean connectDevice(String deviceIp) {
    commandLine.executeCommand(ADB_PATH + "tcpip 5555");
    String connectOutput = commandLine.executeCommand(ADB_PATH + "connect " + deviceIp);
    return connectOutput.contains("connected");
  }
}

@@ -57,311 +57,45 @@
 com.
-android.ddmlib.AndroidDebugBridge;%0Aimport com.android.ddmlib.IDevice;%0Aimport com.github.pedrovgs.androidwifiadb.Device;%0Aimport com.intellij.openapi.projectRoots.ProjectJdkTable;%0Aimport com.intellij.openapi.projectRoots.Sdk;%0Aimport com.intellij.openapi.projectRoots.SdkTypeId;%0Aimport java.util.LinkedList
+github.pedrovgs.androidwifiadb.Device
 ;%0Aim
@@ -171,20 +171,31 @@
 ng A
-DB_PATH
+NDROID_ENV_VAR_NAME
  = %22
-$
 ANDR
@@ -202,16 +202,70 @@
 OID_HOME
+%22;%0A  private static final String ADB_RELATIVE_PATH = %22
 /platfor
@@ -360,63 +360,8 @@
 ser;
-%0A  private final AndroidDebugBridge androidDebugBridge;
 %0A%0A  
@@ -493,441 +493,271 @@
 ;%0A  
-  AndroidDebugBridge.init(true);%0A    AndroidDebugBridge.createBridge();%0A    this.androidDebugBridge =  AndroidDebugBridge.getBridge();%0A%0A  %7D%0A%0A  public boolean isInstalled() %7B%0A    return androidDebugBridge.isConnected();%0A  %7D%0A%0A  public List%3CDevice%3E getDevicesConnectedByUSB() %7B%0A    IDevice%5B%5D adbDevices = androidDebugBridge.getDevices();%0A    List%3CDevice%3E devices = new LinkedList%3CDevice%3E();%0A    for (IDevice adbDevice : adbDevices) %7B%0A  
+%7D%0A%0A  public boolean isInstalled() %7B%0A    String versionCommand = getCommand(%22version%22);%0A    return !commandLine.executeCommand(versionCommand).isEmpty();%0A  %7D%0A%0A  public List%3CDevice%3E getDevicesConnectedByUSB() %7B%0A    String getDevicesCommand = getCommand(%22devices%22);%0A
     
@@ -766,15 +766,8 @@
 ring
- name =
  adb
@@ -776,157 +776,125 @@
 vice
-.getName();%0A      String id = adbDevice.getAvdName();%0A      Device device = new Device(name, id);%0A      devices.add(device);%0A    %7D%0A    return devices
+sOutput = commandLine.executeCommand(getDevicesCommand);%0A    return adbParser.parseGetDevicesOutput(adbDevicesOutput)
 ;%0A  
@@ -1313,50 +1313,28 @@
 ing 
-ipInfoOutput = commandLine.execute
+getDeviceIp
 Command
-(
+ =
 %0A   
@@ -1342,21 +1342,20 @@
     
-ADB_PATH + %22 
+getCommand(%22
 -s %22
@@ -1408,24 +1408,98 @@
 ow wlan0%22);%0A
+    String ipInfoOutput = commandLine.executeCommand(getDeviceIpCommand);%0A
     return a
@@ -1603,58 +1603,176 @@
     
-commandLine.executeCommand(ADB_PATH + %22tcpip 5555%22
+String enableTCPCommand = getCommand(%22tcpip 5555%22);%0A    commandLine.executeCommand(enableTCPCommand);%0A    String connectDeviceCommand = getCommand(%22connect %22 + deviceIp
 );%0A 
@@ -1828,40 +1828,28 @@
 and(
-ADB_PATH + %22connect %22 + deviceIp
+connectDeviceCommand
 );%0A 
@@ -1898,11 +1898,315 @@
 d%22);%0A  %7D
+%0A%0A  private String getAdbPath() %7B%0A    String androidSdkPath = System.getenv().get(ANDROID_ENV_VAR_NAME);%0A    if (androidSdkPath == null) %7B%0A      return %22%22;%0A    %7D%0A    return androidSdkPath + ADB_RELATIVE_PATH;%0A  %7D%0A%0A  private String getCommand(String command) %7B%0A    return getAdbPath() + %22 %22 + command;%0A  %7D
 %0A%7D%0A

Replace AndroidDebugBridge usage with the full adb path configured in the /Users/pedro/Development/AndroidSDK/sdk environment var