package com.hs.api.common.handlers;

import com.hs.api.common.Result;
import com.hs.api.common.exceptions.DBConfigurationError;
import com.hs.api.common.exceptions.ExceptionBase;
import com.hs.api.common.exceptions.InvalidTokenException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 全局异常处理类
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 其他异常处理
     *
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object defaultErrorHandler(HttpServletRequest request,Exception e) {
        String url = request.getRequestURI();
        log.error(url + "请求未知异常:" + e.getMessage(),e);
        return Result.error();
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public Object MethodArgumentNotValidErrorHandler(HttpServletRequest request,MethodArgumentNotValidException e) {
        String url = request.getRequestURI();

        BindingResult bindingResult = e.getBindingResult();
        List<ObjectError> allErrors = bindingResult.getAllErrors();
        StringBuilder errorStrBu = new StringBuilder();
        allErrors.forEach(objectError -> {
            FieldError fieldError = (FieldError)objectError;
            errorStrBu.append(fieldError.getDefaultMessage());
            errorStrBu.append(",");
        });
        String errorStr = errorStrBu.toString();


        log.error(url + "请求未知异常:" + e.getMessage(),e);
        return Result.error(errorStr.substring(0,errorStr.length() - 1));
    }

    /**
     * 其他内部异常
     *
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(ExceptionBase.class)
    @ResponseBody
    public Object BaseErrorHandler(HttpServletRequest request,Exception e) {
        String url = request.getRequestURI();
        log.error(url + "请求未知异常:" + e.getMessage(),e);
        return Result.error();
    }

    @ExceptionHandler(DBConfigurationError.class)
    @ResponseBody
    public Object DBConfigurationErrorHandler(HttpServletRequest request,Exception e) {
        String url = request.getRequestURI();
        log.error(url + "请求未知异常:" + e.getMessage(),e);
        return Result.error(400,e.getMessage());
    }

    /**
     * 无效token
     *
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(InvalidTokenException.class)
    @ResponseBody
    public Object InvalidTokenExceptionHandler(HttpServletRequest request,Exception e) {
        String url = request.getRequestURI();
        log.error(url + "请求未知异常:" + e.getMessage(),e);
        return Result.error(Result.ErrorCode.INVALID_TOKEN);
    }
}
