package jnet.client;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import jnet.lib.LogFile;

public class ExtAppManager {

    private String PUTTY_PATH = "app/putty.exe";
    private String WINBOX_PATH = "app/winbox.exe";

    private static List<Process> processes;

    public ExtAppManager() {
        processes = new ArrayList<>();

    }

    private void runApp(List<String> commands) {
        try {
            ProcessBuilder processBuilder = new ProcessBuilder(commands);
            Process process = processBuilder.start();
            processes.add(process);

            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                LogFile.printInfo("Terminating an external application...");
                process.destroy();
            }));

        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }

    public void closeApp() {
        for (Process process : processes) {
            if (process.isAlive()) {
                process.destroy();
                LogFile.printInfo("Terminating an external application ...");
            }
        }
    }

    public void runSshPutty(String address, String username, String password, int port) {
        List<String> params = new ArrayList<>();
        params.add(PUTTY_PATH);
        params.add("-ssh");
        params.add(username + "@" + address);
        if (!password.equals("")) {
            params.add("-pw");
            params.add(password);
        }
        params.add(String.valueOf(port));
        runApp(params);
        LogFile.printInfo("Run Putty SSH (" + address + ")");
    }

    public void runTelnetPutty(String address, String username, String password, int port) {
        List<String> params = new ArrayList<>();
        params.add(PUTTY_PATH);
        params.add("-telnet ");
        params.add(username + "@" + address);
        if (!password.equals("")) {
            params.add("-pw");
            params.add(password);
        }
        params.add(String.valueOf(port));
        runApp(params);
        LogFile.printInfo("Run Putty Telnet (" + address + ")");
    }

    public void runWinbox(String address, String username, String password, int port) {
        List<String> params = new ArrayList<>();
        params.add(WINBOX_PATH);
        params.add(address + ":" + port);
        params.add(username);
        params.add(password);
        runApp(params);
        LogFile.printInfo("Run Winbox (" + address + ")");
    }

    public void runWeb(int verze, String address, String port) {
        try {
            Desktop desktop = Desktop.getDesktop();
            URI uri;
            if (verze == 0) {
                uri = new URI("http://" + address + ":" + port);
            } else {
                uri = new URI("https://" + address + ":" + port);
            }
            desktop.browse(uri);
        } catch (URISyntaxException ex) {
            Logger.getLogger(ExtAppManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ExtAppManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
