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

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

import javax.annotation.Nonnull;

public abstract class AbstractGitNotesBuilder<B extends GitNotesBuilderSupport<B>>
        extends AbstractGitProcessBuilder<B>
        implements GitNotesBuilderSupport<B> {

    private final String object;
    private final String subcommand;

    private String ref;

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

        this.object = requireNonBlank(object, "object");
        this.subcommand = requireNonBlank(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
    }
}
