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

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

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

import static java.util.Objects.requireNonNull;

/**
 * Enumerates the possible modes for {@code git diff-core --color}.
 */
public enum GitDiffColor {

    /**
     * Always include color markers in the diff output.
     */
    ALWAYS("always"),
    /**
     * Automatically turn color markers in diff output on when connected to a terminal.
     * <p>
     * <b>Warning</b>: This value is included for completeness in enumerating Git's supported color modes. However,
     * it functions the same as {@link #NEVER}, here, since the system is not a terminal.
     */
    AUTO("auto"),
    /**
     * Uses the {@code .gitconfig}-configured setting for color markers in diff output.
     */
    DEFAULT,
    /**
     * Never include color markers in the diff output, <i>even if it is configured</i> in {@code .gitconfig}.
     */
    NEVER("never");

    private final String flag;

    GitDiffColor() {
        flag = null;
    }

    GitDiffColor(String mode) {
        flag = "--color=" + mode;
    }

    @Nonnull
    public static GitDiffColor fromRpc(@Nonnull RpcDiffColor rpc) {
        switch (requireNonNull(rpc, "rpc")) {
            case COLOR_ALWAYS:
                return ALWAYS;
            case COLOR_AUTO:
                return AUTO;
            case COLOR_NEVER:
                return NEVER;
            default: //Unspecified
                return DEFAULT;
        }
    }

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

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