/*
 * Copyright (C) 2015 Pedro Vicente Gómez Sánchez.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.github.pedrovgs.androidwifiadb.adb;

import com.github.pedrovgs.androidwifiadb.Device;
import com.intellij.util.EnvironmentUtil;
import java.io.File;
import java.util.List;

public class ADB {

  private static final String ANDROID_ENV_VAR_NAME = "ANDROID_HOME";
  private static final String ADB_RELATIVE_PATH_UNIX =
      File.separator + "platform-tools" + File.separator + "adb";
  private static final String ADB_RELATIVE_PATH_WINDOWS =
      File.separator + "platform-tools" + File.separator + "adb.exe";

  private final CommandLine commandLine;
  private final ADBParser adbParser;

  public ADB(CommandLine commandLine, ADBParser adbParser) {
    this.commandLine = commandLine;
    this.adbParser = adbParser;
  }

  public boolean isInstalled() {
    String versionCommand = getCommand("version");
    return !commandLine.executeCommand(versionCommand).isEmpty();
  }

  public List<Device> getDevicesConnectedByUSB() {
    String getDevicesCommand = getCommand("devices -l");
    String adbDevicesOutput = commandLine.executeCommand(getDevicesCommand);
    return adbParser.parseGetDevicesOutput(adbDevicesOutput);
  }

  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);
    if (deviceIp.isEmpty()) {
      return false;
    } else {
      return connectDevice(deviceIp);
    }
  }

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

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

  private String getAdbPath() {
    String androidSdkPath = EnvironmentUtil.getValue(ANDROID_ENV_VAR_NAME);
    if (androidSdkPath == null || androidSdkPath.isEmpty()) {
      return "";
    }
    String adbPath = isWindowsTheOS() ? ADB_RELATIVE_PATH_WINDOWS : ADB_RELATIVE_PATH_UNIX;
    String separator =
        androidSdkPath.substring(androidSdkPath.length() - 1).equals(File.separator) ? ""
            : File.separator;
    return androidSdkPath + separator + adbPath;
  }

  private String getCommand(String command) {
    return getAdbPath() + " " + command;
  }

  private boolean isWindowsTheOS() {
    String os = System.getProperty("os.name");
    return os.toLowerCase().contains("windows");
  }
}
@@ -928,21 +928,16 @@
 IVE_PATH
-_UNIX
  =%0A     
@@ -1000,136 +1000,8 @@
 db%22;
-%0A  private static final String ADB_RELATIVE_PATH_WINDOWS =%0A      File.separator + %22platform-tools%22 + File.separator + %22adb.exe%22;
 %0A%0A  
@@ -2911,77 +2911,25 @@
 h = 
-isWindowsTheOS() ? ADB_RELATIVE_PATH_WINDOWS : ADB_RELATIVE_PATH_UNIX
+ADB_RELATIVE_PATH
 ;%0A  
@@ -3217,144 +3217,6 @@
 %0A  %7D
-%0A%0A  private boolean isWindowsTheOS() %7B%0A    String os = System.getProperty(%22os.name%22);%0A    return os.toLowerCase().contains(%22windows%22);%0A  %7D
 %0A%7D

Remove specific path for windows