// Path: sthlmtraveling/src/main/java/com/markupartist/sthlmtraveling/data/misc/HttpHelper.java
// public class HttpHelper {
//     static final int DISK_CACHE_SIZE = 50 * 1024 * 1024; // 50MB
//     private static HttpHelper sInstance;
//     OkHttpClient mClient;
// 
//     private HttpHelper(final Context context) {
//         mClient = new OkHttpClient();
//         mClient.setConnectTimeout(15, TimeUnit.SECONDS);
//         mClient.setReadTimeout(15, TimeUnit.SECONDS);
//         installHttpCache(context);
//     }
// 
//     public static HttpHelper getInstance(final Context context) {
//         if (sInstance == null) {
//             sInstance = new HttpHelper(context);
//         }
//         return sInstance;
//     }
// 
//     /**
//      * Install an HTTP cache in the application cache directory.
//      */
//     private void installHttpCache(final Context context) {
//         // Install an HTTP cache in the application cache directory.
//         try {
//             // Install an HTTP cache in the application cache directory.
//             File cacheDir = new File(context.getCacheDir(), "http");
//             Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
//             mClient.setCache(cache);
//         } catch (Throwable e) {
//             Log.e("HttpHelper", "Unable to install disk cache.");
//         }
//     }
// 
//     public Request createRequest(final String endpoint) throws IOException {
//         return new Request.Builder()
//                 .get()
//                 .url(endpoint)
//                 .addHeader("X-STHLMTraveling-API-Key", ApiConf.get(ApiConf.KEY))
//                 .header("User-Agent", AppConfig.USER_AGENT)
//                 .build();
//     }
// 
//     public OkHttpClient getClient() {
//         return mClient;
//     }
// 
// }
// 
// Path: sthlmtraveling/src/main/java/com/markupartist/sthlmtraveling/utils/LocationUtils.java
// public class LocationUtils {
// 
//     private static final String TAG = "LocationUtils";
//     /**
//      * Simple formatting of an {@link Address}.
//      * @param address the address
//      * @return formatted address.
//      */
//     public static String getAddressLine(Address address) {
//         StringBuilder sb = new StringBuilder();
//         if (address.getAddressLine(0) != null) {
//             sb.append(address.getAddressLine(0));
//         }
//         if (address.getAddressLine(1) != null) {
//             sb.append(" ").append(address.getAddressLine(1));
//         }
//         return sb.toString();
//     }
// 
//     /**
//      * Converts a string representation to {@link android.location.Location}.
//      * @param locationData the location (latitude,longitude)
//      * @return android location.
//      */
//     public static Location parseLocation(String locationData) {
//         String[] longAndLat = locationData.split(",");
//         //Defualt to the world center, The Royal Palace
//         float latitude = 59.3270053f;
//         float longitude = 18.0723166f;
//         try {
//             latitude = Float.parseFloat(longAndLat[0]);
//             longitude = Float.parseFloat(longAndLat[1]);
//         } catch (NumberFormatException nfe) {
//             Log.e(TAG, nfe.toString());
//         } catch (ArrayIndexOutOfBoundsException aioobe) {
//             Log.e(TAG, aioobe.toString());
//         }
// 
//         Location location = new Location("sthlmtraveling");
//         location.setLatitude(latitude);
//         location.setLongitude(longitude);
// 
//         return location;
//     }
// 
// }
// 
// Path: sthlmtraveling/src/main/java/com/markupartist/sthlmtraveling/provider/ApiConf.java
// public static String apiEndpoint2() {
//     return AppConfig.STHLM_TRAVELING_API_ENDPOINT;
// }


