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

import com.atlassian.bitbucket.mesh.git.builder.AbstractGitProcessBuilder;
import com.atlassian.bitbucket.mesh.git.builder.GitProcess;
import com.atlassian.bitbucket.mesh.git.builder.GitProcessBuilder;

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

public class DefaultGitConfigListBuilder
        extends AbstractGitProcessBuilder<GitConfigListBuilder>
        implements GitConfigListBuilder {

    private boolean nameOnly;
    private boolean nullTerminated;

    public DefaultGitConfigListBuilder(GitProcessBuilder builder) {
        super(builder);
    }

    @Nonnull
    @Override
    public GitProcess<List<String>> build() {
        return build(new ListConfigStdioHandler(nullTerminated));
    }

    @Nonnull
    @Override
    public GitConfigListBuilder nameOnly(boolean value) {
        nameOnly = value;

        return self();
    }

    @Nonnull
    @Override
    public GitConfigListBuilder nullTerminated(boolean value) {
        nullTerminated = value;

        return self();
    }

    @Override
    protected void applyArguments() {
        if (nameOnly) {
            builder.argument("--name-only");
        }
        if (nullTerminated) {
            builder.argument("-z");
        }
        builder.argument("--list");
    }

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