package com.atlassian.bitbucket.mesh.repository;

import com.atlassian.bitbucket.mesh.rpc.v1.RpcReplicaState;

import javax.annotation.Nonnull;

/**
 * The state of a repository replica on a Mesh node
 */
public enum ReplicaState {

    /**
     * The replica is consistent; the content hash and metadata hash match with a known good replica.
     */
    CONSISTENT(RpcReplicaState.REPLICA_STATE_CONSISTENT),
    /**
     * The replica is known to be inconsistent.
     */
    INCONSISTENT(RpcReplicaState.REPLICA_STATE_INCONSISTENT),
    /**
     * The repository replica does not exist on the node.
     */
    MISSING(RpcReplicaState.REPLICA_STATE_MISSING),
    /**
     * The replica is currently being repaired.
     */
    REPAIRING(RpcReplicaState.REPLICA_STATE_REPAIRING),
    /**
     * The state of the replica is currently unknown.
     */
    UNKNOWN(RpcReplicaState.REPLICA_STATE_UNKNOWN);

    private final RpcReplicaState rpc;

    ReplicaState(RpcReplicaState rpc) {
        this.rpc = rpc;
    }

    @Nonnull
    public static ReplicaState fromRpc(@Nonnull RpcReplicaState rpc) {
        for (ReplicaState value : values()) {
            if (value.rpc == rpc) {
                return value;
            }
        }
        return UNKNOWN;
    }

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