package be.iminds.ilabt.jfed.experimenter_gui.login;

import be.iminds.ilabt.jfed.connectivity_tester.ConnectivityTest;
import be.iminds.ilabt.jfed.connectivity_tester.SafeConnectivityTest;
import be.iminds.ilabt.jfed.connectivity_tester.URLConnectionTest;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Optional;
import java.util.Properties;

/**
 * User: twalcari
 * Date: 8/19/2016
 * Time: 16:07
 */
public class SystemProxyTestThread extends Thread {
    private static final Logger LOG = LoggerFactory.getLogger(SystemProxyTestThread.class);
    private static final URL HTTP_TEST_URL;
    private static final URL HTTPS_TEST_URL;

    static {
        URL httpUrl = null, httpsUrl = null;
        try {
            httpUrl = new URL("http://jfed.iminds.be");
            httpsUrl = new URL("https://flsmonitor.fed4fire.eu");
        } catch (MalformedURLException ignored) {
        }
        HTTP_TEST_URL = httpUrl;
        HTTPS_TEST_URL = httpsUrl;

    }


    public SystemProxyTestThread() {
        setDaemon(true);
        setName("SystemProxyTest");
    }

    @Override
    public void run() {
        //check the System proxy settings

        Properties systemProperties = System.getProperties();
        if (systemProperties.getProperty("http.proxyHost") == null
                && systemProperties.getProperty("http.proxyPort") == null
                && systemProperties.getProperty("https.proxyHost") == null
                && systemProperties.getProperty("https.proxyPort") == null
                ) {
            LOG.debug("No system proxy settings to test.");
            return;
        }

        LOG.debug("Found the following system proxy settings: HTTP: {}:{}, HTTPS: {}:{}",
                systemProperties.getProperty("http.proxyHost"),
                systemProperties.getProperty("http.proxyPort"),
                systemProperties.getProperty("https.proxyHost"),
                systemProperties.getProperty("https.proxyPort"));
        {
            SafeConnectivityTest<URLConnectionTest> safeHttpTest =
                    new SafeConnectivityTest<>(new URLConnectionTest(HTTP_TEST_URL, "Http URL Test", ""), 10 * 1000);

            final Optional<ConnectivityTest.ConnectivityTestResult> httpTestResult = safeHttpTest.call();
            LOG.info("HttpURLTest result: {}", httpTestResult);
            if (!httpTestResult.isPresent() || httpTestResult.get().getStatus() != ConnectivityTest.Status.SUCCEEDED) {

                Platform.runLater(() -> {
                    Alert httpProxyAlert = new Alert(Alert.AlertType.ERROR);
                    httpProxyAlert.setContentText("The HTTP proxy settings on your system are invalid. Please correct them to prevent connectivity issues.");
                    httpProxyAlert.setTitle("Invalid system proxy settings");
                    httpProxyAlert.setHeaderText("Invalid system proxy settings");
                    httpProxyAlert.show();
                });

            }
        }
        {
            SafeConnectivityTest<URLConnectionTest> safeHttpsTest =
                    new SafeConnectivityTest<>(new URLConnectionTest(HTTPS_TEST_URL, "Https URL Test", ""), 10 * 1000);

            final Optional<ConnectivityTest.ConnectivityTestResult> httpsTestResult = safeHttpsTest.call();
            LOG.info("HttpsURLTest result: {}", httpsTestResult);
            if (!httpsTestResult.isPresent() || httpsTestResult.get().getStatus() != ConnectivityTest.Status.SUCCEEDED) {
                Platform.runLater(() -> {
                    Alert httpsProxyAlert = new Alert(Alert.AlertType.ERROR);
                    httpsProxyAlert.setContentText("The HTTPS proxy settings on your system are invalid. Please correct them to prevent connectivity issues.");
                    httpsProxyAlert.setTitle("Invalid system proxy settings");
                    httpsProxyAlert.setHeaderText("Invalid system proxy settings");
                    httpsProxyAlert.show();
                });
            }
        }
    }
}
