package com.tiesheng.core.config.exception;

import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.extra.spring.SpringUtil;
import com.tiesheng.core.config.web.TieshengWebConfigurer;
import com.tiesheng.util.exception.ApiException;
import com.tiesheng.util.exception.ApiRespEnum;
import com.tiesheng.util.pojos.ApiResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.MultipartProperties;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.io.IOException;

/**
 * @author huang
 */
@RestControllerAdvice
public class SpringExceptionHandler {

    @Autowired(required = false)
    TieshengWebConfigurer tieshengWebConfigurer;


    /**
     * 全局异常捕获
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ApiResp<String> handle(Exception e) {

        // 自定义异常
        if (e instanceof ApiException) {
            return ((ApiException) e).getApiResp();
        }

        // 请求异常
        if (e instanceof ValidateException) {
            return ApiResp.respCust(ApiRespEnum.BadRequest.getCode(), e.getMessage());
        }
        if (e instanceof BindException) {
            FieldError error = ((BindException) e).getFieldError();
            String msg = "请求参数不合法";
            if (error != null) {
                msg = error.getDefaultMessage();
            }
            return ApiResp.respCust(ApiRespEnum.BadRequest.getCode(), msg);
        }
        if (e instanceof MethodArgumentTypeMismatchException) {
            return ApiResp.respCust(ApiRespEnum.BadRequest.getCode(), "请求参数不合法");
        }
        if (e instanceof HttpRequestMethodNotSupportedException) {
            return ApiResp.respCust(ApiRespEnum.BadRequest.getCode(), "不支持的请求方式");
        }
        if (e instanceof NoHandlerFoundException) {
            return ApiResp.respCust(ApiRespEnum.BadRequest.getCode(), "请求路径不存在");
        }
        if (e instanceof MaxUploadSizeExceededException) {
            MultipartProperties property = SpringUtil.getBean(MultipartProperties.class);
            return ApiResp.respCust(ApiRespEnum.BadRequest.getCode(), String.format("上传文件不得超过%dMB", property.getMaxFileSize().toMegabytes()));
        }

        // 服务器内部异常
        if (e instanceof IOException) {
            return ApiResp.respCust(ApiRespEnum.ServerError.getCode(), "IO异常");
        }

        if (tieshengWebConfigurer != null) {
            return tieshengWebConfigurer.addExceptionHandler(e);
        }
        return ApiResp.respCust(ApiRespEnum.ServerError);
    }

}
