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

import com.atlassian.stash.internal.scm.git.command.AbstractGitCommandBuilder;
import com.atlassian.bitbucket.scm.git.command.GitCommand;
import com.atlassian.bitbucket.scm.git.command.GitScmCommandBuilder;
import com.atlassian.bitbucket.scm.git.command.add.GitAddBuilder;
import com.google.common.collect.Sets;

import javax.annotation.Nonnull;
import java.util.Set;

/**
 * @since 1.3
 */
public class DefaultGitAddBuilder extends AbstractGitCommandBuilder<GitAddBuilder> implements GitAddBuilder {

    private final Set<String> paths;

    private boolean all;
    private boolean force;

    public DefaultGitAddBuilder(GitScmCommandBuilder builder) {
        super(builder.command("add"));

        paths = Sets.newHashSet();
    }

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

        return self();
    }

    @Nonnull
    @Override
    public GitCommand<Void> build() {
        return buildWithLoggingHandler();
    }

    @Nonnull
    @Override
    public GitAddBuilder force(boolean value) {
        force = value;

        return self();
    }

    @Nonnull
    @Override
    public GitAddBuilder path(String value) {
        addIfNotBlank(paths, value);

        return self();
    }

    @Nonnull
    @Override
    public GitAddBuilder paths(Iterable<String> values) {
        addIfNotBlank(paths, values);

        return self();
    }

    @Nonnull
    @Override
    public GitAddBuilder paths(String value, String... values) {
        addIfNotBlank(paths, value, values);

        return self();
    }

    @Override
    protected void applyArguments() {
        if (all) {
            builder.argument("-A");
        }
        if (force) {
            builder.argument("--force");
        }
        builder.argument("--");
        for (String path : paths) {
            builder.argument(path);
        }
    }

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