package jp.studyplus.android.sdk.service.auth;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;

import com.google.common.base.Optional;

import jp.studyplus.android.sdk.BuildConfig;
import jp.studyplus.android.sdk.service.api.ApiCertification;

import static android.content.SharedPreferences.Editor;

/**
 * Saves and retrieves a certification which is required to use API.
 */
public class CertificationStore {
	private final SharedPreferences preferences;

	private static String KEY_ACCESS_TOKEN = "access_token";
	private static final String baseUrl = BuildConfig.API_ENDPOINT;

	public static CertificationStore create(Context context) {
		String prefName = "Certification:" + Uri.parse(baseUrl).getHost();
		SharedPreferences pref = context.getSharedPreferences(prefName, Context.MODE_PRIVATE);
		return new CertificationStore(pref, baseUrl);
	}
	private CertificationStore(SharedPreferences preferences, String baseUrl) {
		this.preferences = preferences;
	}

	public void update(Intent data){
		if (data == null){
			return;
		}
		String code = data.getStringExtra("sp_auth_result_code");
		if ("AUTHENTICATED".equals(code)){
			Editor editor = preferences.edit();
			editor.putString(KEY_ACCESS_TOKEN, data.getStringExtra("sp_auth_access_token"));
			editor.commit();
		}
	}
	private Optional<String> findAccessToken(){
		return Optional.fromNullable(preferences.getString(KEY_ACCESS_TOKEN, null));
	}

	/**
	 * Return the basic {@link ApiCertification} instance for API.
	 *
	 * @return certification instance for API.
	 * @throws AccessTokenNotFound
	 */
	public ApiCertification getDefault() {
		Optional<String> token = findAccessToken();
		if (!token.isPresent()){
			throw new AccessTokenNotFound();
		}
		return new ApiCertificationImpl(token.get(), baseUrl);
	}

	private static class ApiCertificationImpl implements ApiCertification{
		private final String accessToken;
		private final String baseUrl;

		private ApiCertificationImpl(String accessToken, String baseUrl) {
			this.accessToken = accessToken;
			this.baseUrl = baseUrl;
		}

		@Override
		public String getAccessToken() {
			return accessToken;
		}

		@Override
		public String getBaseUrl() {
			return baseUrl;
		}
	}
}

@@ -596,73 +596,8 @@
 en%22;
-%0A%09private static final String baseUrl = BuildConfig.API_ENDPOINT;
 %0A%0A%09p
@@ -703,23 +703,40 @@
 i.parse(
-baseUrl
+BuildConfig.API_ENDPOINT
 ).getHos
@@ -865,25 +865,16 @@
 ore(pref
-, baseUrl
 );%0A%09%7D%0A%09p
@@ -928,32 +928,16 @@
 ferences
-, String baseUrl
 ) %7B%0A%09%09th
@@ -1771,17 +1771,8 @@
 et()
-, baseUrl
 );%0A%09
@@ -1968,24 +1968,8 @@
 oken
-, String baseUrl
 ) %7B%0A
@@ -2021,23 +2021,40 @@
 seUrl = 
-baseUrl
+BuildConfig.API_ENDPOINT
 ;%0A%09%09%7D%0A%0A%09

Remove argument and constant value