package land.chipmunk.chipmunkmod.util;

import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

public class DownloadUtilities {

    public static byte[] DownloadToByteArray (final URL url) throws IOException, KeyManagementException, NoSuchAlgorithmException {
        final SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
        SSLContext.setDefault(ctx);
        final URLConnection conn = url.openConnection();
        conn.setConnectTimeout(5000);
        conn.setReadTimeout(10000);
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0");

        try (final BufferedInputStream downloadStream = new BufferedInputStream(conn.getInputStream())) {
            final ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
            final byte[] buf = new byte[1024];
            int n;
            while ((n = downloadStream.read(buf)) > 0) {
                byteArrayStream.write(buf, 0, n);

                if (Thread.interrupted()) {
                    return null;
                }
            }
            return byteArrayStream.toByteArray();
        }
        // Closing a ByteArrayInputStream has no effect, so I do not close it.
    }

    public static InputStream DownloadToInputStream (final URL url) throws KeyManagementException, NoSuchAlgorithmException, IOException {
        return new ByteArrayInputStream(DownloadToByteArray(url));
    }

    private static class DefaultTrustManager implements X509TrustManager {

        @Override
        public void checkClientTrusted (final X509Certificate[] arg0, final String arg1) {
        }

        @Override
        public void checkServerTrusted (final X509Certificate[] arg0, final String arg1) {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers () {
            return null;
        }
    }
}
