package com.atlassian.bitbucket.mesh.git.builder.revlist;

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

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static java.util.Objects.requireNonNull;

/**
 * Enumerates the possible merge commit modes for {@code git rev-list}, controlling use of the {@code --merges},
 * {@code --no-merges} and {@code --no-min-parents} flags.
 */
public enum GitRevListMerges {

    /**
     * Apply {@code git rev-list}'s default behaviour.
     */
    DEFAULT,
    /**
     * Exclude merge commits.
     */
    EXCLUDE("--no-merges"),
    /**
     * Include merge commits. This does not exclude non-merge commits.
     */
    INCLUDE("--no-min-parents"),
    /**
     * Include <i>only</i> merge commits.
     */
    ONLY("--merges");

    private final String flag;

    GitRevListMerges() {
        flag = null;
    }

    GitRevListMerges(String flag) {
        this.flag = flag;
    }

    @Nonnull
    public static GitRevListMerges fromRpc(@Nonnull RpcRevListMerges rpc) {
        switch (requireNonNull(rpc, "rpc")) {
            case MERGES_EXCLUDE:
                return EXCLUDE;
            case MERGES_INCLUDE:
                return INCLUDE;
            case MERGES_ONLY:
                return ONLY;
            default:
                return DEFAULT;
        }
    }

    @Nullable
    public String getFlag() {
        return flag;
    }

    public boolean isFlagged() {
        return flag != null;
    }
}