package com.blueland.ebpm.exception;

import java.util.Map;

import org.activiti.engine.ActivitiObjectNotFoundException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.transaction.UnexpectedRollbackException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
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.servlet.NoHandlerFoundException;

import com.blueland.bpm.engine.core.model.ExceptionErrorCode;
import com.blueland.bpm.engine.core.model.ProcessExeErrorShare;
import com.blueland.bpm.engine.util.WorkFlowException;

@ControllerAdvice
public class ExceptionHandle {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	@ResponseBody
	@ExceptionHandler
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public ErrorInfo handleNotFound(ActivitiObjectNotFoundException e) {
		return new ErrorInfo(ExceptionErrorCode.B2000.name(), "Object Not found", e);
	}
	
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	@ResponseBody
	@ExceptionHandler
	public ErrorInfo handleWorkFlowException(WorkFlowException e) {
		logger.error("",e);
		return new ErrorInfo(e.getExceptionErrorCode().name() ,e.getMessage(), e);
	}
	
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	@ResponseBody
	@ExceptionHandler
	public ErrorInfo handleRestException(RestException e) {
		logger.error("",e);
		return new ErrorInfo(e.getErrCode(), "Excute Error", e);
	}
	
	
	
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	@ResponseBody
	@ExceptionHandler
	public ErrorInfo handleNullPointerException(NullPointerException e) {
		logger.error("",e);
		return new ErrorInfo(ExceptionErrorCode.A1000.name(), "Null Pointer", e);
	}
	
	@ResponseStatus(value=HttpStatus.NOT_FOUND)
	@ResponseBody
	@ExceptionHandler
	public ErrorInfo handleNoHandlerFoundException(NoHandlerFoundException e) {
		return new ErrorInfo(ExceptionErrorCode.A1000.name(), "No HandlerFound", e);
	}
	
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	@ResponseBody
	@ExceptionHandler
	public ErrorInfo handleJsonPropertyException(HttpMessageNotReadableException e) {
		logger.error("",e);
		return new ErrorInfo(ExceptionErrorCode.A1000.name(), "Json Error", e);
	}
	
	@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
	@ResponseBody
	@ExceptionHandler
	public ErrorInfo handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
		return new ErrorInfo(ExceptionErrorCode.A1000.name(), "Http Method Error", e);
	}
	
	@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
	@ResponseBody
	@ExceptionHandler
	public ErrorInfo handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
		return new ErrorInfo(ExceptionErrorCode.A1000.name(), "Media Type Error", e);
	}
	
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	@ResponseBody
	@ExceptionHandler
	public ErrorInfo handleIllegalArgumentException(IllegalArgumentException e) {
		return new ErrorInfo(ExceptionErrorCode.A1000.name(), "Illegal Argument", e);
	}
	
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	@ResponseBody
	@ExceptionHandler
	public ErrorInfo handleUnexpectedRollbackException(UnexpectedRollbackException e) {
		logger.error("",e);
		Map resultMap=ProcessExeErrorShare.getProcessExeErrorData();
		if(resultMap!=null){
			ExceptionErrorCode exceptionErrorCode=(ExceptionErrorCode) resultMap.get("executeErrorCode");
			String executeErrorMsg=(String) resultMap.get("executeErrorMsg");
			if(StringUtils.isNotEmpty(executeErrorMsg)){
				return new ErrorInfo(exceptionErrorCode.name(), executeErrorMsg, e);
			}
		}
		return new ErrorInfo(ExceptionErrorCode.A1000.name(), "Database Error", e);
	}
}
