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

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

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

import static java.util.Objects.requireNonNull;

/**
 * A flag for indicating how whitespace should be handled in a diff, such as being {@link #IGNORE_ALL_SPACE ignored}.
 */
public enum GitDiffWhitespace {

    /**
     * The default whitespace setting, where the exact changes of the diff are shown, whitespace and all.
     */
    SHOW(null),
    /**
     * Ignore all whitespace in the diff.
     */
    IGNORE_ALL_SPACE("--ignore-all-space");

    private final String flag;

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

    @Nonnull
    public static GitDiffWhitespace fromRpc(@Nonnull RpcDiffWhitespace rpc) {
        if (requireNonNull(rpc, "rpc") == RpcDiffWhitespace.WHITESPACE_IGNORE_ALL) {
            return IGNORE_ALL_SPACE;
        }

        return SHOW; //Show, unspecified
    }

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

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