package org.helioviewer.jhv.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.URL;
import java.util.concurrent.TimeUnit;

//import java.util.logging.Level;
//import java.util.logging.Logger;

import org.helioviewer.jhv.JHVGlobals;
import org.helioviewer.jhv.log.Log;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okio.BufferedSource;

public class NetClient implements AutoCloseable {

    //static {
    //    Logger.getLogger(OkHttpClient.class.getName()).setLevel(Level.FINE);
    //}

    private static final OkHttpClient client = new OkHttpClient.Builder()
        .connectTimeout(JHVGlobals.getStdConnectTimeout(), TimeUnit.MILLISECONDS)
        .readTimeout(JHVGlobals.getStdReadTimeout(), TimeUnit.MILLISECONDS)
        //.addInterceptor(new LoggingInterceptor())
        .build();

    private final Response response;

    public NetClient(String url) throws IOException {
        this(new URL(url), false);
    }

    public NetClient(URL url) throws IOException {
        this(url, false);
    }

    public NetClient(String url, boolean allowError) throws IOException {
        this(new URL(url), allowError);
    }

    public NetClient(URL url, boolean allowError) throws IOException {
        Request request = new Request.Builder().header("User-Agent", JHVGlobals.userAgent).url(url).build();
        response = client.newCall(request).execute();
        if (!allowError && !response.isSuccessful())
            throw new IOException(response.toString());
    }

    public boolean isSuccessful() {
        return response.isSuccessful();
    }

    public InputStream getStream() {
        return response.body().byteStream();
    }

    public Reader getReader() {
        return response.body().charStream();
    }

    public BufferedSource getSource() {
        return response.body().source();
    }

    public long getContentLength() {
        return response.body().contentLength();
    }

    @Override
    public void close() {
        if (response != null)
            response.close();
    }

    private static class LoggingInterceptor implements Interceptor {
        @Override
        public Response intercept(Chain chain) throws IOException {
            long t1 = System.nanoTime();
            Request r1 = chain.request();
            Log.info(String.format("Sending request %s on %s%n%s", r1.url(), chain.connection(), r1.headers()));

            Response r2 = chain.proceed(r1);
            long t2 = System.nanoTime();
            Log.info(String.format("Received response for %s in %.1fms%n%s", r1.url(), (t2 - t1) / 1e6d, r2.networkResponse().headers()));

            return r2;
        }
    }

}

@@ -26,16 +26,37 @@
 hv.io;%0A%0A
+import java.io.File;%0A
 import j
@@ -103,16 +103,50 @@
 Stream;%0A
+import java.io.InputStreamReader;%0A
 import j
@@ -181,16 +181,58 @@
 et.URL;%0A
+import java.nio.charset.StandardCharsets;%0A
 import j
@@ -371,16 +371,59 @@
 lobals;%0A
+import org.helioviewer.jhv.base.FileUtils;%0A
 import o
@@ -1079,16 +1079,54 @@
 esponse;
+%0A    private final InputStream stream;
 %0A%0A    pu
@@ -1483,32 +1483,220 @@
 s IOException %7B%0A
+        if (%22file%22.equals(url.getProtocol())) %7B%0A            stream = FileUtils.newBufferedInputStream(new File(url.getPath()));%0A            response = null;%0A            return;%0A        %7D%0A%0A
         Request 
@@ -1947,24 +1947,47 @@
 oString());%0A
+        stream = null;%0A
     %7D%0A%0A    p
@@ -2107,32 +2107,49 @@
 %7B%0A        return
+ stream == null ?
  response.body()
@@ -2153,32 +2153,41 @@
 y().byteStream()
+ : stream
 ;%0A    %7D%0A%0A    pub
@@ -2217,32 +2217,49 @@
 %7B%0A        return
+ stream == null ?
  response.body()
@@ -2271,16 +2271,72 @@
 Stream()
+ : new InputStreamReader(stream, StandardCharsets.UTF_8)
 ;%0A    %7D%0A
@@ -2550,25 +2550,100 @@
 oid close() 
-%7B
+throws IOException %7B%0A        if (stream != null)%0A            stream.close();
 %0A        if 

Support local files