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

import com.atlassian.stash.internal.scm.git.command.AbstractGitCommandBuilder;
import com.atlassian.bitbucket.scm.git.command.GitCommand;
import com.atlassian.bitbucket.scm.git.command.GitScmCommandBuilder;
import com.atlassian.bitbucket.scm.git.command.symbolicref.GitSymbolicRefSetBuilder;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nonnull;

/**
 * @since 1.2
 */
public class DefaultGitSymbolicRefSetBuilder
        extends AbstractGitCommandBuilder<GitSymbolicRefSetBuilder>
        implements GitSymbolicRefSetBuilder {

    private final String name;
    private final String ref;

    private String reason;

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

    @Nonnull
    @Override
    public GitCommand<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;
    }
}
