package de.fu_berlin.cdv.chasingpictures.api;

import android.content.Context;
import android.support.annotation.StringRes;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.List;

import de.fu_berlin.cdv.chasingpictures.R;

/**
 * @author Simon Kalt
 */
public class ApiUtil {
    private final Context context;

    public ApiUtil(Context context) {
        this.context = context;
    }

    /**
     * Builds a basic JSON rest template for sending requests.
     * @return A RestTemplate with a {@link org.springframework.http.converter.json.MappingJackson2HttpMessageConverter} attached.
     */
    public static RestTemplate buildJsonRestTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        restTemplate.setErrorHandler(new DefaultAPIResponseErrorHandler());
        return restTemplate;
    }

    /**
     * Retrieves the API URI for the specified endpoint.
     * @param endpointId An android resource id pointing to an R.strings.api_path_* value
     * @return The URI to send your request to
     */
    public String getURIforEndpoint(@StringRes int endpointResID) {
        return context.getString(R.string.api_url) + context.getString(endpointResID);
    }

    /**
     * Returns <i>the first</i> header for the header field named by the given String.
     * @param responseEntity The request response
     * @param key The resource ID for the header field
     * @return A list of strings containing the values for this header field
     */
    public static String getHeader(ResponseEntity<?> responseEntity, String key) {
        List<String> headers = getHeaders(responseEntity, key);
        return headers == null || headers.isEmpty() ? null : headers.get(0);
    }

    public static List<String> getHeaders(ResponseEntity<?> responseEntity, String key) {
        HttpHeaders headers = responseEntity.getHeaders();
        return headers == null ? null : headers.get(key);
    }

    /**
     * This default error handler, ignores the 403 error code,
     * which is not always a fatal error.
     */
    public static class DefaultAPIResponseErrorHandler extends DefaultResponseErrorHandler {
        public void handleError(ClientHttpResponse response) throws IOException {
            final HttpStatus statusCode = response.getStatusCode();
            if (statusCode == HttpStatus.FORBIDDEN || statusCode == HttpStatus.UNAUTHORIZED) {
                // Request was denied,
                // do nothing and return.
                return;
            }
            super.handleError(response);
        }
    }
}

@@ -851,16 +851,23 @@
 quests.%0A
+     *%0A
      * @
@@ -1379,16 +1379,23 @@
 dpoint.%0A
+     *%0A
      * @
@@ -1412,21 +1412,15 @@
 oint
-Id An android
+ResID A
  res
@@ -1714,23 +1714,25 @@
 rns 
-%3Ci%3E
 the 
+%3Cem%3E
 first%3C/
-i
+em
 %3E he
@@ -1736,24 +1736,30 @@
  header 
+value 
 for the 
 header f
@@ -1754,47 +1754,25 @@
 the 
-header field named by the given String.
+given key.%0A     *
 %0A   
@@ -1840,27 +1840,30 @@
 key 
-The resource ID for
+           The name of
  the
@@ -1897,23 +1897,14 @@
 n A 
-list of 
 string
-s
  con
@@ -1919,14 +1919,19 @@
 the 
+first 
 value
-s
  for
@@ -2184,24 +2184,294 @@
 (0);%0A    %7D%0A%0A
+    /**%0A     * Returns all available header values for the given key.%0A     *%0A     * @param responseEntity The request response%0A     * @param key            The name of the header field%0A     * @return A list of strings containing the values for this header field%0A     */%0A
     public s

Update documentation for ApiUtil