package io.ljunggren.ups.api.model;

public enum HttpStatusCode {

	OK(200, "OK response"),
	INVALID_REQUEST(400, "Invalid request"),
	UNAUTHORIZED(401, "Unauthorized"),
	FORBIDDEN(403, "Forbidden"),
	NOT_FOUND(404, "Not found"),
	THROTTLING(429, "Too many requests"),
	SERVER_ERROR(500, "Server error"),
	SERVICE_UNAVAILABLE(503, "Service unavailable");
	
	private int code;
	private String text;
	
	HttpStatusCode(int code, String text) {
		this.code = code;
		this.text = text;
	}
	
	public int getCode() {
		return code;
	}
	
	public String getText() {
		return text;
	}
	
	public boolean isRetryable() {
	    return this == SERVICE_UNAVAILABLE || this == THROTTLING || this == SERVER_ERROR;
	}
	
	public static HttpStatusCode getByCode(int code) {
	    for (HttpStatusCode responseCode : HttpStatusCode.values()) {
	        if (responseCode.getCode() == code) {
	            return responseCode;
	        }
	    }
	    return null;
	}
	
	public static String getTextByCode(int code) {
		HttpStatusCode responseCode = getByCode(code);
		return responseCode == null ? null : responseCode.getText();
	}
	
}
