package com.battlelancer.seriesguide.backend;

import android.content.Context;
import com.battlelancer.seriesguide.BuildConfig;
import com.battlelancer.seriesguide.util.Utils;
import com.google.api.client.googleapis.services.AbstractGoogleClient;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;
import java.io.IOException;

/**
 * Common utilities for working with Cloud Endpoints.
 *
 * If you'd like to test using a locally-running version of your App Engine backend (i.e. running on
 * the Development App Server), you need to set LOCAL_ANDROID_RUN to 'true'.
 *
 * See https://cloud.google.com/appengine/docs/java/endpoints
 */
public class CloudEndpointUtils {

    /*
     * Change this to 'true' if you're running your backend locally using the DevAppServer.
     */
    @SuppressWarnings("PointlessBooleanExpression")
    protected static final boolean LOCAL_ANDROID_RUN = false && BuildConfig.DEBUG;

    /*
     * The root URL of where your DevAppServer is running (if you're running the
     * DevAppServer locally).
     */
    @SuppressWarnings("unused")
    protected static final String LOCAL_APP_ENGINE_SERVER_URL = "http://localhost:8080/";

    /*
     * The root URL of where your DevAppServer is running when it's being
     * accessed via the Android emulator (if you're running the DevAppServer
     * locally). In this case, you're running behind Android's virtual router.
     * See
     * http://developer.android.com/tools/devices/emulator.html#networkaddresses
     * for more information.
     */
    protected static final String LOCAL_APP_ENGINE_SERVER_URL_FOR_ANDROID = "http://10.0.2.2:8080";

    /**
     * Updates the Google client builder to connect the appropriate server based on whether
     * LOCAL_ANDROID_RUN is true or false and sets a custom user agent.
     *
     * @param builder Google client builder
     * @return same Google client builder
     */
    public static <B extends AbstractGoogleClient.Builder> B updateBuilder(Context context,
            B builder) {
        if (LOCAL_ANDROID_RUN) {
            builder.setRootUrl(LOCAL_APP_ENGINE_SERVER_URL_FOR_ANDROID + "/_ah/api/");
        }
        // used for user agent
        builder.setApplicationName("SeriesGuide " + Utils.getVersion(context));

        // only enable GZip when connecting to remote server
        final boolean enableGZip = builder.getRootUrl().startsWith("https:");

        builder.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
            public void initialize(AbstractGoogleClientRequest<?> request)
                    throws IOException {
                if (!enableGZip) {
                    request.setDisableGZipContent(true);
                }
            }
        });

        return builder;
    }
}

@@ -638,32 +638,32 @@
  to set 
+USE_
 LOCAL_
-ANDROID_RU
+VERSIO
 N to 'tr
@@ -771,25 +771,83 @@
 tils %7B%0A%0A    
-/
+private static final String PATH_API = %22/_ah/api/%22;%0A%0A    /*
 *%0A     * Cha
@@ -1027,62 +1027,255 @@
 ean 
+USE_
 LOCAL_
-ANDROID_RUN = false && BuildConfig.DEBUG;%0A%0A    /
+VERSION = false && BuildConfig.DEBUG;%0A%0A    @SuppressWarnings(%22PointlessBooleanExpression%22)%0A    private static final boolean USE_STAGING_VERSION = false && BuildConfig.DEBUG;%0A%0A    private static final String ROOT_URL_STAGING = %22%22;%0A%0A    /*
 *%0A  
@@ -1352,23 +1352,16 @@
 ning the
-%0A     *
  DevAppS
@@ -1365,16 +1365,23 @@
 ppServer
+%0A     *
  locally
@@ -1399,97 +1399,54 @@
     
-@SuppressWarnings(%22unused%22)%0A    protected static final String LOCAL_APP_ENGINE_SERVER_URL
+private static final String ROOT_URL_LOCALHOST
  = %22
@@ -1477,16 +1477,17 @@
 %0A%0A    /*
+*
 %0A     * 
@@ -1552,23 +1552,16 @@
 's being
-%0A     *
  accesse
@@ -1577,16 +1577,23 @@
  Android
+%0A     *
  emulato
@@ -1621,39 +1621,32 @@
 the DevAppServer
-%0A     *
  locally). In th
@@ -1675,16 +1675,23 @@
 g behind
+%0A     *
  Android
@@ -1712,26 +1712,12 @@
 ter.
-%0A     * See%0A     *
+ See
  htt
@@ -1826,31 +1826,29 @@
    */%0A    pr
-otec
+iva
 te
-d
  static fina
@@ -1860,39 +1860,26 @@
 ing 
-LOCAL_APP_ENGINE_SERVER_URL_FOR
+ROOT_URL_LOCALHOST
 _AND
@@ -2017,32 +2017,32 @@
 %0A     * 
+USE_
 LOCAL_
-ANDROID_RU
+VERSIO
 N is tru
@@ -2316,24 +2316,24 @@
 if (
+USE_
 LOCAL_
-ANDROID_RU
+VERSIO
 N) %7B
@@ -2368,61 +2368,148 @@
 Url(
-LOCAL_APP_ENGINE_SERVER_URL_FOR_ANDROID + %22/_ah/api/%22
+ROOT_URL_LOCALHOST_ANDROID + PATH_API);%0A        %7D else if (USE_STAGING_VERSION) %7B%0A            builder.setRootUrl(ROOT_URL_STAGING + PATH_API
 );%0A 

Support setting staging version for hexagon.