package com.qmth.boot.api.exception;

import com.qmth.boot.core.exception.CodeNameException;
import org.springframework.http.HttpStatus;

/**
 * 框架预留的异常类型，以及已使用的错误码
 */
public enum DefaultExceptionEnum {

    REQUEST_PARAMETER_INVALID(HttpStatus.BAD_REQUEST, 400000, "request parameter invalid"),
    HEADER_PLATFORM_INVALID(HttpStatus.BAD_REQUEST, 400001, "header platform invalid"),
    HEADER_DEVICEID_INVALID(HttpStatus.BAD_REQUEST, 400002, "header deviceId invalid"),
    ACCESS_FORBIDDEN(HttpStatus.FORBIDDEN, 403000, "access forbidden"),
    PLATFORM_FORBIDDEN(HttpStatus.FORBIDDEN, 403001, "platform not allowed"),
    IP_FORBIDDEN(HttpStatus.FORBIDDEN, 403002, "ip not allowed"),
    AUTHORIZATION_ERROR(HttpStatus.UNAUTHORIZED, 401000, "authorization error"),
    COMMON_EXCEPTION(HttpStatus.INTERNAL_SERVER_ERROR, 500000, "internal server error"),
    REENTRANT_EXCEPTION(HttpStatus.SERVICE_UNAVAILABLE, 503001, "service unavailable"),
    RATE_LIMITED(HttpStatus.SERVICE_UNAVAILABLE, 503002, "request rate limited"),
    RESOURCE_NOT_FOUND(HttpStatus.NOT_FOUND, 404000, "resource not found");

    private HttpStatus status;

    private int code;

    private String name;

    private DefaultExceptionEnum(HttpStatus status, int code, String name) {
        this.status = status;
        this.code = code;
        this.name = name;
    }

    public ApiException exception() {
        return new ApiException(status, code, name, null);
    }

    public ApiException exception(CodeNameException ex) {
        return new ApiException(status, ex.getCode() != null ? status.value() * 1000 + ex.getCode() : code,
                ex.getName() != null ? ex.getName() : name, ex.getMessage());
    }

    public ApiException exception(int code, String message) {
        return new ApiException(status, code, name, message);
    }

    public ApiException exception(String name, String message) {
        return new ApiException(status, code, name, message);
    }

    public ApiException exception(String message) {
        return new ApiException(status, code, name, message);
    }

    public HttpStatus getStatus() {
        return status;
    }

    public int getCode() {
        return code;
    }

    public String getName() {
        return name;
    }
}
