package com.atlassian.bitbucket.mesh.throttle;

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

public enum ThrottledResource {

    /**
     * LFS resource. Acquiring a ticket for this resource allows execution of LFS operations.
     */
    LFS("git-lfs"),
    /**
     * SCM mirror hosting resource. Acquiring a ticket for this resource allows execution of hosting operations from
     * a mirror.
     */
    MIRROR_PACK("mirror-hosting"),
    /**
     * The SCM command resource. Acquiring a ticket for this resource allows execution of SCM commands <i>excluding</i>
     * push/pull-type operations.
     */
    COMMAND("scm-command"),
    /**
     * The SCM hosting resource. Acquiring a ticket for this resource allows execution of hosting operations.
     */
    PACK("scm-hosting"),
    /**
     * Ref. advertisement resource. Acquiring a ticket for this resources allows execution of ref. advertisement
     * operations.
     */
    REFS("scm-refs");

    private final String id;

    ThrottledResource(String id) {
        this.id = id;
    }

    @Nonnull
    public static ThrottledResource forId(@Nonnull String id) {
        requireNonNull(id, "id");
        for (ThrottledResource value : values()) {
            if (value.id.equals(id)) {
                return value;
            }
        }
        throw new NoSuchResourceException("Resource [" + id + "] is not supported", id);
    }

    @Nonnull
    public String getId() {
        return id;
    }

    @Override
    public String toString() {
        return id;
    }
}