import android.content.Context;
import android.location.Location;
import androidx.core.util.Pair;
import android.text.TextUtils;
import android.util.Log;
import com.markupartist.sthlmtraveling.data.misc.HttpHelper;
import com.markupartist.sthlmtraveling.utils.LocationUtils;
import com.squareup.okhttp.Response;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.markupartist.sthlmtraveling.provider.ApiConf.apiEndpoint2;
    }

    public ArrayList<Site> getSite(final Context context, final String name) throws IOException {
        return getSiteV2(context, name);
    }

    public ArrayList<Site> getSiteV2(final Context context, final String name) throws IOException {
        return getSiteV2(context, name, true);
    }

    public ArrayList<Site> getSiteV2(final Context context, final String name, final boolean onlyStations) throws IOException {
        if (TextUtils.isEmpty(name)) {
            return new ArrayList<>();
        }

        HttpHelper httpHelper = HttpHelper.getInstance(context);
        String onlyStationsParam = onlyStations ? "true" : "false";
        String url = apiEndpoint2() + "v1/site/"
                + "?q=" + URLEncoder.encode(name, "UTF-8")
                + "&onlyStations=" + onlyStationsParam
                + "&addLocation=true";
        Response response = httpHelper.getClient().newCall(
                httpHelper.createRequest(url)).execute();

        if (!response.isSuccessful()) {
            throw new IOException("Server error while fetching sites");
        }

        ArrayList<Site> sites = new ArrayList<Site>();
        try {
// Path: sthlmtraveling/src/main/java/com/markupartist/sthlmtraveling/data/misc/HttpHelper.java
// public class HttpHelper {
//     static final int DISK_CACHE_SIZE = 50 * 1024 * 1024; // 50MB
//     private static HttpHelper sInstance;
//     OkHttpClient mClient;
// 
//     private HttpHelper(final Context context) {
//         mClient = new OkHttpClient();
//         mClient.setConnectTimeout(15, TimeUnit.SECONDS);
//         mClient.setReadTimeout(15, TimeUnit.SECONDS);
//         installHttpCache(context);
//     }
// 
//     public static HttpHelper getInstance(final Context context) {
//         if (sInstance == null) {
//             sInstance = new HttpHelper(context);
//         }
//         return sInstance;
//     }
// 
//     /**
//      * Install an HTTP cache in the application cache directory.
//      */
//     private void installHttpCache(final Context context) {
//         // Install an HTTP cache in the application cache directory.
//         try {
//             // Install an HTTP cache in the application cache directory.
//             File cacheDir = new File(context.getCacheDir(), "http");
//             Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
//             mClient.setCache(cache);
//         } catch (Throwable e) {
//             Log.e("HttpHelper", "Unable to install disk cache.");
//         }
//     }
// 
//     public Request createRequest(final String endpoint) throws IOException {
//         return new Request.Builder()
//                 .get()
//                 .url(endpoint)
//                 .addHeader("X-STHLMTraveling-API-Key", ApiConf.get(ApiConf.KEY))
//                 .header("User-Agent", AppConfig.USER_AGENT)
//                 .build();
//     }
// 
//     public OkHttpClient getClient() {
//         return mClient;
//     }
// 
// }
// 
// Path: sthlmtraveling/src/main/java/com/markupartist/sthlmtraveling/utils/LocationUtils.java
// public class LocationUtils {
// 
//     private static final String TAG = "LocationUtils";
//     /**
//      * Simple formatting of an {@link Address}.
//      * @param address the address
//      * @return formatted address.
//      */
//     public static String getAddressLine(Address address) {
//         StringBuilder sb = new StringBuilder();
//         if (address.getAddressLine(0) != null) {
//             sb.append(address.getAddressLine(0));
//         }
//         if (address.getAddressLine(1) != null) {
//             sb.append(" ").append(address.getAddressLine(1));
//         }
//         return sb.toString();
//     }
// 
//     /**
//      * Converts a string representation to {@link android.location.Location}.
//      * @param locationData the location (latitude,longitude)
//      * @return android location.
//      */
//     public static Location parseLocation(String locationData) {
//         String[] longAndLat = locationData.split(",");
//         //Defualt to the world center, The Royal Palace
//         float latitude = 59.3270053f;
//         float longitude = 18.0723166f;
//         try {
//             latitude = Float.parseFloat(longAndLat[0]);
//             longitude = Float.parseFloat(longAndLat[1]);
//         } catch (NumberFormatException nfe) {
//             Log.e(TAG, nfe.toString());
//         } catch (ArrayIndexOutOfBoundsException aioobe) {
//             Log.e(TAG, aioobe.toString());
//         }
// 
//         Location location = new Location("sthlmtraveling");
//         location.setLatitude(latitude);
//         location.setLongitude(longitude);
// 
//         return location;
//     }
// 
// }
// 
// Path: sthlmtraveling/src/main/java/com/markupartist/sthlmtraveling/provider/ApiConf.java
// public static String apiEndpoint2() {
//     return AppConfig.STHLM_TRAVELING_API_ENDPOINT;
// }


// Path: sthlmtraveling/src/main/java/com/markupartist/sthlmtraveling/provider/site/SitesStore.java
import android.content.Context;
import android.location.Location;
import androidx.core.util.Pair;
import android.text.TextUtils;
import android.util.Log;
import com.markupartist.sthlmtraveling.data.misc.HttpHelper;
import com.markupartist.sthlmtraveling.utils.LocationUtils;
import com.squareup.okhttp.Response;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.markupartist.sthlmtraveling.provider.ApiConf.apiEndpoint2;

    }

    public ArrayList<Site> getSite(final Context context, final String name) throws IOException {
        return getSiteV2(context, name);
    }

    public ArrayList<Site> getSiteV2(final Context context, final String name) throws IOException {
        return getSiteV2(context, name, true);
    }

    public ArrayList<Site> getSiteV2(final Context context, final String name, final boolean onlyStations) throws IOException {
        if (TextUtils.isEmpty(name)) {
            return new ArrayList<>();
        }

        HttpHelper httpHelper = HttpHelper.getInstance(context);
        String onlyStationsParam = onlyStations ? "true" : "false";
        String url = apiEndpoint2() + "v1/site/"
                + "?q=" + URLEncoder.encode(name, "UTF-8")
                + "&onlyStations=" + onlyStationsParam
                + "&addLocation=true";
        Response response = httpHelper.getClient().newCall(
                httpHelper.createRequest(url)).execute();

        if (!response.isSuccessful()) {
            throw new IOException("Server error while fetching sites");
        }

        ArrayList<Site> sites = new ArrayList<Site>();
        try {
            JSONObject jsonResponse = new JSONObject(response.body().string());