package com.atlassian.bitbucket.mesh.git.hosting;

import com.atlassian.bitbucket.mesh.rpc.v1.RpcPackCacheResult;
import com.atlassian.util.contentcache.CacheResult;

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

/**
 * Represents the different hosting cache results that are supported.
 */
public enum HostingCacheResult {

    BYPASS(RpcPackCacheResult.PACK_CACHE_BYPASS),
    HIT(RpcPackCacheResult.PACK_CACHE_HIT),
    MISS(RpcPackCacheResult.PACK_CACHE_MISS),
    NONE(RpcPackCacheResult.PACK_CACHE_NONE),
    UNRECOGNIZED(RpcPackCacheResult.UNRECOGNIZED);

    private final RpcPackCacheResult rpc;

    HostingCacheResult(@Nonnull RpcPackCacheResult rpc) {
        this.rpc = rpc;
    }

    @Nonnull
    public static HostingCacheResult fromCacheResult(@Nonnull CacheResult cacheResult) {
        switch (requireNonNull(cacheResult, "cacheResult")) {
            case BYPASS:
                return BYPASS;
            case HIT:
                return HIT;
            case MISS:
                return MISS;
        }
        return UNRECOGNIZED;
    }

    @Nonnull
    public RpcPackCacheResult toRpc() {
        return rpc;
    }
}
