package launcher.util;


import launcher.util.DesktopApi;
import launcher.util.LaunchLogger;
import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.xhtmlrenderer.simple.XHTMLPanel;
import org.xhtmlrenderer.swing.FSMouseListener;
import org.xhtmlrenderer.swing.LinkListener;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.net.URI;

public class BrowserComponent {
    private static class HyperlinkHandler extends LinkListener {
        @Override
        public void linkClicked(org.xhtmlrenderer.swing.BasicPanel panel, java.lang.String uri) {
            final String safeUri = uri;
            SwingWorker worker = new SwingWorker() {
                @Override
                protected Object doInBackground() throws Exception {
                    try {
                        LaunchLogger.info("Opening link in a browser: " + safeUri);
                        DesktopApi.browse(new URI(safeUri));
                    }
                    catch (Exception e) {
                        LaunchLogger.exception(e);
                    }
                    return null;
                }
            };
            worker.execute();
        }
    }

    public static void initialize() {

    }

    private XHTMLPanel _news;

    public BrowserComponent() {
        _news = new XHTMLPanel();
    }

    public Component getSwingComponent() {
        return _news;
    }

    public void init(final Component container, final JFrame frame) {
        for (Object ml : _news.getMouseTrackingListeners()) {
            _news.removeMouseTrackingListener((FSMouseListener) ml);
        }
        _news.addMouseTrackingListener(new HyperlinkHandler());
    }

    public void load(final String url) {
        try {
            //TODO Figure out a way to do this without writing to a temp file

            //The process here is (read HTML) -> (write HTML to file) -> (read file with flying-saucer) -> (f-s render)
            //There MUST be a simpler way to use flying-saucer, but I've spent ALL day trying out other web
            //components for swing. This is working (better than JEditorPane), so it is staying for now.
            Document doc = Jsoup.connect(url).get();
            String contents = doc.toString();
            File tmp = new File("news.html");
            FileUtils.write(tmp, contents);
            _news.setDocument(tmp);
            LaunchLogger.info("Finished loading latest news");

        }
        catch (Exception e) {
            LaunchLogger.error("The latest news could not be loaded.");
        }
    }
}

@@ -126,16 +126,45 @@
 eUtils;%0A
+import org.jsoup.Connection;%0A
 import o
@@ -179,16 +179,16 @@
 .Jsoup;%0A
-
 import o
@@ -350,16 +350,40 @@
 tener;%0A%0A
+import javax.net.ssl.*;%0A
 import j
@@ -456,16 +456,234 @@
 net.URI;
+%0Aimport java.security.KeyManagementException;%0Aimport java.security.NoSuchAlgorithmException;%0Aimport java.security.SecureRandom;%0Aimport java.security.cert.CertificateException;%0Aimport java.security.cert.X509Certificate;
 %0A%0Apublic
@@ -1994,32 +1994,1070 @@
 dler());%0A    %7D%0A%0A
+    // Taken from http://nanashi07.blogspot.tw/2014/06/enable-ssl-connection-for-jsoup.html%0A    public static void enableSSLSocket() throws KeyManagementException, NoSuchAlgorithmException %7B%0A        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() %7B%0A            public boolean verify(String hostname, SSLSession session) %7B%0A                return true;%0A            %7D%0A        %7D);%0A%0A        SSLContext context = SSLContext.getInstance(%22TLS%22);%0A        context.init(null, new X509TrustManager%5B%5D%7Bnew X509TrustManager() %7B%0A            public void checkClientTrusted(X509Certificate%5B%5D chain, String authType) throws CertificateException %7B%0A            %7D%0A%0A            public void checkServerTrusted(X509Certificate%5B%5D chain, String authType) throws CertificateException %7B%0A            %7D%0A%0A            public X509Certificate%5B%5D getAcceptedIssuers() %7B%0A                return new X509Certificate%5B0%5D;%0A            %7D%0A        %7D%7D, new SecureRandom());%0A        HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());%0A    %7D%0A%0A
     public void 
@@ -3087,32 +3087,63 @@
 %7B%0A        try %7B%0A
+            enableSSLSocket();%0A
             //TO
@@ -3554,20 +3554,22 @@
     
-Document doc
+Connection con
  = J
@@ -3585,16 +3585,88 @@
 ect(url)
+;%0A            con.ignoreHttpErrors(true);%0A            Document doc = con
 .get();%0A
@@ -3933,32 +3933,32 @@
 (Exception e) %7B%0A
-
             Laun
@@ -4009,24 +4009,72 @@
  loaded.%22);%0A
+            LaunchLogger.error(e.getMessage());%0A
         %7D%0A  

Allow SSL site loading for news