package org.mtransit.android.commons;

import org.mtransit.android.commons.task.MTAsyncTask;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class PreferenceUtils {

	private static final String TAG = PreferenceUtils.class.getSimpleName();

	public static final String LCL_PREF_NAME = "lcl";

	public static final String PREFS_DISTANCE_UNIT = "pDistanceUnit";
	public static final String PREFS_DISTANCE_UNIT_METRIC = "metric";
	public static final String PREFS_DISTANCE_UNIT_IMPERIAL = "imperial";
	public static final String PREFS_DISTANCE_UNIT_DEFAULT = PREFS_DISTANCE_UNIT_METRIC;

	public static final String PREFS_LCL_NEARBY_TAB_TYPE = "pNearbyTabType";

	public static final int PREFS_LCL_NEARBY_TAB_TYPE_DEFAULT = -1;

	private static final String PREFS_LCL_AGENCY_TYPE_TAB_AGENCY = "pAgencyTypeTabAgency";

	public static String getPREFS_LCL_AGENCY_TYPE_TAB_AGENCY(int typeId) {
		return PREFS_LCL_AGENCY_TYPE_TAB_AGENCY + typeId;
	}

	public static final String PREFS_LCL_AGENCY_TYPE_TAB_AGENCY_DEFAULT = null;

	public static final String PREFS_LCL_ROOT_SCREEN_ITEM_ID = "pRootScreenItemId";

	public static final String PREFS_LCL_ROOT_SCREEN_ITEM_ID_DEFAULT = null; // worst default

	public static String getPrefDefault(Context context, String prefKey, String defaultValue) {
		if (context == null) {
			MTLog.w(TAG, "Context null, using default value '%s' for preference '%s'!", defaultValue, prefKey);
			return defaultValue;
		}
		return getPref(PreferenceManager.getDefaultSharedPreferences(context), prefKey, defaultValue);
	}

	public static int getPrefLcl(Context context, String prefKey, int defaultValue) {
		return getPref(context.getSharedPreferences(LCL_PREF_NAME, Context.MODE_PRIVATE), prefKey, defaultValue);
	}

	public static long getPrefLcl(Context context, String prefKey, long defaultValue) {
		return getPref(context.getSharedPreferences(LCL_PREF_NAME, Context.MODE_PRIVATE), prefKey, defaultValue);
	}

	public static String getPrefLcl(Context context, String prefKey, String defaultValue) {
		return getPref(context.getSharedPreferences(LCL_PREF_NAME, Context.MODE_PRIVATE), prefKey, defaultValue);
	}

	private static int getPref(SharedPreferences sharedPreferences, String prefKey, int defaultValue) {
		return sharedPreferences.getInt(prefKey, defaultValue);
	}

	private static long getPref(SharedPreferences sharedPreferences, String prefKey, long defaultValue) {
		return sharedPreferences.getLong(prefKey, defaultValue);
	}

	private static String getPref(SharedPreferences sharedPreferences, String prefKey, String defaultValue) {
		return sharedPreferences.getString(prefKey, defaultValue);
	}

	public static void savePrefLcl(final Context context, final String prefKey, final int newValue, final boolean sync) {
		if (sync) {
			savePref(context, context.getSharedPreferences(LCL_PREF_NAME, Context.MODE_PRIVATE), prefKey, newValue);
		} else {
			new MTAsyncTask<Void, Void, Void>() {
				@Override
				public String getLogTag() {
					return TAG;
				}

				@Override
				protected Void doInBackgroundMT(Void... params) {
					savePref(context, context.getSharedPreferences(LCL_PREF_NAME, Context.MODE_PRIVATE), prefKey, newValue);
					return null;
				}
			}.execute();
		}
	}

	public static void savePrefLcl(final Context context, final String prefKey, final long newValue, final boolean sync) {
		if (sync) {
			savePref(context, context.getSharedPreferences(LCL_PREF_NAME, Context.MODE_PRIVATE), prefKey, newValue);
		} else {
			new MTAsyncTask<Void, Void, Void>() {
				@Override
				public String getLogTag() {
					return TAG;
				}

				@Override
				protected Void doInBackgroundMT(Void... params) {
					savePref(context, context.getSharedPreferences(LCL_PREF_NAME, Context.MODE_PRIVATE), prefKey, newValue);
					return null;
				}
			}.execute();
		}
	}

	public static void savePrefLcl(final Context context, final String prefKey, final String newValue, final boolean sync) {
		if (sync) {
			savePref(context, context.getSharedPreferences(LCL_PREF_NAME, Context.MODE_PRIVATE), prefKey, newValue);
		} else {
			new MTAsyncTask<Void, Void, Void>() {
				@Override
				public String getLogTag() {
					return TAG;
				}

				@Override
				protected Void doInBackgroundMT(Void... params) {
					savePref(context, context.getSharedPreferences(LCL_PREF_NAME, Context.MODE_PRIVATE), prefKey, newValue);
					return null;
				}
			}.execute();
		}
	}

	private static void savePref(Context context, final SharedPreferences sharedPreferences, String prefKey, int newValue) {
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putInt(prefKey, newValue);
		editor.apply();
	}

	private static void savePref(Context context, final SharedPreferences sharedPreferences, String prefKey, long newValue) {
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putLong(prefKey, newValue);
		editor.apply();
	}

	private static void savePref(Context context, final SharedPreferences sharedPreferences, String prefKey, String newValue) {
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putString(prefKey, newValue);
		editor.apply();
	}

}

@@ -1094,16 +1094,332 @@
  null;%0A%0A
+%09private static final String PREFS_LCL_RTS_ROUTE_TRIP_ID_TAB = %22pRTSRouteTripIdTab%22;%0A%0A%09public static String getPREFS_LCL_RTS_ROUTE_TRIP_ID_TAB(String authority, int routeId) %7B%0A%09%09return PREFS_LCL_RTS_ROUTE_TRIP_ID_TAB + authority + routeId;%0A%09%7D%0A%0A%09public static final int PREFS_LCL_RTS_ROUTE_TRIP_ID_TAB_DEFAULT = -1;%0A%0A
 %09public 

Add use preference for route trip.