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

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

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

/**
 * Enumerates the possible modes for rename detection in {@code git diff-core}.
 */
public enum GitDiffRenames {

    /**
     * Enables {@code --find-copies}. This mode {@link #supportsThreshold() supports a threshold}.
     */
    FIND_COPIES,
    /**
     * Enables {@code --find-copies} <i>and</i> {@code --find-copies-harder}. This mode {@link #supportsThreshold()
     * supports a threshold}.
     */
    FIND_COPIES_HARDER,
    /**
     * Enables {@code --find-renames}. This mode {@link #supportsThreshold() supports a threshold}.
     */
    FIND_RENAMES,
    /**
     * Disables rename and copy detection. This mode <i>does not</i> {@link #supportsThreshold() support a threshold}.
     */
    OFF {

        /**
         * @inheritDoc
         */
        @Override
        public boolean supportsThreshold() {
            return false;
        }
    };

    @Nonnull
    public static GitDiffRenames fromRpc(@Nonnull RpcDiffRenames rpc) {
        switch (requireNonNull(rpc, "rpc")) {
            case RENAMES_COPIES:
                return FIND_COPIES;
            case RENAMES_MORE_COPIES:
                return FIND_COPIES_HARDER;
            case RENAMES_ON:
                return FIND_RENAMES;
            default: //Off, unspecified
                return OFF;
        }
    }

    /**
     * Retrieves a flag indicating whether this mode supports appending a threshold. When supported, the threshold
     * controls the necessary similarity percentage files must have to be marked as copies or renames.
     *
     * @return {@code true} if this mode supports thresholds; otherwise, {@code false}
     */
    public boolean supportsThreshold() {
        return true;
    }
}
