package @RESPONSE_PCK@;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import it.micegroup.voila2runtime.response.Messages;

@RestControllerAdvice
public class BaseResponseEntityWrapper<T> implements ResponseBodyAdvice<T> {

	private static final Logger LOGGER = LoggerFactory.getLogger(BaseResponseEntityWrapper.class);

	private Messages messages;

	@Override
	public boolean supports(MethodParameter returnType, Class converterType) {
		return true;
	}

	@Override
	public T beforeBodyWrite(T baseResponse, MethodParameter returnType,
		      MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType,
		      ServerHttpRequest request, ServerHttpResponse response) {

		   this.messages = new Messages();
		   return baseResponse;
	}
	
	
	//TODO capire se � ok questo comportamento
	private BaseResponse<T> wrapIterableResponse(BaseResponse<T> baseResponse) {
		// TODO decidere come trattare la risposta in caso di liste paginate ecc...
		return baseResponse;
	}

	private BaseResponse<T> wrapDetailResponse(BaseResponse<T> baseResponse) {
		return baseResponse;
	}
	
	
//	private BaseResponse<T> wrapIterableResponse(T body) {
//		// TODO decidere come trattare la risposta in caso di liste paginate ecc...
//		return new BaseResponse<>(body);
//	}
//
//	private BaseResponse<T> wrapDetailResponse(T body) {
//		return new BaseResponse<>(body);
//	}

	public Messages getMessages() {
		if (messages == null) {
			messages = new Messages();
		}
		return this.messages;
	}
}