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

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

import javax.annotation.Nonnull;

public class DefaultGitConfigSetBuilder
        extends AbstractTypedGitConfigBuilder<GitConfigSetBuilder>
        implements GitConfigSetBuilder {

    private final String value;

    private boolean replaceAll;

    public DefaultGitConfigSetBuilder(GitProcessBuilder builder, String name, String value) {
        super(builder, name);

        this.value = requireNonBlank(value, "value");
    }

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

    @Nonnull
    @Override
    public GitConfigSetBuilder replaceAll() {
        replaceAll = true;

        return self();
    }

    @Override
    protected void applyArguments() {
        super.applyArguments();

        if (replaceAll) {
            builder.argument("--replace-all");
        }
        builder.argument(name).argument(String.valueOf(value));
    }

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