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

import com.atlassian.stash.internal.scm.git.command.AbstractGitCommandBuilder;
import com.atlassian.bitbucket.scm.git.command.GitScmCommandBuilder;
import com.atlassian.bitbucket.scm.git.command.notes.GitNotesBuilderSupport;

import javax.annotation.Nonnull;

/**
 * @since 1.3
 */
public abstract class AbstractGitNotesBuilder<B extends GitNotesBuilderSupport<B>>
        extends AbstractGitCommandBuilder<B>
        implements GitNotesBuilderSupport<B> {

    private final String object;
    private final String subcommand;

    private String ref;

    protected AbstractGitNotesBuilder(GitScmCommandBuilder builder, String object, String subcommand) {
        super(builder);

        this.object = checkNotBlank(object, "object");
        this.subcommand = checkNotBlank(subcommand, "subcommand");
    }

    @Nonnull
    @Override
    public B ref(String value) {
        ref = value;

        return self();
    }

    @Override
    protected final void applyArguments() {
        //Strangely, on git notes, the --ref flag must be _before_ the subcommand or it will not be applied correctly
        if (ref != null) {
            builder.argument("--ref")
                    .argument(ref);
        }
        builder.argument(subcommand);

        //Add any options configured on the concrete builder type
        applyOptions();

        //After the arguments, apply the object (Note: not all subcommands require this, but all the ones for which
        //there is a type-safe builder do)
        builder.argument(object);
    }

    protected void applyOptions() {
        //Some subcommands, such as show, do not have any options that can be set
    }
}
