package jguard.client;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class AppLauncher {

    public static void winbox(String ip, String port, String user, String password) {
        if (port.equals("8291")) {
            runApp(new String[]{"app/winbox.exe", ip, user, password});
        } else {
            runApp(new String[]{"app/winbox.exe", ip + ":" + port, user, password});
        }

    }

    public static void openWebpage(String address, String port, int verze) {
        String urlString = "http://"+address+":"+port;
        if (verze == 1) {
            urlString = "https://"+address+":"+port;
        }
        try {
            Desktop.getDesktop().browse(new URL(urlString).toURI());
        } catch (URISyntaxException | IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void ssh(String address, String username, String password, int port){
        runPutty("-ssh", address, username, password, port);
    }

    private static void runPutty(String versionParam, String address, String username, String password, Integer port) {
        List<String> params = new ArrayList();
        params.add("app/putty.exe");
        params.add(versionParam);
        if (port != null) {
            params.add("-P");
            params.add(port.toString());
        }
        if (username.equals("")) {
            params.add(address);
        } else {
            params.add(username + "@" + address);
            if (!password.equals("")) {
                params.add("-pw");
                params.add(password);
            }
        }
        String[] aParams = new String[params.size()];
        runApp((String[]) params.toArray(aParams));
    }

    private static void runApp(String[] run) {
        Thread t = new Thread(new Runnable() {
            public void run() {
                try {
                    Process p = Runtime.getRuntime().exec(run);
                } catch (Exception ex) {
                }
            }
        });
        t.start();
    }
}
