package me.proxylink.common;

import java.util.HashMap;
import java.util.Map;

public enum FrameType {
    HELLO(1),
    AUTH_CHALLENGE(2),
    AUTH_RESPONSE(3),
    AUTH_OK(4),
    AUTH_FAILED(5),
    PING(6),
    PONG(7),
    STREAM_OPEN(10),
    STREAM_DATA(11),
    STREAM_CLOSE(12),
    STREAM_RESET(13),
    ERROR(20),
    GOAWAY(21);

    private static final Map<Integer, FrameType> BY_CODE = new HashMap<>();

    static {
        for (FrameType type : values()) {
            BY_CODE.put(type.code, type);
        }
    }

    private final int code;

    FrameType(int code) {
        this.code = code;
    }

    public int code() {
        return code;
    }

    public static FrameType fromCode(int code) throws ProtocolException {
        FrameType type = BY_CODE.get(code);
        if (type == null) {
            throw new ProtocolException("Unknown frame type: " + code);
        }
        return type;
    }
}
