package com.microservices.wiki.config;



import cn.hutool.core.exceptions.ValidateException;
import com.microservices.common.core.web.domain.AjaxResult;
import com.microservices.wiki.refactoring.enums.ResponseEnum;
import com.microservices.wiki.refactoring.exception.BusiException;
import com.microservices.wiki.refactoring.utils.ValidatorUtils;
import javassist.NotFoundException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
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 org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class ExceptionInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(ExceptionInterceptor.class);
    private static String logTemplate = "request page url{}, error:{}";

    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler({IllegalArgumentException.class})
    public AjaxResult handle(HttpServletRequest request, IllegalArgumentException e) {
        logger.error(logTemplate, request.getRequestURI(), e.getMessage());
        return AjaxResult.error(e.getMessage());
    }

    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler({MethodArgumentNotValidException.class})
    public AjaxResult handle(HttpServletRequest request, MethodArgumentNotValidException e) {
        logger.error(logTemplate, request.getRequestURI(), e);
        return AjaxResult.error(ValidatorUtils.buildValidationdErrorMessage(e.getBindingResult()));
    }

    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler({BindException.class})
    public AjaxResult handle(HttpServletRequest request, BindException e) {
        logger.error(logTemplate, request.getRequestURI(), e);
        return AjaxResult.error(ValidatorUtils.buildValidationdErrorMessage(e.getBindingResult()));
    }

    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler({BusiException.class})
    public AjaxResult handle(HttpServletRequest request, BusiException e) {
        logger.error(logTemplate, request.getRequestURI(), e);
        return AjaxResult.error(e.getMessage());
    }

    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler({Exception.class})
    public AjaxResult handle(HttpServletRequest request, Exception e) {
        logger.error(e.toString() + "\n" + StringUtils.join(e.getStackTrace(), "\n"));
        return AjaxResult.error(ResponseEnum.SYS_ERROR.getValue());
    }

    @ResponseBody
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({ValidateException.class})
    public AjaxResult handle(HttpServletRequest request, ValidateException e) {
        return AjaxResult.error(ResponseEnum.BAD_REQUEST.getValue());
    }

    @ResponseBody
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler({NotFoundException.class})
    public AjaxResult handle(HttpServletRequest request, NotFoundException e) {
        logger.error(e.toString() + "\n" + StringUtils.join(e.getStackTrace(), "\n"));
        return AjaxResult.error(ResponseEnum.NOT_FOUND.getValue());
    }
}

