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

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;

public class DefaultGitRemoteSetUrlBuilder
        extends AbstractGitProcessBuilder<GitRemoteSetUrlBuilder>
        implements GitRemoteSetUrlBuilder {

    private final String name;
    private final String newUrl;

    private boolean push;

    public DefaultGitRemoteSetUrlBuilder(GitProcessBuilder builder, String name, String newUrl) {
        super(builder);

        this.name = name;
        this.newUrl = newUrl;
    }

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

    @Nonnull
    @Override
    public GitRemoteSetUrlBuilder push(boolean value) {
        push = value;

        return self();
    }

    @Override
    protected void applyArguments() {
        builder.argument("set-url");
        if (push) {
            builder.argument("--push");
        }
        builder.argument(name)
                .argument(newUrl);
    }

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