package com.ejie.ab79.rest.serviciosWeb;

import java.util.Locale;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ejie.ab79.comun.Constantes;
import com.ejie.ab79.model.REST.servicioWeb.GenericResponse;

/**
 * Error Controller. handles the calls for 404, 500 and 401 HTTP Status codes.
 */
@Controller
@RequestMapping(value = ErrorController.ERROR_URL, produces = Constantes.APPLICATION_JSON_UTF8_VALUE)
public class ErrorController {

	@Resource()
	ReloadableResourceBundleMessageSource appMessageSource;
	
    public static final String ERROR_URL = "/error";
    public static final String TILE_ERROR = "error.page";
    
    /**
     * Page Not Found.
     *
     * @return Home Page
     */
    @RequestMapping(value = "/404")
    public @ResponseBody() GenericResponse notFound(HttpServletResponse response) {
		response.setStatus(HttpServletResponse.SC_NOT_FOUND);
    	String error = "error.404";
    	
		return new GenericResponse(Constantes.ENVIO_JSON_ESTADO_ERROR, error,
				this.appMessageSource.getMessage(error, null, new Locale("es")),
				this.appMessageSource.getMessage(error, null, new Locale("eu")));
    }

    /**
     * Error page.
     *
     * @return the model and view
     */
    @RequestMapping(value = "/500")
    public @ResponseBody() GenericResponse errorPage(HttpServletResponse response) {
		response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    	String error = "error.500";
    	
		return new GenericResponse(Constantes.ENVIO_JSON_ESTADO_ERROR, error,
				this.appMessageSource.getMessage(error, null, new Locale("es")),
				this.appMessageSource.getMessage(error, null, new Locale("eu")));
    }
}