package edu.rpi.cs.yacs.retrofit;

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

import java.lang.annotation.Retention;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
 * Created by Mark Robinson on 9/24/16.
 */

public class ServiceHelper {
    @Retention(RUNTIME)
    @interface Json {
    }

    private YACSService service;
    private Context context;

    public ServiceHelper(Context context) {
        this.context = context;

        invalidateService();
    }

    public YACSService getService() {
        return service;
    }

    /**
     * This method is used when we need to invalidate the YACSService
     * @return new YACSService that uses the latest "House of YACS" setting
     */
    public YACSService invalidateService() {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

        // If exists, set to String value
        // Otherwise, set to "unknown"
        String houseOfYACS = preferences.getString("House of YACS", "unknown");

        String baseURL = convertHouseToURL(houseOfYACS);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseURL)
                .addConverterFactory(new JsonStringConverterFactory(GsonConverterFactory.create()))
                .build();

        service = retrofit.create(YACSService.class);

        return service;
    }

    /**
     * This method returns the YACS URL of the input college name.
     * @param house College name
     * @return College YACS URL if College supports YACS. Otherwise, returns "not supported yet"
     */
    private String convertHouseToURL(String house) {
        switch (house) {
            case "Rensselaer Polytechnic Institute":
                return "https://yacs.cs.rpi.edu/api/v5/";
            default :
                return "not supported yet";
        }
    }
}
@@ -146,16 +146,41 @@
 Manager;
+%0Aimport android.util.Log;
 %0A%0Aimport
@@ -1218,18 +1218,18 @@
 ng baseU
-RL
+rl
  = conve
@@ -1252,24 +1252,71 @@
 seOfYACS);%0A%0A
+        Log.d(%22Persistent BaseUrl%22, baseUrl);%0A%0A
         Retr
@@ -1384,18 +1384,18 @@
 rl(baseU
-RL
+rl
 )%0A      
@@ -1429,39 +1429,8 @@
 ory(
-new JsonStringConverterFactory(
 Gson
@@ -1455,17 +1455,16 @@
 reate())
-)
 %0A       

Fix Retrofit Builder