package com.atlassian.stash.internal.scm.git.command.merge;

import com.atlassian.fugue.Option;
import com.atlassian.bitbucket.scm.git.command.GitScmCommandBuilder;
import com.atlassian.bitbucket.scm.git.command.merge.GitMergeBaseBuilder;

import javax.annotation.Nonnull;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @since 1.3
 */
public class DefaultGitMergeBaseBuilder
        extends AbstractGitMergeBaseBuilder<GitMergeBaseBuilder>
        implements GitMergeBaseBuilder {

    private final Option<String> mode;

    private boolean all;

    public DefaultGitMergeBaseBuilder(@Nonnull GitScmCommandBuilder builder) {
        this(builder, Option.none(String.class));
    }

    public DefaultGitMergeBaseBuilder(@Nonnull GitScmCommandBuilder builder, @Nonnull Option<String> mode) {
        super(builder);

        this.mode = checkNotNull(mode, "mode");
    }

    @Nonnull
    @Override
    public GitMergeBaseBuilder all(boolean value) {
        all = value;

        return self();
    }

    @Override
    protected void applyModeArguments() {
        if (all) {
            builder.argument("--all");
        }
        for (String argument : mode) {
            builder.argument(argument);
        }
    }

    @Nonnull
    @Override
    protected GitMergeBaseBuilder self() {
        return this;
    }
}
