package de.reinhardt_karlheinz.pcc.pc;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.util.Scanner;

import de.reinhardt_karlheinz.pcc.exceptions.AdbDeviceNotFoundException;
import de.reinhardt_karlheinz.pcc.interfaces.PCCConnection;
import de.reinhardt_karlheinz.pcc.interfaces.PluginLoader;
import de.reinhardt_karlheinz.pcc.pc.gui.PCC_PCGui;
import de.reinhardt_karlheinz.pcc.pc.plugins.PC_PluginManager;
import de.reinhardt_karlheinz.pcc.pc.plugins.example.ConnectionTrafficMonitor;
import de.reinhardt_karlheinz.pcc.pc.plugins.example.PCCChat;
import de.reinhardt_karlheinz.pcc.pc.plugins.example.PCCMinecraftRemote;
import de.reinhardt_karlheinz.pcc.pc.servers.USBClient;

public class Main {
  public static void main(String[] args) {
    String pathToAdb = "D:\\Android\\android-sdk\\platform-tools\\adb.exe";
	pathToAdb = "/home/i/psi12049/android-sdks/platform-tools/adb";
    int port = 38001;
    boolean androidDeviceConnected = false;
    try {
      androidDeviceConnected = execAdb(pathToAdb, port);
    } catch (AdbDeviceNotFoundException e1) {
      System.out.println("NO ANDROID DEVICE CONNECTED!");
      e1.printStackTrace();
    }
    if (androidDeviceConnected) {
      // if (true) {
      PCCConnection rmpcServer = new USBClient(port);
      PluginLoader pluginCtrl = new PC_PluginManager(rmpcServer);

      try {
        pluginCtrl.loadPlugins();
      } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      pluginCtrl.addPlugin(new ConnectionTrafficMonitor());
      pluginCtrl.addPlugin(new PCCChat());
      pluginCtrl.addPlugin(new PCCMinecraftRemote());

      pluginCtrl.printLoadedPluginInfos();
      pluginCtrl.startAllPlugins();

      PCC_PCGui gui = new PCC_PCGui(pluginCtrl, rmpcServer);
      // try {
      // if (rmpcServer.startServer() == true) {
      // System.out.println("connection closed");
      // } else {
      // System.out.println("connection not established");// TODO msg
      // // System.exit(0);
      // }
      // } catch (ConnectException e) {
      // // TODO Auto-generated catch block
      // e.printStackTrace();
      // } catch (UnknownHostException e) {
      // // TODO Auto-generated catch block
      // e.printStackTrace();
      // } catch (IOException e) {
      // // TODO Auto-generated catch block
      // e.printStackTrace();
      // }
    } else {
      System.exit(1);
    }
    // Runtime.getRuntime().addShutdownHook(new Thread() {
    // public void run() {
    // System.out.println("Die VM wird gerade beendet!");
    // }
    // });
    // System.exit(0);
  }

  /**
   * Runs the android debug bridge command of forwarding the ports
   * 
   * @throws AdbDeviceNotFoundException
   * 
   */
  private static boolean execAdb(String adbPath, int port)
      throws AdbDeviceNotFoundException {
    boolean ret = true;
    // run the adb bridge
    try {
      Process p = Runtime.getRuntime().exec(
          adbPath + " forward tcp:" + port + " tcp:" + port);
      // Process
      // p=Runtime.getRuntime().exec("C:\\android-sdk-windows\\tools\\adb.exe forward tcp:38300 tcp:38300");
      Scanner sc = new Scanner(p.getErrorStream());
      if (sc.hasNext()) {
        ret = false;
        while (sc.hasNext())
          System.err.println(sc.next());
        System.err.println("Cannot start the Android debug bridge");
        System.out.flush();
        System.err.flush();
        throw new AdbDeviceNotFoundException("device not found");
      }
    } catch (Exception e) {
      System.out.println(e.toString());
      ret = false;
    }
    return ret;
  }
}
