package org.devcloud.snippets.app;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.plus.Plus;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * Class with common utilities and constants needed across the app.
 */
public class Const {
  private static final String TAG = "Const";
  private static final String USER_ID_PREF_STRING = "user_id";

  static GoogleApiClient getGoogleApiClient(Activity activity) {
    GoogleApiClient.Builder client_builder = new GoogleApiClient.Builder(activity);

    client_builder.addApi(Plus.API, Plus.PlusOptions.builder().build());
    client_builder.addScope(Plus.SCOPE_PLUS_LOGIN);

    if (activity instanceof ConnectionCallbacks) {
      client_builder.addConnectionCallbacks((ConnectionCallbacks) activity);
    } else {
      Log.w(TAG, "Activity should implement ConnectionCallbacks");
    }

    if (activity instanceof OnConnectionFailedListener) {
      client_builder.addOnConnectionFailedListener((OnConnectionFailedListener) activity);
    } else {
      Log.w(TAG, "Activity should implement OnConnectionFailedListener");
    }

    return client_builder.build();
  }

  static SharedPreferences getPreferences(Context context) {
    return context.getSharedPreferences("snippets_preferences", Context.MODE_PRIVATE);
  }

  static String getUserId(Context context) {
    SharedPreferences prefs = Const.getPreferences(context);
    return prefs.getString(USER_ID_PREF_STRING, "");
  }

  @SuppressLint("CommitPrefEdits")
  static void setUserId(Context context, String user_id) {
    SharedPreferences prefs = Const.getPreferences(context);
    prefs.edit().putString(USER_ID_PREF_STRING, user_id).apply();
  }

  @SuppressLint("CommitPrefEdits")
  public static void deleteUserId(Context context) {
    SharedPreferences prefs = Const.getPreferences(context);
    prefs.edit().remove(USER_ID_PREF_STRING).apply();
  }

  /**
   * @param route URI path to hit starting with a /.
   */
  static String getServerUrl(String route) throws MalformedURLException, URISyntaxException {
    String host;
    URI url;
    if (BuildConfig.DEBUG) {
      host = "https://snippets-app-api-test.herokuapp.com";
    } else {
      host = "https://snippets-app-api.herokuapp.com";
    }

    url = new URI(host + route);
    return url.toURL().toString();
  }

  /**
   * Tell us if we have any internet connection.
   *
   * @param context Current context.
   *
   * @return Whether or not we have a network connection.
   */
  static boolean isNetworkAvailable(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    return ni != null && ni.isConnected();
  }
}

@@ -840,782 +840,8 @@
 %22;%0A%0A
-  static GoogleApiClient getGoogleApiClient(Activity activity) %7B%0A    GoogleApiClient.Builder client_builder = new GoogleApiClient.Builder(activity);%0A%0A    client_builder.addApi(Plus.API, Plus.PlusOptions.builder().build());%0A    client_builder.addScope(Plus.SCOPE_PLUS_LOGIN);%0A%0A    if (activity instanceof ConnectionCallbacks) %7B%0A      client_builder.addConnectionCallbacks((ConnectionCallbacks) activity);%0A    %7D else %7B%0A      Log.w(TAG, %22Activity should implement ConnectionCallbacks%22);%0A    %7D%0A%0A    if (activity instanceof OnConnectionFailedListener) %7B%0A      client_builder.addOnConnectionFailedListener((OnConnectionFailedListener) activity);%0A    %7D else %7B%0A      Log.w(TAG, %22Activity should implement OnConnectionFailedListener%22);%0A    %7D%0A%0A    return client_builder.build();%0A  %7D%0A%0A
   st

Remove unused method