package de.champonthis.buntspecht.controller.support;

import java.io.IOException;
import java.lang.reflect.Type;

import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import com.google.gson.Gson;
import com.google.gson.JsonPrimitive;


@ControllerAdvice
public class JsonStringBodyControllerAdvice implements RequestBodyAdvice, ResponseBodyAdvice<String> {

	private Gson gson = new Gson();

	/*
	 * @see org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice#
	 * supports(org.springframework.core.MethodParameter, java.lang.reflect.Type,
	 * java.lang.Class)
	 */
	@Override
	public boolean supports(MethodParameter methodParameter, Type targetType,
			Class<? extends HttpMessageConverter<?>> converterType) {
		return targetType instanceof Class && String.class.equals((Class<?>) targetType);
	}

	/*
	 * @see org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice#
	 * beforeBodyRead(org.springframework.http.HttpInputMessage,
	 * org.springframework.core.MethodParameter, java.lang.reflect.Type,
	 * java.lang.Class)
	 */
	@Override
	public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
			Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
		return inputMessage;
	}

	/*
	 * @see org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice#
	 * afterBodyRead(java.lang.Object, org.springframework.http.HttpInputMessage,
	 * org.springframework.core.MethodParameter, java.lang.reflect.Type,
	 * java.lang.Class)
	 */
	@Override
	public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
			Class<? extends HttpMessageConverter<?>> converterType) {
		body = ((String) body).replaceAll("^\"|\"$", "");
		return body;
	}

	/*
	 * @see org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice#
	 * handleEmptyBody(java.lang.Object, org.springframework.http.HttpInputMessage,
	 * org.springframework.core.MethodParameter, java.lang.reflect.Type,
	 * java.lang.Class)
	 */
	@Override
	public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
			Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
		return body;
	}

	/*
	 * @see
	 * org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice#
	 * supports(org.springframework.core.MethodParameter, java.lang.Class)
	 */
	@Override
	public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
		return converterType == StringHttpMessageConverter.class;
	}

	/*
	 * @see
	 * org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice#
	 * beforeBodyWrite(java.lang.Object, org.springframework.core.MethodParameter,
	 * org.springframework.http.MediaType, java.lang.Class,
	 * org.springframework.http.server.ServerHttpRequest,
	 * org.springframework.http.server.ServerHttpResponse)
	 */
	@Override
	public String beforeBodyWrite(String body, MethodParameter returnType, MediaType selectedContentType,
			Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
			ServerHttpResponse response) {
		response.getHeaders().set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
		return gson.toJson(new JsonPrimitive(body));
	}

}