package com.beecode.inz.common;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MultipartException;

import com.beecode.bap.attachment.exception.AttachmentDataUploadException;
import com.beecode.bcp.workflow.exception.WorkflowRuntimeException;
import com.beecode.inz.common.exception.BusinessException;
import com.beecode.inz.common.exception.ForbiddenOperateException;
import com.beecode.inz.common.exception.ParameterException;
import com.beecode.inz.common.exception.ResourceNotFoundException;
import com.beecode.inz.common.exception.UnOperateAuthorityException;

/**
 * 
 * @author tanshuqiang 2018年2月12日
 */
@ControllerAdvice()
@Component("com.beecode.inz.common.GlobalControllerExceptionHandler")
public class GlobalControllerExceptionHandler {

	private static final Logger logger = LoggerFactory.getLogger(GlobalControllerExceptionHandler.class);

	@Autowired
    private ErrorCodeMessageConfigurer errorCodeMessageConfigurer;
	
	@ResponseStatus(value = HttpStatus.BAD_REQUEST)
	@ExceptionHandler(ParameterException.class)
	@ResponseBody
	public RestServiceError handleParameterException(HttpServletRequest req, ParameterException ex) {
		logError(req, ex);
		return RestServiceError.build(ex, errorCodeMessageConfigurer);
	}

	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(UnOperateAuthorityException.class)
	@ResponseBody
	public RestServiceError handleNonAuthorizationException(HttpServletRequest req, UnOperateAuthorityException ex) {
		logError(req, ex);
		return RestServiceError.build(ex, errorCodeMessageConfigurer);
	}

	@ResponseStatus(value = HttpStatus.FORBIDDEN)
	@ExceptionHandler(ForbiddenOperateException.class)
	@ResponseBody
	public RestServiceError handleForbiddenOperationException(HttpServletRequest req, ForbiddenOperateException ex) {
		logError(req, ex);
		return RestServiceError.build(ex, errorCodeMessageConfigurer);
	}

	@ResponseStatus(value = HttpStatus.NOT_FOUND)
	@ExceptionHandler(ResourceNotFoundException.class)
	@ResponseBody
	public RestServiceError handleResourceNotFoundException(HttpServletRequest req, ResourceNotFoundException ex) {
		logError(req, ex);
		return RestServiceError.build(ex, errorCodeMessageConfigurer);
	}

	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(BusinessException.class)
	@ResponseBody
	public RestServiceError handleBusinessException(HttpServletRequest req, BusinessException ex) {
		logError(req, ex);
		return RestServiceError.build(ex, errorCodeMessageConfigurer);
	}
	
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(AttachmentDataUploadException.class)
	@ResponseBody
	public RestServiceError handleAttachmentDataUploadException(HttpServletRequest req, AttachmentDataUploadException ex) {
		logError(req, ex);
		return RestServiceError.build(ex, errorCodeMessageConfigurer);
	}
	
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(WorkflowRuntimeException.class)
	@ResponseBody
	public RestServiceError handleWorkflowRuntimeException(HttpServletRequest req, WorkflowRuntimeException ex) {
		logError(req, ex);
		return RestServiceError.build(ex, errorCodeMessageConfigurer);
	}
	
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(MultipartException.class)
	@ResponseBody
	public RestServiceError handleMultipartException(HttpServletRequest req, MultipartException ex) {
		logError(req, ex);
		return RestServiceError.build(ex, errorCodeMessageConfigurer);
	}

	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public RestServiceError handleException(HttpServletRequest req, Exception ex) {
		logError(req, ex);
		return RestServiceError.build(CommonErrorCodeEnum.INTERNAL_SERVER_ERROR, errorCodeMessageConfigurer);
	}

	private void logError(HttpServletRequest req, Throwable ex) {
		logger.error("Request:{}, raised:", req.getRequestURI(), ex);
		ex.printStackTrace();
	}

}