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

import com.atlassian.stash.internal.scm.git.command.AbstractGitCommandBuilder;
import com.atlassian.bitbucket.scm.git.command.GitScmCommandBuilder;
import com.atlassian.bitbucket.scm.git.command.lsfiles.GitLsFilesBuilder;
import com.google.common.collect.Sets;

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

public class DefaultGitLsFilesBuilder extends AbstractGitCommandBuilder<GitLsFilesBuilder>
        implements GitLsFilesBuilder {

    private final Set<String> paths;

    private boolean stage;

    public DefaultGitLsFilesBuilder(GitScmCommandBuilder builder) {
        super(builder.command("ls-files"));

        paths = Sets.newHashSet();
    }

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

        return self();
    }

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

        return self();
    }

    @Nonnull
    @Override
    public GitLsFilesBuilder stage(boolean value) {
        this.stage = value;

        return self();
    }

    @Override
    protected void applyArguments() {
        if (stage) {
            builder.argument("--stage");
        }

        builder.argument("--");
        for (String path : paths) {
            builder.argument(path);
        }
    }

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