package com.eactive.eai.authserver.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Configuration
@ControllerAdvice
public class WebMvcConfig {

    @ExceptionHandler({
        HttpMessageNotReadableException.class,
        MethodArgumentNotValidException.class,
        MissingServletRequestParameterException.class,
        BindException.class
    })
    public String handleBadRequest(HttpServletRequest request, HttpServletResponse response, Exception ex) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        request.setAttribute("_authServerStatusCode", HttpServletResponse.SC_BAD_REQUEST);
        request.setAttribute("errorMessage", "400 Bad Request");
        return "forward:/error.jsp";
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public String handleNotFound(HttpServletRequest request, HttpServletResponse response, Exception ex) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        request.setAttribute("_authServerStatusCode", HttpServletResponse.SC_NOT_FOUND);
        request.setAttribute("errorMessage", "404 Not Found");
        return "forward:/error.jsp";
    }

    @ExceptionHandler(AuthenticationException.class)
    public String handleUnauthorized(HttpServletRequest request, HttpServletResponse response, Exception ex) {
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        request.setAttribute("_authServerStatusCode", HttpServletResponse.SC_UNAUTHORIZED);
        request.setAttribute("errorMessage", "401 Unauthorized");
        return "forward:/error.jsp";
    }

    @ExceptionHandler(AccessDeniedException.class)
    public String handleAccessDenied(HttpServletRequest request, HttpServletResponse response, Exception ex) {
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        request.setAttribute("_authServerStatusCode", HttpServletResponse.SC_FORBIDDEN);
        request.setAttribute("errorMessage", "403 Forbidden");
        return "forward:/error.jsp";
    }

    @ExceptionHandler(Exception.class)
    public String handleException(HttpServletRequest request, HttpServletResponse response, Exception ex) {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        request.setAttribute("_authServerStatusCode", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        String detailMessage =  ex.getMessage();
        if ( StringUtils.isBlank(detailMessage) ) {
        	detailMessage = "광주은행 관리자에게 문의하세요.";
        }
        request.setAttribute("errorMessage", detailMessage);
        return "forward:/error.jsp";
    }
}
