package gaf3.core.cloud.web

import gaf3.core.data.ErrorResult
import gaf3.core.exception.BusinessError
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.core.Ordered
import org.springframework.core.annotation.Order
import org.springframework.http.HttpStatus
import org.springframework.http.server.ServerHttpResponse
import org.springframework.web.bind.MethodArgumentNotValidException
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.RestControllerAdvice


@Order(Ordered.LOWEST_PRECEDENCE)
@RestControllerAdvice
class GafControllerAdvice {
    val log: Logger = LoggerFactory.getLogger(GafControllerAdvice::class.java)

    @ExceptionHandler(BusinessError::class)
    @ResponseBody
    fun handleError(ex: BusinessError, res: ServerHttpResponse): ErrorResult {
        log.warn("BusinessError:{}-{}", ex.errcode, ex.errmsg)
        ex.details?.let{ log.warn("details: {}", ex.details) }
        log.debug("handleError", ex)
        res.setStatusCode(HttpStatus.BAD_REQUEST)
        return ErrorResult(ex.errcode, ex.errmsg, ex.details)
    }

    @ExceptionHandler(MethodArgumentNotValidException::class)
    @ResponseBody
    fun handleValid(ex: MethodArgumentNotValidException, res: ServerHttpResponse): ErrorResult{
        log.warn("MethodArgumentNotValidException:{}", ex.message)
        log.debug("handleValid", ex)
        res.setStatusCode(HttpStatus.BAD_REQUEST)
        return ErrorResult(errcode = BusinessError.ERR_BADPARAM, errmsg = "请求参数错误", details = ex.message)
    }

    @ExceptionHandler(IllegalArgumentException::class)
    @ResponseBody
    fun handleAssert(ex: IllegalArgumentException, res: ServerHttpResponse): ErrorResult{
        log.warn("IllegalArgumentException:{}", ex.message)
        log.debug("handleAssert", ex)
        res.setStatusCode(HttpStatus.BAD_REQUEST)
        return ErrorResult(errcode = BusinessError.ERR_BADPARAM, errmsg = "请求参数错误", details = ex.message)
    }

//    @ExceptionHandler(ConstraintViolationException::class)
//    @ResponseBody
//    fun handleValidate(ex: ConstraintViolationException, res: ServerHttpResponse): ErrorResult{
//        log.warn("ConstraintViolationException:{}", ex.message)
//        log.debug("handleAssert", ex)
//        res.setStatusCode(HttpStatus.BAD_REQUEST)
//        return ErrorResult(errcode = BusinessError.ERR_BADPARAM, errmsg = "请求参数错误", details = ex.message)
//    }

    // 默认错误处理
    @ExceptionHandler(Throwable::class)
    @ResponseBody
    @ConditionalOnProperty(name = ["debug"], havingValue = "false", matchIfMissing = true)
    fun handleExcept(ex: Throwable, res: ServerHttpResponse): ErrorResult{
        log.warn("系统错误:{}", ex.message)
        log.debug("handleExcept", ex)
        res.setStatusCode(HttpStatus.BAD_REQUEST)
        return errorResult(ex)
    }
}