/****************************************************************************/
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
// Copyright (C) 2016-2019 German Aerospace Center (DLR) and others.
// This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v2.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v20.html
// SPDX-License-Identifier: EPL-2.0
/****************************************************************************/
/// @file    Tools.java
/// @author  Maximiliano Bottazzi
/// @date    2016
///
//
/****************************************************************************/
package de.dlr.ts.lisum.gui;

import de.dlr.ts.commons.logger.DLRLogger;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;

/**
 *
 * @author @author <a href="mailto:maximiliano.bottazzi@dlr.de">Maximiliano
 * Bottazzi</a>
 */
public class Tools {


    /**
     *
     * @param lisaRESTfulServerPath
     * @param lisaPort
     * @return
     */
    public static boolean startLisaRESTfulServer(String lisaRESTfulServerPath, int lisaPort) {
        try {
            DLRLogger.info("Tools", "Starting Lisa+ RESTful Server");

            //String lsd = GlobalConfig.getInstance().getLisaRestFulServerDir();

            if (System.getProperty("os.name").toUpperCase().contains("WIN")) {
                ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", "start", "java", "-jar", lisaRESTfulServerPath + File.separator + "OmlFgServer.jar");
                Process p = pb.start();
                p.waitFor();
            } else {
                //String ss = GlobalConfig.getInstance().getDefaultLisaRestFulServerDir();
                File file = new File(lisaRESTfulServerPath);
                String text = "java -jar " + file.getAbsolutePath() + File.separator + "OmlFgServer.jar\n";

                Alert alert = new Alert(AlertType.INFORMATION);
                alert.setTitle("Start Lisa+ RESTful Server");
                alert.setHeaderText("To start a Lisa+ RESTful Server copy the text below, open a new terminal and paste it there.");
                alert.setContentText(text);

                ButtonType buttonCopy = new ButtonType("Copy");
                ButtonType buttonCopyAndOpen = new ButtonType("Copy & Open terminal");
                ButtonType buttonClose = new ButtonType("Close", ButtonData.OK_DONE);

                alert.getButtonTypes().setAll(buttonCopy, buttonCopyAndOpen, buttonClose);

                Optional<ButtonType> result = alert.showAndWait();
                if (result.get() == buttonCopy) {
                    StringSelection selection = new StringSelection(text);
                    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                    clipboard.setContents(selection, selection);
                } else if (result.get() == buttonCopyAndOpen) {
                    StringSelection selection = new StringSelection(text);
                    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                    clipboard.setContents(selection, selection);

                    ProcessBuilder pb = new ProcessBuilder("gnome-terminal");
                    Process p = pb.start();
                    p.waitFor();
                }
            }
        } catch (IOException | InterruptedException ex) {
            return false;
        }

        return true;
    }

    /**
     *
     * @param time
     */
    public static void sleepi(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException ex) {
            DLRLogger.severe("Config", ex);
        }
    }

}
