package edu.rpi.cs.yacs.retrofit;

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

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

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

public class ServiceHelper {

    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);

        Log.d("Persistent BaseUrl", baseUrl);

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

        service = retrofit.create(YACSService.class);

        return service;
    }

    /**
     * This method returns the YACS URL of the input college name.
     * @param college name of college
     * @return College YACS URL if College supports YACS, return RPI's YACS URL otherwise
     */
    private String convertHouseToURL(String college) {
        switch (college) {
            case "Rensselaer Polytechnic Institute":
                return "https://yacs.cs.rpi.edu/api/v5/";
            default :
                return "https://yacs.cs.rpi.edu/api/v5/";
        }
    }
}
@@ -710,29 +710,23 @@
 latest %22
-House of YACS
+College
 %22 settin
@@ -973,27 +973,23 @@
  String 
-houseOfYACS
+college
  = prefe
@@ -1010,21 +1010,15 @@
 ng(%22
-House of YACS
+College
 %22, %22
@@ -1065,30 +1065,28 @@
 vert
-Hous
+Colleg
 eToURL(
-houseOfYACS
+college
 );%0A%0A
@@ -1639,12 +1639,14 @@
 vert
-Hous
+Colleg
 eToU

Update ServiceHelper.java