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

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 org.apache.commons.lang3.StringUtils;

import javax.annotation.Nonnull;

public class DefaultGitSymbolicRefSetBuilder
        extends AbstractGitProcessBuilder<GitSymbolicRefSetBuilder>
        implements GitSymbolicRefSetBuilder {

    private final String name;
    private final String ref;

    private String reason;

    public DefaultGitSymbolicRefSetBuilder(GitProcessBuilder builder, String name, String ref) {
        super(builder);
        this.name = name;
        this.ref = ref;
    }

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

    @Nonnull
    @Override
    public GitSymbolicRefSetBuilder reason(String value) {
        reason = value;

        return self();
    }

    @Override
    protected void applyArguments() {
        if (StringUtils.isNotBlank(reason)) {
            builder.argument("-m").argument(reason);
        }
        builder.argument(name)
                .argument(ref);
    }

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